From 25e8a4c92b0df7207e0d95fc74652119789d109d Mon Sep 17 00:00:00 2001 From: jaydee Date: Sat, 8 Jun 2024 23:37:48 +0200 Subject: [PATCH] bitwarden --- playbooks/files/scripts/mqtt_srv.py | 133 +++++++++++++++++----------- 1 file changed, 83 insertions(+), 50 deletions(-) diff --git a/playbooks/files/scripts/mqtt_srv.py b/playbooks/files/scripts/mqtt_srv.py index d126652..114dfb7 100644 --- a/playbooks/files/scripts/mqtt_srv.py +++ b/playbooks/files/scripts/mqtt_srv.py @@ -7,7 +7,6 @@ if paho.mqtt.__version__[0] > '1': client = mqttClient.Client(mqttClient.CallbackAPIVersion.VERSION1) else: client = mqttClient.Client() - from getmac import get_mac_address import platform import socket @@ -25,7 +24,7 @@ import random import requests #import psutil stats = {} -VERSION = "1.0.23" +VERSION = "1.0.26" curos = platform.system() host = platform.node().lower() print(host) @@ -84,27 +83,32 @@ def writeLog(msg, svr="INFO"): def check_router(): stats["uptime"] = uptime() stats["version"] = VERSION + stats["status"] = "on" cmnd = "crontab -l" status, output = subprocess.getstatusoutput(cmnd) if "#Skynet_banmalware#" in output: - stats["skynet"] = "on" + stats["skynet"] = "On" else: - stats["skynet"] = "off" + stats["skynet"] = "Off" if "#Diversion_CountAds#" in output: - stats["diversion"] = "on" + stats["diversion"] = "On" else: - stats["diversion"] = "off" + stats["diversion"] = "Off" cmnd = "nvram get vpn_client1_state" status, output = subprocess.getstatusoutput(cmnd) - writeLog("Openvpn1 Status : " + str(output)) - stats["vpnclient1"] = output + writeLog("VpnClient1 Status : " + str(output)) + stats["vpnclient1"] = "On" if output == "2" else "Off" cmnd = "nvram get vpn_client2_state" status, output = subprocess.getstatusoutput(cmnd) - writeLog("Openvpn2 Status : " + str(output)) - stats["vpnclient2"] = output + writeLog("VpnClient2 Status : " + str(output)) + stats["vpnclient2"] = "On" if output == "2" else "Off" + cmnd = "nvram get vpn_server1_state" + status, output = subprocess.getstatusoutput(cmnd) + writeLog("VpnServer1 Status : " + str(output)) + stats["vpnserver1"] = "On" if output == "2" else "Off" cmnd = "ncat -z 192.168.77.106 25" status, output = subprocess.getstatusoutput(cmnd) if status == 0: @@ -238,11 +242,11 @@ if _PUBLISH: print("Connected to MQTT Broker!") else: print("Failed to connect, return code %d\n", rc) + if paho.mqtt.__version__[0] > '1': client = mqttClient.Client(mqttClient.CallbackAPIVersion.VERSION1) else: - client = mqttClient.Client(client_id) - + client = mqttClient.Client() client.username_pw_set(username, password) client.on_connect = on_connect client.connect(MQTT_HOST, MQTT_PORT) @@ -334,6 +338,8 @@ elif host == "SERVER": mac = str(get_mac_address("enp2s0")) elif host == "ASUS" and curos != "Windows": mac = str(get_mac_address("enp3s0f1")) +elif host == "morefine" and curos != "Windows": + mac = str(get_mac_address("eno1")) else: mac = str(get_mac_address("Ethernet")) @@ -366,7 +372,7 @@ def on_connect(client, userdata, flags, rc): if host.lower() == "nas": print("Getting VM info") - cmnd = "/share/ZFS530_DATA/.qpkg/QKVM/usr/bin/virsh list --all" + cmnd = "/share/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh list --all" print(cmnd) status, output = subprocess.getstatusoutput(cmnd) l = 1 @@ -384,7 +390,7 @@ def on_connect(client, userdata, flags, rc): for m in VMS: - cmnd = "/share/ZFS530_DATA/.qpkg/QKVM/usr/bin/virsh domstate " + m + cmnd = "/share/ZFS532_DATA/.qpkg/QKVM/usr/bin/virsh domstate " + m #out = subprocess.Popen(cmnd.split()) print(cmnd) status, output = subprocess.getstatusoutput(cmnd) @@ -473,7 +479,8 @@ def on_message(client, userdata, msg): if myObj["cmd"] == "nas_cmd": print("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"]) print(cmnd) status, output = subprocess.getstatusoutput(cmnd) print(output) @@ -489,7 +496,7 @@ def on_message(client, userdata, msg): cmd_type = "shutdown" - cmnd = "/share/ZFS530_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()) print(cmnd) if myObj["command"] == "start": @@ -791,37 +798,38 @@ def on_message(client, userdata, msg): mon3 = "DVI-I-2-1" mon4 = "eDP-1-1" ''' - mon1 = "HDMI-1" - mon2 = "HDMI-2" - mon3 = "DVI-I-1-1" - mon4 = "eDP-1" - writeLog(mon3) - if myObj["target"] == "mid": - myCmd = "xrandr --output " + mon2 + " --primary --auto --mode 1920x1080 --output " + mon3 + " --off --output " +mon1 + " --off --output " + mon4 + " --off" - elif myObj["target"] == "left": - myCmd = "xrandr --output " + mon1 + " --primary --auto --mode 1920x1080 --output " + mon3 + " --off --output " +mon2 + " --off --output " + mon4 + " --off" + right = ["HDMI-A-0", "1920x1080"] + tleft = ["HDMI-A-0", "1920x1080"] + tright = ["DVI-I-1-1", "1920x1080"] + left = ["DisplayPort-0", "2560x1440"] + mid = ["DisplayPort-1", "3440x1440"] - elif myObj["target"] == "all": - myCmd = "xrandr --output " + mon2 + " --primary --auto --mode 1920x1080 --output " + mon3 + " --auto --right-of " + mon2 + " --mode 1920x1080 --output " + mon1 + " --auto --left-of " + mon2 + " --mode 1920x1080 --output " + mon4 + " --auto --right-of " + mon2 + " --mode 1920x1080" + #xrandr --output DisplayPort-1 --primary --auto --mode 3440x1440 --output HDMI-A-0 --auto --left-of DVI-I-1-1 --mode 1920x1080 --output DisplayPort-0 --auto --left-of DisplayPort-1 --mode 2560x1440 --output DVI-I-1-1 --above DisplayPort-1 --mode 1920x1080 + if myObj["target"] == "mid": + myCmd = f"xrandr --output {mid[0]} --primary --auto --mode {mid[1]} --output {left[0]} --off --output {tleft[0]} --off --output {tright[0]} --off" + elif myObj["target"] == "left": + myCmd = f"xrandr --output {left[0]} --primary --auto --mode {left[1]} --output {mid[0]} --off --output {tleft[0]} --off --output {tright[0]} --off" + elif myObj["target"] == "all": + myCmd = f"xrandr --output {mid[0]} --primary --auto --mode {mid[1]} --pos 0x0 --output {tleft[0]} --auto --pos -1920x-1080 --mode {tleft[1]} --output {left[0]} --auto --left-of {mid[0]} --mode {left[1]} --output {tright[0]} --above {mid[0]} --mode {tright[1]}" + #myCmd = f"xrandr --output {mid[0]} --primary --auto --mode {mid[1]} --output {right[0]} --auto --right-of {mid[0]} --mode {right[1]} --output {left[0]} --auto --left-of {mid[0]} --mode {left[1]}" elif myObj["target"] == "midleft": - myCmd = "xrandr --output " + mon2 + " --primary --auto --mode 1920x1080 --output " + mon3 + " --off --output " + mon1 + " --auto --left-of " + mon2 + " --mode 1920x1080 --output " + mon4 + " --off" + myCmd = f"xrandr --output {mid[0]} --primary --auto --mode {mid[1]} --output {tleft[0]} --off --output {left[0]} --auto --left-of {mid[0]} --mode {left[1]} --output {tright[0]} --off" elif myObj["target"] == "midtop": - myCmd = "xrandr --output " + mon2 + " --primary --auto --mode 1920x1080 --output " + mon3 + " --auto --right-of " + mon2 + " --mode 1920x1080 --output " + mon1 + " --off --output " + mon4 + " --off" - elif myObj["target"] == "midleftbuild": - myCmd = "xrandr --output " + mon2 + " --primary --auto --mode 1920x1080 --output " + mon3 + " --off --output " + mon1 + " --auto --left-of " + mon2 + " --mode 1920x1080 --output " + mon4 + " --auto --right-of " + mon2 + " --mode 1920x1080" - elif myObj["target"] == "midbuild": - myCmd = "xrandr --output " + mon2 + " --primary --auto --mode 1920x1080 --output " + mon3 + " --off --output " + mon1 + " --off --output " + mon4 + " --auto --right-of " + mon2 + " --mode 1920x1080" - elif myObj["target"] == "midlefttop": - myCmd = "xrandr --output " + mon2 + " --primary --auto --mode 1920x1080 --output " + mon3 + " --auto --right-of " + mon2 + " --mode 1920x1080 --output " + mon1 + " --auto --left-of " + mon2 + " --mode 1920x1080 --output " + mon4 + " --off" - elif myObj["target"] == "build": - myCmd = "xrandr --output " + mon2 + " --off --output " + mon3 + " --off --output " + mon1 + " --off --output " + mon4 + " --auto --mode 1920x1080" + myCmd = f"xrandr --output {mid[0]} --primary --auto --mode {mid[1]} --output {tright[0]} --auto --above {mid[0]} --mode {tright[1]} --output {left[0]} --off --output {tleft[0]} --off" + elif myObj["target"] == "midlefttleft": + myCmd = f"xrandr --output {mid[0]} --primary --auto --mode {mid[1]} --pos 0x0 --output {tright[0]} --off --output {left[0]} --auto --left-of {mid[0]} --mode {left[1]} --output {tleft[0]} --auto --pos -1920x-1080 --mode {tleft[1]}" + elif myObj["target"] == "lefttlefttright": + myCmd = f"xrandr --output {mid[0]} --off --output {tleft[0]} --auto --pos 640x-1080 --mode {tright[1]} --output {left[0]} --primary --auto --mode {left[1]} --pos 0x0 --output {tright[0]} --right-of {tleft[0]} --mode {tright[1]}" + elif myObj["target"] == "lefttleft": + myCmd = f"xrandr --output {mid[0]} --off --output {tleft[0]} --auto --pos 640x-1080 --mode {tright[1]} --output {left[0]} --primary --auto --mode {left[1]} --pos 0x0 --output {tright[0]} --off" + else: pass writeLog(myCmd) if myCmd != "": print(myCmd) - writeLog("Executing") + writeLog("Executing : {myCmd}") #subprocess.Popen(myCmd.split()) status, output = subprocess.getstatusoutput(myCmd) writeLog(output) @@ -924,13 +932,13 @@ def on_message(client, userdata, msg): except: print("not a json!") -try: +if paho.mqtt.__version__[0] > '1': client = mqttClient.Client(mqttClient.CallbackAPIVersion.VERSION1) -except: +else: client = mqttClient.Client() -payload = "off" +payload = '{"status": "off"}' client.username_pw_set("jaydee", password="jaydee1") -client.will_set("sectorq/systems/" + host.lower() + "/stat", payload=payload, qos=0, retain=True) +client.will_set("home-assistant/" + host.lower() + "/hwstats", payload=payload, qos=0, retain=True) client.on_connect = on_connect client.on_message = on_message writeLog(MQTT_HOST) @@ -998,6 +1006,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: @@ -1022,6 +1031,7 @@ if curos == "Windows": print(swap_data) print("MEM : {}".format(mem_data.percent)) print(psutil.getloadavg()) + #lodctr /R try: print(psutil.sensors_temperatures()) except: @@ -1044,24 +1054,47 @@ else: NEXT_CHECK = 0 while True: - print(VERSION) + #print(VERSION) time.sleep(5) try: mtime = os.path.getmtime(sys.argv[0]) except OSError: mtime = 0 - print(mtime) + #print(mtime) if NEXT_CHECK < int(time.time()): NEXT_CHECK = int(time.time()) + 20 writeLog("Blalala : Next : " + str(NEXT_CHECK)) - if host.lower() == "asus": + if host.lower() == "asus" or host.lower() == "morefine": print(psutil.sensors_temperatures()) #print(psutil.sensors_fans()) sences = psutil.sensors_temperatures() - cpu_data = sences["coretemp"][0] + print("SENCES") + print(sences) + print("#"*40) + try: + cpu_data = sences["coretemp"][0] + except: + try: + cpu_data = sences["k10temp"][0] + except: + cpu_data = "None" + print("CPUDATA") + print(cpu_data) + print("#"*40) + + mb_data = sences["acpitz"][0] - fan_data = psutil.sensors_fans()["asus"][0] + print("MBDATA") + print(mb_data) + print(mb_data.current) + print("#"*40) + + + try: + fan_data = psutil.sensors_fans()["asus"][0].current + except: + fan_data = "None" mem_data = psutil.virtual_memory() swap_data = psutil.swap_memory() #cpu_usage = psutil.cpu_percent(interval=1) @@ -1082,7 +1115,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 + '","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='{"status":"on", "version":"' + VERSION + '","TCPU":"NA", "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) + '", "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()) @@ -1124,7 +1157,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": @@ -1176,4 +1209,4 @@ else: os.execv(sys.executable, ['python3'] + sys.argv) #os.startfile(sys.argv[0]) - sys.exit() + sys.exit() \ No newline at end of file