mirror of
https://gitlab.sectorq.eu/jaydee/mqtt_srv.git
synced 2025-12-14 10:34: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()
|
||||
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":
|
||||
import winreg
|
||||
def uptime():
|
||||
@@ -83,13 +90,7 @@ else:
|
||||
|
||||
if os.path.exists(log_path):
|
||||
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():
|
||||
stats["uptime"] = uptime()
|
||||
@@ -216,7 +217,7 @@ try:
|
||||
opts, args = getopt.getopt(sys.argv[1:], "Sspmt:", ["command=", "help", "output="])
|
||||
except getopt.GetoptError as err:
|
||||
# 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()
|
||||
sys.exit(2)
|
||||
output = None
|
||||
@@ -266,9 +267,9 @@ if _PUBLISH:
|
||||
# result: [0, 1]
|
||||
status = result[0]
|
||||
if status == 0:
|
||||
print(f"Send `{_MESSAGE}` to topic `{_TOPIC}`")
|
||||
writeLog(f"Send `{_MESSAGE}` to topic `{_TOPIC}`")
|
||||
else:
|
||||
print(f"Failed to send message to topic {_TOPIC}")
|
||||
writeLog(f"Failed to send message to topic {_TOPIC}")
|
||||
|
||||
client = connect_mqtt()
|
||||
publish(client)
|
||||
@@ -336,7 +337,6 @@ while ex == 0:
|
||||
time.sleep(5)
|
||||
|
||||
|
||||
print("OS : " + curos)
|
||||
writeLog("OS : " + curos)
|
||||
if curos != "Windows":
|
||||
import autorandr
|
||||
@@ -356,17 +356,13 @@ else:
|
||||
mac = str(get_mac_address("Ethernet"))
|
||||
|
||||
|
||||
print("MAC : " + mac)
|
||||
writeLog("MAC : " + mac)
|
||||
print("SYSTEM : " + host)
|
||||
writeLog("SYSTEM : " + host)
|
||||
print("IP : " + IP)
|
||||
writeLog("IP : " + IP)
|
||||
|
||||
flag_connected = 0
|
||||
# The callback for when the client receives a CONNACK response from the server.
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
print("Connected with result code " + str(rc))
|
||||
writeLog("Connected with result code " + str(rc))
|
||||
global flag_connected
|
||||
flag_connected = 1
|
||||
@@ -375,7 +371,6 @@ def on_connect(client, userdata, flags, rc):
|
||||
topic = "sectorq/systems/" + host.lower()
|
||||
topic2 = "sectorq/systems/all"
|
||||
MQTT_TOPIC = [(topic,0),(topic2,0)]
|
||||
print(topic)
|
||||
writeLog(topic)
|
||||
time.sleep(1)
|
||||
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":
|
||||
check_router()
|
||||
|
||||
if host.lower() == "nas":
|
||||
print("Getting VM info")
|
||||
|
||||
if host.lower() == "nas":
|
||||
cmnd = "/share/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh list --all"
|
||||
print(cmnd)
|
||||
writeLog(cmnd)
|
||||
status, output = subprocess.getstatusoutput(cmnd)
|
||||
l = 1
|
||||
lines = output.splitlines()
|
||||
@@ -397,7 +390,7 @@ def on_connect(client, userdata, flags, rc):
|
||||
if l < 4:
|
||||
continue
|
||||
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())
|
||||
|
||||
|
||||
@@ -406,9 +399,9 @@ def on_connect(client, userdata, flags, rc):
|
||||
|
||||
cmnd = "/share/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh domstate " + m
|
||||
#out = subprocess.Popen(cmnd.split())
|
||||
print(cmnd)
|
||||
writeLog(cmnd)
|
||||
status, output = subprocess.getstatusoutput(cmnd)
|
||||
print(output)
|
||||
writeLog(output)
|
||||
if output.strip() == "running":
|
||||
client.publish("sectorq/systems/nas/" + m, payload=1, qos=0, retain=True)
|
||||
else:
|
||||
@@ -417,7 +410,6 @@ def on_connect(client, userdata, flags, rc):
|
||||
client.subscribe(MQTT_TOPIC)
|
||||
|
||||
def on_disconnect(client, userdata, rc):
|
||||
print("disconnecting reason " +str(rc))
|
||||
writeLog("disconnecting reason " +str(rc))
|
||||
client.connected_flag=False
|
||||
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.
|
||||
def on_message(client, userdata, msg):
|
||||
print(msg.topic)
|
||||
print(msg.payload)
|
||||
writeLog(msg.topic)
|
||||
writeLog(msg.payload)
|
||||
myObj = msg.payload.decode('utf-8')
|
||||
# command = re.sub(r"^b\'|\'$", "", str(msg.payload))
|
||||
# command = json.loads(msg.payload.decode("utf-8"))
|
||||
@@ -436,49 +428,36 @@ def on_message(client, userdata, msg):
|
||||
myObj = json.loads(msg.payload)
|
||||
try:
|
||||
writeLog(myObj["action"])
|
||||
print(myObj["action"])
|
||||
except:
|
||||
print("Action not set")
|
||||
writeLog("Action not set", "WARNING")
|
||||
|
||||
try:
|
||||
print(myObj["cmd"])
|
||||
writeLog(myObj["cmd"])
|
||||
except:
|
||||
print("Cmd not set")
|
||||
writeLog("Cmd not set", "WARNING")
|
||||
|
||||
try:
|
||||
print(myObj["target"])
|
||||
writeLog(myObj["target"])
|
||||
except:
|
||||
print("Target Not Set")
|
||||
writeLog("Target Not Set", "WARNING")
|
||||
try:
|
||||
print(myObj["status"])
|
||||
writeLog(myObj["status"])
|
||||
except:
|
||||
print("Status Not Set")
|
||||
writeLog("Status Not Set")
|
||||
|
||||
try:
|
||||
print(myObj["command"])
|
||||
writeLog(myObj["command"])
|
||||
except:
|
||||
print("Command Not Set")
|
||||
writeLog("Command Not Set")
|
||||
|
||||
try:
|
||||
print(myObj["cmd_line"])
|
||||
writeLog(myObj["cmd_line"])
|
||||
except:
|
||||
print("Cmd_line Not Set")
|
||||
writeLog("Cmd_line Not Set")
|
||||
|
||||
|
||||
if myObj["cmd"] == "command":
|
||||
|
||||
print("Executing " + myObj["cmd_line"])
|
||||
writeLog("Executing " + myObj["cmd_line"])
|
||||
#os.system('start /b "' + myObj["cmd_line"] + '"')
|
||||
try:
|
||||
@@ -487,23 +466,22 @@ def on_message(client, userdata, msg):
|
||||
# ctypes.windll.user32.MessageBoxW(0, myObj["cmd_line"], "Executed", 64)
|
||||
except:
|
||||
ctypes.windll.user32.MessageBoxW(0, "Your text", "Your title", 16)
|
||||
print("Executed " + myObj["cmd_line"])
|
||||
writeLog("Executed " + myObj["cmd_line"])
|
||||
|
||||
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/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh domstate " + str(myObj["target"])
|
||||
print(cmnd)
|
||||
writeLog(cmnd)
|
||||
status, output = subprocess.getstatusoutput(cmnd)
|
||||
print(output)
|
||||
writeLog(output)
|
||||
cmd_type = myObj["command"]
|
||||
if myObj["command"] == "start":
|
||||
if output.strip() == "paused":
|
||||
print("System suspended")
|
||||
writeLog("System suspended")
|
||||
cmd_type = "resume"
|
||||
else:
|
||||
print("System Off")
|
||||
writeLog("System Off")
|
||||
cmd_type = "start"
|
||||
if myObj["command"] == "suspend":
|
||||
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"])
|
||||
subprocess.Popen(cmnd.split())
|
||||
print(cmnd)
|
||||
writeLog(cmnd)
|
||||
if myObj["command"] == "start":
|
||||
try:
|
||||
print("sending")
|
||||
writeLog("sending")
|
||||
client.publish("sectorq/systems/nas/" + str(myObj["target"]), payload=1, qos=0, retain=False)
|
||||
except:
|
||||
print("failed to execute!")
|
||||
writeLog("failed to execute!")
|
||||
client.publish("sectorq/systems/nas/" + str(myObj["target"]), payload="failed", qos=0, retain=False)
|
||||
else:
|
||||
try:
|
||||
print("sending")
|
||||
writeLog("sending")
|
||||
client.publish("sectorq/systems/nas/" + str(myObj["target"]), payload=0, qos=0, retain=False)
|
||||
except:
|
||||
print("failed to execute!")
|
||||
writeLog("failed to execute!")
|
||||
client.publish("sectorq/systems/nas/" + str(myObj["target"]), payload="failed", qos=0, retain=False)
|
||||
|
||||
|
||||
if myObj["cmd"] == "distrib":
|
||||
print("distributin313g")
|
||||
writeLog("distributin313g")
|
||||
# os.startfile(sys.argv[0])
|
||||
#sys.exit()
|
||||
subprocess.Popen(['/bin/systemctl', 'restart', 'mqtt'])
|
||||
@@ -1178,7 +1156,7 @@ else:
|
||||
elif host.lower() == "router":
|
||||
check_router()
|
||||
elif host.lower() == "nas":
|
||||
print("Getting VM info")
|
||||
#print("Getting VM info")
|
||||
payload = {"status":"on"}
|
||||
cmnd = "/share/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh list --all"
|
||||
writeLog(cmnd)
|
||||
@@ -1191,7 +1169,7 @@ else:
|
||||
if l < 4:
|
||||
continue
|
||||
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())
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user