mirror of
https://gitlab.sectorq.eu/jaydee/python.git
synced 2025-12-14 02:34:53 +01:00
das
This commit is contained in:
116
mqtt_srv.pyw
116
mqtt_srv.pyw
@@ -18,16 +18,53 @@ import getopt
|
||||
import random
|
||||
import requests
|
||||
#import psutil
|
||||
|
||||
|
||||
stats = {}
|
||||
VERSION = "1.0.10"
|
||||
VERSION = "1.0.22"
|
||||
curos = platform.system()
|
||||
host = platform.node().lower()
|
||||
print(host)
|
||||
if curos == "Windows":
|
||||
import winreg
|
||||
|
||||
def uptime():
|
||||
|
||||
try:
|
||||
f = open( "/proc/uptime" )
|
||||
contents = f.read().split()
|
||||
f.close()
|
||||
except:
|
||||
return "Cannot open uptime file: /proc/uptime"
|
||||
|
||||
total_seconds = float(contents[0])
|
||||
|
||||
# Helper vars:
|
||||
MINUTE = 60
|
||||
HOUR = MINUTE * 60
|
||||
DAY = HOUR * 24
|
||||
|
||||
# Get the days, hours, etc:
|
||||
days = int( total_seconds / DAY )
|
||||
hours = int( ( total_seconds % DAY ) / HOUR )
|
||||
minutes = int( ( total_seconds % HOUR ) / MINUTE )
|
||||
seconds = int( total_seconds % MINUTE )
|
||||
|
||||
# Build up the pretty string (like this: "N days, N hours, N minutes, N seconds")
|
||||
string = ""
|
||||
# if days > 0:
|
||||
# string += str(days) + " " + (days == 1 and "d" or "d" ) + ", "
|
||||
# if len(string) > 0 or hours > 0:
|
||||
# string += str(hours) + " " + (hours == 1 and "h" or "h" ) + ", "
|
||||
# if len(string) > 0 or minutes > 0:
|
||||
# string += str(minutes) + " " + (minutes == 1 and "m" or "m" ) + ", "
|
||||
if days > 0:
|
||||
string += str(days) + " " + (days == 1 and "d" or "d" ) + ", "
|
||||
if len(string) > 0 or hours > 0:
|
||||
string += str(hours) + ":"
|
||||
if len(string) > 0 or minutes > 0:
|
||||
string += str(minutes) + ":"
|
||||
string += str(seconds)
|
||||
|
||||
return string;
|
||||
|
||||
def writeLog(msg, svr="INFO"):
|
||||
ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
if curos == "Windows":
|
||||
@@ -39,6 +76,21 @@ def writeLog(msg, svr="INFO"):
|
||||
f.close()
|
||||
|
||||
def check_router():
|
||||
stats["uptime"] = uptime()
|
||||
stats["version"] = VERSION
|
||||
cmnd = "crontab -l"
|
||||
status, output = subprocess.getstatusoutput(cmnd)
|
||||
|
||||
if "#Skynet_banmalware#" in output:
|
||||
stats["skynet"] = "on"
|
||||
else:
|
||||
stats["skynet"] = "off"
|
||||
if "#Diversion_CountAds#" in output:
|
||||
stats["diversion"] = "on"
|
||||
else:
|
||||
stats["diversion"] = "off"
|
||||
|
||||
|
||||
cmnd = "nvram get vpn_client1_state"
|
||||
status, output = subprocess.getstatusoutput(cmnd)
|
||||
writeLog("Openvpn1 Status : " + str(output))
|
||||
@@ -720,28 +772,9 @@ def on_message(client, userdata, msg):
|
||||
myCmd = ""
|
||||
writeLog(myObj["target"])
|
||||
if curos == "Windows":
|
||||
if myObj["target"] == "mid":
|
||||
myCmd = "DisplayFusionCommand.exe -monitorloadprofile mid"
|
||||
elif myObj["target"] == "all":
|
||||
myCmd = "DisplayFusionCommand.exe -monitorloadprofile all"
|
||||
elif myObj["target"] == "left":
|
||||
myCmd = "DisplayFusionCommand.exe -monitorloadprofile left"
|
||||
elif myObj["target"] == "midleft":
|
||||
myCmd = "DisplayFusionCommand.exe -monitorloadprofile midleft"
|
||||
elif myObj["target"] == "midtop":
|
||||
myCmd = "DisplayFusionCommand.exe -monitorloadprofile midtop"
|
||||
elif myObj["target"] == "midleftbuild":
|
||||
myCmd = "DisplayFusionCommand.exe -monitorloadprofile midleftbuild"
|
||||
elif myObj["target"] == "midbuild":
|
||||
myCmd = "DisplayFusionCommand.exe -monitorloadprofile midbuild"
|
||||
elif myObj["target"] == "midlefttop":
|
||||
myCmd = "DisplayFusionCommand.exe -monitorloadprofile midlefttop"
|
||||
elif myObj["target"] == "build":
|
||||
myCmd = "DisplayFusionCommand.exe -monitorloadprofile build"
|
||||
elif myObj["target"] == "midtopbuild":
|
||||
myCmd = "DisplayFusionCommand.exe -monitorloadprofile midtopbuild"
|
||||
else:
|
||||
pass
|
||||
|
||||
myCmd = "DisplayFusionCommand.exe -monitorloadprofile {}".format(myObj["target"])
|
||||
|
||||
else:
|
||||
'''
|
||||
mon1 = "HDMI-1-1"
|
||||
@@ -779,8 +812,11 @@ def on_message(client, userdata, msg):
|
||||
writeLog(myCmd)
|
||||
if myCmd != "":
|
||||
print(myCmd)
|
||||
subprocess.Popen(myCmd.split())
|
||||
|
||||
writeLog("Executing")
|
||||
#subprocess.Popen(myCmd.split())
|
||||
status, output = subprocess.getstatusoutput(myCmd)
|
||||
writeLog(output)
|
||||
writeLog(status)
|
||||
if myObj["cmd"] == "display2":
|
||||
# os.startfile(sys.argv[0])
|
||||
#sys.exit()
|
||||
@@ -882,11 +918,11 @@ def on_message(client, userdata, msg):
|
||||
client = mqtt.Client()
|
||||
payload = "off"
|
||||
client.username_pw_set("jaydee", password="jaydee1")
|
||||
client.will_set("sectorq/systems/" + host.lower() + "/stat", payload=payload, qos=0, retain=False)
|
||||
client.will_set("sectorq/systems/" + host.lower() + "/stat", payload=payload, qos=0, retain=True)
|
||||
client.on_connect = on_connect
|
||||
client.on_message = on_message
|
||||
writeLog(MQTT_HOST)
|
||||
client.connect(MQTT_HOST, MQTT_PORT, 5)
|
||||
client.connect(MQTT_HOST, MQTT_PORT, 25)
|
||||
|
||||
# client.publish("sectorq/status", payload="{'action':'connect', 'system':'" + host + "', 'ip': '" + IP + "', 'mac':'" + mac + "', 'ver':'?'}", qos=0, retain=False)
|
||||
|
||||
@@ -982,11 +1018,12 @@ if curos == "Windows":
|
||||
print(psutil.sensors_fans())
|
||||
except:
|
||||
pass
|
||||
results = {"MB_TEMP":"NA", "MEM_UTIL":str(mem_data.percent), "CPU_TEMP":"NA", "FAN_CPU":"NA", "VMEM_UTIL":swap_data.percent, "CPU_UTIL":cpu_usage, "GPU_TEMP":"NA", "GPU_UTIL":"NA"}
|
||||
results = {"version":VERSION,"MB_TEMP":"NA", "MEM_UTIL":str(mem_data.percent), "CPU_TEMP":"NA", "FAN_CPU":"NA", "VMEM_UTIL":swap_data.percent, "CPU_UTIL":cpu_usage, "GPU_TEMP":"NA", "GPU_UTIL":"NA"}
|
||||
print(json.dumps(results))
|
||||
r = json.dumps(results)
|
||||
#client.publish("home-assistant/" + host + "/hwstats", payload='{"TPCHDIO":"' + str(results["TPCHDIO"]) + '", "SMEMUTI":"' + str(results["SMEMUTI"]) + '", "TCPU":"' + str(results["TCPU"]) + '", "TGPU1DIO":"' + str(results["TGPU1DIO"]) + '", "SGPU1BIUTI":"' + str(results["SGPU1BIUTI"]) + '", "FCPU":"' + str(results["FCPU"]) + '", "SVIRTMEMUTI":"' + str(results["SVIRTMEMUTI"]) + '", "SCPUUTI":"' + str(results["SCPUUTI"]) + '"}', qos=0, retain=False)
|
||||
client.publish("home-assistant/" + host + "/hwstats", payload=r, qos=0, retain=False)
|
||||
client.publish("sectorq/systems/" + host.lower() + "/stat", payload="on", qos=0, retain=True)
|
||||
#print(json.dumps(stats))
|
||||
#writeLog(json.dumps(stats))
|
||||
else:
|
||||
@@ -1033,7 +1070,8 @@ else:
|
||||
#print(str(jn1["coretemp-isa-0000"]["Package id 0"]["temp1_input"]))
|
||||
#print(str(jn1["asus-isa-0000"]["cpu_fan"]["fan1_input"]))
|
||||
#client.publish("sectorq/monitor/" + host.lower(), payload="{\"mb_temp\":0,\"mem_usage\":0,\"cpu_temp\":0,\"cpu_usage\":0,\"gpu_temp\":0,\"gpu_usage\":0}", qos=0, retain=False)
|
||||
client.publish("home-assistant/" + host + "/hwstats", payload='{"MB_TEMP":' + str(mb_data.current) + ', "MEM_UTIL":' + str(mem_data.percent) + ', "CPU_TEMP":' + str(cpu_data.current) + ', "GPU_TEMP":0, "GPU_UTIL":0, "FAN_CPU":' + str(fan_data.current) + ', "VMEM_UTIL":' + str(swap_data.percent) + ', "CPU_UTIL":' + str(cpu_usage) + '}', qos=0, retain=False)
|
||||
client.publish("home-assistant/" + host + "/hwstats", payload='{"version":"' + VERSION + '","MB_TEMP":' + str(mb_data.current) + ', "MEM_UTIL":' + str(mem_data.percent) + ', "CPU_TEMP":' + str(cpu_data.current) + ', "GPU_TEMP":0, "GPU_UTIL":0, "FAN_CPU":' + str(fan_data.current) + ', "VMEM_UTIL":' + str(swap_data.percent) + ', "CPU_UTIL":' + str(cpu_usage) + '}', qos=0, retain=False)
|
||||
client.publish("sectorq/systems/" + host.lower() + "/stat", payload="on", qos=0, retain=True)
|
||||
elif host.lower() == "openmediavault" or host.lower() == "omv":
|
||||
#print(psutil.sensors_temperatures())
|
||||
#print(psutil.sensors_fans())
|
||||
@@ -1074,7 +1112,7 @@ else:
|
||||
#print(str(jn1["coretemp-isa-0000"]["Package id 0"]["temp1_input"]))
|
||||
#print(str(jn1["asus-isa-0000"]["cpu_fan"]["fan1_input"]))
|
||||
#client.publish("sectorq/monitor/" + host.lower(), payload="{\"mb_temp\":0,\"mem_usage\":0,\"cpu_temp\":0,\"cpu_usage\":0,\"gpu_temp\":0,\"gpu_usage\":0}", qos=0, retain=False)
|
||||
client.publish("home-assistant/" + host + "/hwstats", payload='{"reboot":"' + str(reboot_pending) + '", "uptime":"' + str(sys_uptime) + '", "raid_usage": ' + str(disk_usage) + ',"rsync":"' + str(stat_rsync) +'", "MB_TEMP":' + str(mb_data.current) + ', "MEM_UTIL":' + str(mem_data.percent) + ', "CPU_TEMP":' + str(cpu_data.current) + ', "GPU_TEMP":0, "GPU_UTIL":0, "FAN_CPU":0, "VMEM_UTIL":' + str(swap_data.percent) + ', "CPU_UTIL":' + str(cpu_usage) + '}', qos=0, retain=False)
|
||||
client.publish("home-assistant/" + host + "/hwstats", payload='{"version":"' + VERSION + '","reboot":"' + str(reboot_pending) + '", "uptime":"' + str(sys_uptime) + '", "raid_usage": ' + str(disk_usage) + ',"rsync":"' + str(stat_rsync) +'", "MB_TEMP":' + str(mb_data.current) + ', "MEM_UTIL":' + str(mem_data.percent) + ', "CPU_TEMP":' + str(cpu_data.current) + ', "GPU_TEMP":0, "GPU_UTIL":0, "FAN_CPU":0, "VMEM_UTIL":' + str(swap_data.percent) + ', "CPU_UTIL":' + str(cpu_usage) + '}', qos=0, retain=False)
|
||||
elif host.lower() == "router":
|
||||
check_router()
|
||||
elif host.lower() == "nas":
|
||||
@@ -1117,7 +1155,13 @@ else:
|
||||
if host.lower() == "nas":
|
||||
sys.stdout.flush()
|
||||
os.execv("/share/ZFS530_DATA/.qpkg/QPython39/bin/python3", sys.argv)
|
||||
if host.lower() == "router":
|
||||
if host.lower() == "router" or host.lower() == "omv":
|
||||
#sys.stdout.flush()
|
||||
sys.exit()
|
||||
#os.execv("/usr/bin/python3", sys.argv)
|
||||
#os.execv(sys.argv, "")
|
||||
print("argv was",sys.argv)
|
||||
print("sys.executable was", sys.executable)
|
||||
print("restart now")
|
||||
|
||||
os.execv(sys.executable, ['python3'] + sys.argv)
|
||||
#os.startfile(sys.argv[0])
|
||||
sys.exit()
|
||||
Reference in New Issue
Block a user