-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturris2_monitor.py
75 lines (60 loc) · 2.42 KB
/
turris2_monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
import paho.mqtt.client as mqtt
import time
import json
import ubus
import shutil
import subprocess
mqtt_topic = "turris2/monitor"
monitor_dict = {"uptime": 0, "load": 0, "temperature": 0, "memory_usage": 100, "uplink_speed": 0, "uplink_strength": 0, "disk_usage": 0, "disk_free": 0}
# initialize MQTT connection
client = mqtt.Client("turris2_monitor")
time.sleep(60)
disconnected_counter = 0
while True:
ubus.connect()
sysinfo_json = ubus.call("system", "info", {})
iwinfo_json = ubus.call("iwinfo", "info", { "device":"wlan1"})
ubus.disconnect()
sysinfo = sysinfo_json[0]
iwinfo = iwinfo_json[0]
#print(sysinfo)
#print(iwinfo)
total, used, free = shutil.disk_usage("/")
disk_usage = round(100 * (used / total))
disk_free = round(free // (2**30), 1)
uptime = sysinfo["uptime"]
load = round(sysinfo["load"][1] / 65536.0, 2)
memory_usage = 100 - round(100 * sysinfo["memory"]["available"] / sysinfo["memory"]["total"])
uplink_strength = iwinfo.get("signal", "0")
uplink_speed = round(iwinfo.get("bitrate", "-120") / 1000, 1)
with open("/sys/class/thermal/thermal_zone0/temp",'r') as f:
temp = f.read()
temp = round(int(temp) / 1000)
#print(temp)
monitor_dict["uptime"] = uptime
monitor_dict["load"] = load
monitor_dict["temperature"] = temp
monitor_dict["memory_usage"] = memory_usage
monitor_dict["uplink_strength"] = uplink_strength
monitor_dict["uplink_speed"] = uplink_speed
monitor_dict["disk_usage"] = disk_usage
monitor_dict["disk_free"] = disk_free
#print(monitor_dict)
mqtt_connected = 1
try:
client.connect("xxx.xxx.xx.xxx")
except:
mqtt_connected = 0
disconnected_counter = disconnected_counter + 1
if mqtt_connected:
infot = client.publish(mqtt_topic, json.dumps(monitor_dict).encode('utf-8'), 0, True)
infot.wait_for_publish()
client.disconnect()
else:
if disconnected_counter > 2:
subprocess.run(['/sbin/wifi', 'down', 'radio2'])
time.sleep(10)
subprocess.run(['/sbin/wifi', 'up', 'radio2'])
disconnected_counter = 0
time.sleep(300)