mirror of
https://gitlab.sectorq.eu/jaydee/mqtt_srv.git
synced 2025-12-14 18:44:53 +01:00
init
This commit is contained in:
68
mqtt_srv.py
68
mqtt_srv.py
@@ -27,49 +27,43 @@ if curos == "Windows":
|
||||
import winreg
|
||||
def uptime():
|
||||
|
||||
try:
|
||||
f = open( "/proc/uptime" )
|
||||
contents = f.read().split()
|
||||
f.close()
|
||||
except:
|
||||
try:
|
||||
f = open( "/proc/uptime" )
|
||||
contents = f.read().split()
|
||||
f.close()
|
||||
except:
|
||||
return "Cannot open uptime file: /proc/uptime"
|
||||
|
||||
total_seconds = float(contents[0])
|
||||
total_seconds = float(contents[0])
|
||||
|
||||
# Helper vars:
|
||||
MINUTE = 60
|
||||
HOUR = MINUTE * 60
|
||||
DAY = HOUR * 24
|
||||
# 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 )
|
||||
# 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 = ""
|
||||
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:
|
||||
if hours < 10:
|
||||
hours = "0{}".format(hours)
|
||||
string += str(hours) + ":"
|
||||
if len(string) > 0 or minutes > 0:
|
||||
if minutes < 10:
|
||||
minutes = "0{}".format(minutes)
|
||||
string += str(minutes) + ":"
|
||||
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)
|
||||
|
||||
if seconds < 10:
|
||||
seconds = "0{}".format(seconds)
|
||||
string += str(seconds)
|
||||
return string;
|
||||
return string;
|
||||
|
||||
def writeLog(msg, svr="INFO"):
|
||||
ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
@@ -128,7 +122,6 @@ def check_router():
|
||||
serv_d[rul[0]] = {"src_port":rul[1],"dest_ip":rul[2],"dest_port":rul[3],"prot":rul[4]}
|
||||
stats["serv_dest"] = rul[2]
|
||||
break
|
||||
stats["status"] = "on"
|
||||
#stats["serv_dest"] = json.dumps(serv_d)
|
||||
#stats["serv_dest"] = json.dumps(serv_d)
|
||||
#out = subprocess.Popen(cmnd.split())
|
||||
@@ -177,10 +170,6 @@ else:
|
||||
if int(count) >= 2:
|
||||
writeLog("script exist")
|
||||
sys.exit()
|
||||
|
||||
|
||||
|
||||
|
||||
# Stops duplicate instance from running
|
||||
|
||||
writeLog(count)
|
||||
@@ -325,7 +314,7 @@ IP = get_ip()
|
||||
print("OS : " + curos)
|
||||
writeLog("OS : " + curos)
|
||||
if curos != "Windows":
|
||||
import autorandr
|
||||
import autorandr
|
||||
else:
|
||||
import winreg
|
||||
|
||||
@@ -927,9 +916,9 @@ def on_message(client, userdata, msg):
|
||||
print("not a json!")
|
||||
|
||||
client = mqtt.Client()
|
||||
payload = '{"status":"off"}'
|
||||
payload = "off"
|
||||
client.username_pw_set("jaydee", password="jaydee1")
|
||||
client.will_set("home-assistant/" + host.lower() + "/hwstats", payload=payload, qos=0, retain=True)
|
||||
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)
|
||||
@@ -997,6 +986,7 @@ if curos == "Windows":
|
||||
try:
|
||||
key = winreg.OpenKeyEx(path, r"SOFTWARE\\FinalWire\\AIDA64\\SensorValues")
|
||||
results = {}
|
||||
results["status"] = "on"
|
||||
for k in aide_refs:
|
||||
k2 = "Value." + str(k)
|
||||
try:
|
||||
@@ -1123,7 +1113,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='{"status":"on","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)
|
||||
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":
|
||||
|
||||
Reference in New Issue
Block a user