mirror of
https://gitlab.sectorq.eu/jaydee/mqtt_srv.git
synced 2025-12-14 18:44:53 +01:00
klal
This commit is contained in:
84
mqtt_srv.py
84
mqtt_srv.py
@@ -32,7 +32,14 @@ VERSION = "1.0.37"
|
|||||||
|
|
||||||
curos = platform.system()
|
curos = platform.system()
|
||||||
host = platform.node().lower().replace(".home.lan","")
|
host = platform.node().lower().replace(".home.lan","")
|
||||||
print(host)
|
def writeLog(msg, svr="INFO"):
|
||||||
|
ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
ts2 = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
||||||
|
|
||||||
|
f = open(log_path, "a")
|
||||||
|
f.write(ts + " | " + svr + " | " + str(msg) + "\n")
|
||||||
|
f.close()
|
||||||
|
writeLog(host)
|
||||||
if curos == "Windows":
|
if curos == "Windows":
|
||||||
import winreg
|
import winreg
|
||||||
def uptime():
|
def uptime():
|
||||||
@@ -83,13 +90,7 @@ else:
|
|||||||
|
|
||||||
if os.path.exists(log_path):
|
if os.path.exists(log_path):
|
||||||
shutil.move(log_path, log_path + "." + ts2)
|
shutil.move(log_path, log_path + "." + ts2)
|
||||||
def writeLog(msg, svr="INFO"):
|
|
||||||
ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
||||||
ts2 = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
|
||||||
|
|
||||||
f = open(log_path, "a")
|
|
||||||
f.write(ts + " | " + svr + " | " + str(msg) + "\n")
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
def check_router():
|
def check_router():
|
||||||
stats["uptime"] = uptime()
|
stats["uptime"] = uptime()
|
||||||
@@ -216,7 +217,7 @@ try:
|
|||||||
opts, args = getopt.getopt(sys.argv[1:], "Sspmt:", ["command=", "help", "output="])
|
opts, args = getopt.getopt(sys.argv[1:], "Sspmt:", ["command=", "help", "output="])
|
||||||
except getopt.GetoptError as err:
|
except getopt.GetoptError as err:
|
||||||
# print help information and exit:
|
# print help information and exit:
|
||||||
print(str(err)) # will print something like "option -a not recognized"
|
writeLog(str(err)) # will print something like "option -a not recognized"
|
||||||
#usage()
|
#usage()
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
output = None
|
output = None
|
||||||
@@ -266,9 +267,9 @@ if _PUBLISH:
|
|||||||
# result: [0, 1]
|
# result: [0, 1]
|
||||||
status = result[0]
|
status = result[0]
|
||||||
if status == 0:
|
if status == 0:
|
||||||
print(f"Send `{_MESSAGE}` to topic `{_TOPIC}`")
|
writeLog(f"Send `{_MESSAGE}` to topic `{_TOPIC}`")
|
||||||
else:
|
else:
|
||||||
print(f"Failed to send message to topic {_TOPIC}")
|
writeLog(f"Failed to send message to topic {_TOPIC}")
|
||||||
|
|
||||||
client = connect_mqtt()
|
client = connect_mqtt()
|
||||||
publish(client)
|
publish(client)
|
||||||
@@ -336,7 +337,6 @@ while ex == 0:
|
|||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
print("OS : " + curos)
|
|
||||||
writeLog("OS : " + curos)
|
writeLog("OS : " + curos)
|
||||||
if curos != "Windows":
|
if curos != "Windows":
|
||||||
import autorandr
|
import autorandr
|
||||||
@@ -356,17 +356,13 @@ else:
|
|||||||
mac = str(get_mac_address("Ethernet"))
|
mac = str(get_mac_address("Ethernet"))
|
||||||
|
|
||||||
|
|
||||||
print("MAC : " + mac)
|
|
||||||
writeLog("MAC : " + mac)
|
writeLog("MAC : " + mac)
|
||||||
print("SYSTEM : " + host)
|
|
||||||
writeLog("SYSTEM : " + host)
|
writeLog("SYSTEM : " + host)
|
||||||
print("IP : " + IP)
|
|
||||||
writeLog("IP : " + IP)
|
writeLog("IP : " + IP)
|
||||||
|
|
||||||
flag_connected = 0
|
flag_connected = 0
|
||||||
# The callback for when the client receives a CONNACK response from the server.
|
# The callback for when the client receives a CONNACK response from the server.
|
||||||
def on_connect(client, userdata, flags, rc):
|
def on_connect(client, userdata, flags, rc):
|
||||||
print("Connected with result code " + str(rc))
|
|
||||||
writeLog("Connected with result code " + str(rc))
|
writeLog("Connected with result code " + str(rc))
|
||||||
global flag_connected
|
global flag_connected
|
||||||
flag_connected = 1
|
flag_connected = 1
|
||||||
@@ -375,7 +371,6 @@ def on_connect(client, userdata, flags, rc):
|
|||||||
topic = "sectorq/systems/" + host.lower()
|
topic = "sectorq/systems/" + host.lower()
|
||||||
topic2 = "sectorq/systems/all"
|
topic2 = "sectorq/systems/all"
|
||||||
MQTT_TOPIC = [(topic,0),(topic2,0)]
|
MQTT_TOPIC = [(topic,0),(topic2,0)]
|
||||||
print(topic)
|
|
||||||
writeLog(topic)
|
writeLog(topic)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
client.publish("sectorq/status", payload="{'action':'connect', 'system':'" + host.lower() + "', 'ip': '" + IP + "', 'mac':'" + mac + "', 'ver':'" + VERS + "'}", qos=0, retain=False)
|
client.publish("sectorq/status", payload="{'action':'connect', 'system':'" + host.lower() + "', 'ip': '" + IP + "', 'mac':'" + mac + "', 'ver':'" + VERS + "'}", qos=0, retain=False)
|
||||||
@@ -383,11 +378,9 @@ def on_connect(client, userdata, flags, rc):
|
|||||||
if host.lower() == "router":
|
if host.lower() == "router":
|
||||||
check_router()
|
check_router()
|
||||||
|
|
||||||
if host.lower() == "nas":
|
if host.lower() == "nas":
|
||||||
print("Getting VM info")
|
|
||||||
|
|
||||||
cmnd = "/share/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh list --all"
|
cmnd = "/share/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh list --all"
|
||||||
print(cmnd)
|
writeLog(cmnd)
|
||||||
status, output = subprocess.getstatusoutput(cmnd)
|
status, output = subprocess.getstatusoutput(cmnd)
|
||||||
l = 1
|
l = 1
|
||||||
lines = output.splitlines()
|
lines = output.splitlines()
|
||||||
@@ -397,7 +390,7 @@ def on_connect(client, userdata, flags, rc):
|
|||||||
if l < 4:
|
if l < 4:
|
||||||
continue
|
continue
|
||||||
line = re.split(r" {2,}", i)
|
line = re.split(r" {2,}", i)
|
||||||
print(str(line[0].strip()) + " " + str(line[1].strip()) + " " + str(line[2].strip()))
|
writeLog(str(line[0].strip()) + " " + str(line[1].strip()) + " " + str(line[2].strip()))
|
||||||
VMS[str(line[1].strip())] = str(line[2].strip())
|
VMS[str(line[1].strip())] = str(line[2].strip())
|
||||||
|
|
||||||
|
|
||||||
@@ -406,9 +399,9 @@ def on_connect(client, userdata, flags, rc):
|
|||||||
|
|
||||||
cmnd = "/share/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh domstate " + m
|
cmnd = "/share/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh domstate " + m
|
||||||
#out = subprocess.Popen(cmnd.split())
|
#out = subprocess.Popen(cmnd.split())
|
||||||
print(cmnd)
|
writeLog(cmnd)
|
||||||
status, output = subprocess.getstatusoutput(cmnd)
|
status, output = subprocess.getstatusoutput(cmnd)
|
||||||
print(output)
|
writeLog(output)
|
||||||
if output.strip() == "running":
|
if output.strip() == "running":
|
||||||
client.publish("sectorq/systems/nas/" + m, payload=1, qos=0, retain=True)
|
client.publish("sectorq/systems/nas/" + m, payload=1, qos=0, retain=True)
|
||||||
else:
|
else:
|
||||||
@@ -417,7 +410,6 @@ def on_connect(client, userdata, flags, rc):
|
|||||||
client.subscribe(MQTT_TOPIC)
|
client.subscribe(MQTT_TOPIC)
|
||||||
|
|
||||||
def on_disconnect(client, userdata, rc):
|
def on_disconnect(client, userdata, rc):
|
||||||
print("disconnecting reason " +str(rc))
|
|
||||||
writeLog("disconnecting reason " +str(rc))
|
writeLog("disconnecting reason " +str(rc))
|
||||||
client.connected_flag=False
|
client.connected_flag=False
|
||||||
client.disconnect_flag=True
|
client.disconnect_flag=True
|
||||||
@@ -427,8 +419,8 @@ def on_disconnect(client, userdata, rc):
|
|||||||
|
|
||||||
# The callback for when a PUBLISH message is received from the server.
|
# The callback for when a PUBLISH message is received from the server.
|
||||||
def on_message(client, userdata, msg):
|
def on_message(client, userdata, msg):
|
||||||
print(msg.topic)
|
writeLog(msg.topic)
|
||||||
print(msg.payload)
|
writeLog(msg.payload)
|
||||||
myObj = msg.payload.decode('utf-8')
|
myObj = msg.payload.decode('utf-8')
|
||||||
# command = re.sub(r"^b\'|\'$", "", str(msg.payload))
|
# command = re.sub(r"^b\'|\'$", "", str(msg.payload))
|
||||||
# command = json.loads(msg.payload.decode("utf-8"))
|
# command = json.loads(msg.payload.decode("utf-8"))
|
||||||
@@ -436,49 +428,36 @@ def on_message(client, userdata, msg):
|
|||||||
myObj = json.loads(msg.payload)
|
myObj = json.loads(msg.payload)
|
||||||
try:
|
try:
|
||||||
writeLog(myObj["action"])
|
writeLog(myObj["action"])
|
||||||
print(myObj["action"])
|
|
||||||
except:
|
except:
|
||||||
print("Action not set")
|
|
||||||
writeLog("Action not set", "WARNING")
|
writeLog("Action not set", "WARNING")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print(myObj["cmd"])
|
|
||||||
writeLog(myObj["cmd"])
|
writeLog(myObj["cmd"])
|
||||||
except:
|
except:
|
||||||
print("Cmd not set")
|
|
||||||
writeLog("Cmd not set", "WARNING")
|
writeLog("Cmd not set", "WARNING")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print(myObj["target"])
|
|
||||||
writeLog(myObj["target"])
|
writeLog(myObj["target"])
|
||||||
except:
|
except:
|
||||||
print("Target Not Set")
|
|
||||||
writeLog("Target Not Set", "WARNING")
|
writeLog("Target Not Set", "WARNING")
|
||||||
try:
|
try:
|
||||||
print(myObj["status"])
|
|
||||||
writeLog(myObj["status"])
|
writeLog(myObj["status"])
|
||||||
except:
|
except:
|
||||||
print("Status Not Set")
|
|
||||||
writeLog("Status Not Set")
|
writeLog("Status Not Set")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print(myObj["command"])
|
|
||||||
writeLog(myObj["command"])
|
writeLog(myObj["command"])
|
||||||
except:
|
except:
|
||||||
print("Command Not Set")
|
|
||||||
writeLog("Command Not Set")
|
writeLog("Command Not Set")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print(myObj["cmd_line"])
|
|
||||||
writeLog(myObj["cmd_line"])
|
writeLog(myObj["cmd_line"])
|
||||||
except:
|
except:
|
||||||
print("Cmd_line Not Set")
|
|
||||||
writeLog("Cmd_line Not Set")
|
writeLog("Cmd_line Not Set")
|
||||||
|
|
||||||
|
|
||||||
if myObj["cmd"] == "command":
|
if myObj["cmd"] == "command":
|
||||||
|
|
||||||
print("Executing " + myObj["cmd_line"])
|
|
||||||
writeLog("Executing " + myObj["cmd_line"])
|
writeLog("Executing " + myObj["cmd_line"])
|
||||||
#os.system('start /b "' + myObj["cmd_line"] + '"')
|
#os.system('start /b "' + myObj["cmd_line"] + '"')
|
||||||
try:
|
try:
|
||||||
@@ -487,23 +466,22 @@ def on_message(client, userdata, msg):
|
|||||||
# ctypes.windll.user32.MessageBoxW(0, myObj["cmd_line"], "Executed", 64)
|
# ctypes.windll.user32.MessageBoxW(0, myObj["cmd_line"], "Executed", 64)
|
||||||
except:
|
except:
|
||||||
ctypes.windll.user32.MessageBoxW(0, "Your text", "Your title", 16)
|
ctypes.windll.user32.MessageBoxW(0, "Your text", "Your title", 16)
|
||||||
print("Executed " + myObj["cmd_line"])
|
|
||||||
writeLog("Executed " + myObj["cmd_line"])
|
writeLog("Executed " + myObj["cmd_line"])
|
||||||
|
|
||||||
if myObj["cmd"] == "nas_cmd":
|
if myObj["cmd"] == "nas_cmd":
|
||||||
print("Executing NAS")
|
writeLog("Executing NAS")
|
||||||
#cmnd = "/share/ZFS530_DATA/.qpkg/QKVM/usr/bin/virsh domstate " + str(myObj["target"])
|
#cmnd = "/share/ZFS530_DATA/.qpkg/QKVM/usr/bin/virsh domstate " + str(myObj["target"])
|
||||||
cmnd = "/share/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh domstate " + str(myObj["target"])
|
cmnd = "/share/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh domstate " + str(myObj["target"])
|
||||||
print(cmnd)
|
writeLog(cmnd)
|
||||||
status, output = subprocess.getstatusoutput(cmnd)
|
status, output = subprocess.getstatusoutput(cmnd)
|
||||||
print(output)
|
writeLog(output)
|
||||||
cmd_type = myObj["command"]
|
cmd_type = myObj["command"]
|
||||||
if myObj["command"] == "start":
|
if myObj["command"] == "start":
|
||||||
if output.strip() == "paused":
|
if output.strip() == "paused":
|
||||||
print("System suspended")
|
writeLog("System suspended")
|
||||||
cmd_type = "resume"
|
cmd_type = "resume"
|
||||||
else:
|
else:
|
||||||
print("System Off")
|
writeLog("System Off")
|
||||||
cmd_type = "start"
|
cmd_type = "start"
|
||||||
if myObj["command"] == "suspend":
|
if myObj["command"] == "suspend":
|
||||||
cmd_type = "shutdown"
|
cmd_type = "shutdown"
|
||||||
@@ -511,25 +489,25 @@ def on_message(client, userdata, msg):
|
|||||||
|
|
||||||
cmnd = "/share/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh " + cmd_type + " " + str(myObj["target"])
|
cmnd = "/share/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh " + cmd_type + " " + str(myObj["target"])
|
||||||
subprocess.Popen(cmnd.split())
|
subprocess.Popen(cmnd.split())
|
||||||
print(cmnd)
|
writeLog(cmnd)
|
||||||
if myObj["command"] == "start":
|
if myObj["command"] == "start":
|
||||||
try:
|
try:
|
||||||
print("sending")
|
writeLog("sending")
|
||||||
client.publish("sectorq/systems/nas/" + str(myObj["target"]), payload=1, qos=0, retain=False)
|
client.publish("sectorq/systems/nas/" + str(myObj["target"]), payload=1, qos=0, retain=False)
|
||||||
except:
|
except:
|
||||||
print("failed to execute!")
|
writeLog("failed to execute!")
|
||||||
client.publish("sectorq/systems/nas/" + str(myObj["target"]), payload="failed", qos=0, retain=False)
|
client.publish("sectorq/systems/nas/" + str(myObj["target"]), payload="failed", qos=0, retain=False)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
print("sending")
|
writeLog("sending")
|
||||||
client.publish("sectorq/systems/nas/" + str(myObj["target"]), payload=0, qos=0, retain=False)
|
client.publish("sectorq/systems/nas/" + str(myObj["target"]), payload=0, qos=0, retain=False)
|
||||||
except:
|
except:
|
||||||
print("failed to execute!")
|
writeLog("failed to execute!")
|
||||||
client.publish("sectorq/systems/nas/" + str(myObj["target"]), payload="failed", qos=0, retain=False)
|
client.publish("sectorq/systems/nas/" + str(myObj["target"]), payload="failed", qos=0, retain=False)
|
||||||
|
|
||||||
|
|
||||||
if myObj["cmd"] == "distrib":
|
if myObj["cmd"] == "distrib":
|
||||||
print("distributin313g")
|
writeLog("distributin313g")
|
||||||
# os.startfile(sys.argv[0])
|
# os.startfile(sys.argv[0])
|
||||||
#sys.exit()
|
#sys.exit()
|
||||||
subprocess.Popen(['/bin/systemctl', 'restart', 'mqtt'])
|
subprocess.Popen(['/bin/systemctl', 'restart', 'mqtt'])
|
||||||
@@ -1178,7 +1156,7 @@ else:
|
|||||||
elif host.lower() == "router":
|
elif host.lower() == "router":
|
||||||
check_router()
|
check_router()
|
||||||
elif host.lower() == "nas":
|
elif host.lower() == "nas":
|
||||||
print("Getting VM info")
|
#print("Getting VM info")
|
||||||
payload = {"status":"on"}
|
payload = {"status":"on"}
|
||||||
cmnd = "/share/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh list --all"
|
cmnd = "/share/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh list --all"
|
||||||
writeLog(cmnd)
|
writeLog(cmnd)
|
||||||
@@ -1191,7 +1169,7 @@ else:
|
|||||||
if l < 4:
|
if l < 4:
|
||||||
continue
|
continue
|
||||||
line = re.split(r" {2,}", i)
|
line = re.split(r" {2,}", i)
|
||||||
print(str(line[0].strip()) + " " + str(line[1].strip()) + " " + str(line[2].strip()))
|
writeLog(str(line[0].strip()) + " " + str(line[1].strip()) + " " + str(line[2].strip()))
|
||||||
VMS[str(line[1].strip())] = str(line[2].strip())
|
VMS[str(line[1].strip())] = str(line[2].strip())
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user