-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturris_monitor.py
85 lines (68 loc) · 2.78 KB
/
turris_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
75
76
77
78
79
80
81
82
83
84
!/usr/bin/env python
import paho.mqtt.client as mqtt
import time
import json
import ubus
import shutil
import subprocess
debug = 0
if debug == 0:
mqtt_topic = "turris/monitor"
monitor_dict = {"uptime": 0, "load": 0, "temperature": 0, "memory_usage": 100, "disk_usage": 0, "disk_free": 0, "disk_temperature": 0, "movies": 0, "series": 0, "lxc": "none"}
if debug == 0:
# initialize MQTT connection
client = mqtt.Client("turris_monitor")
time.sleep(60)
while True:
ubus.connect()
sysinfo_json = ubus.call("system", "info", {})
lxc_list = ubus.call("lxc", "list", {})
#iwinfo_json = ubus.call("iwinfo", "info", { "device":"wlan1"})
ubus.disconnect()
#print(sysinfo)
#print(iwinfo)
mtotal, mused, mfree = shutil.disk_usage("/mnt/movies")
movies_usage = round(100 * (mused / mtotal))
stotal, sused, sfree = shutil.disk_usage("/mnt/series")
series_usage = round(100 * (sused / stotal))
total, used, free = shutil.disk_usage("/srv")
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"])
with open("/sys/class/thermal/thermal_zone0/temp",'r') as f:
temp = f.read()
temp = round(int(temp) / 1000)
#print(temp)
try:
#smartctl -x /dev/sda | grep "Current Temp"
smartdata_output = subprocess.check_output(["smartctl", "-A", "-j", "/dev/sda"])
smartdata = json.loads(smartdata_output)
smart_temperature = smartdata["temperature"]["current"]
except subprocess.CalledProcessError:
print ("smartctl command failed...")
monitor_dict["uptime"] = uptime
monitor_dict["load"] = load
monitor_dict["temperature"] = temp
monitor_dict["memory_usage"] = memory_usage
monitor_dict["disk_usage"] = disk_usage
monitor_dict["disk_free"] = disk_free
monitor_dict["disk_temperature"] = smart_temperature
monitor_dict["movies"] = movies_usage
monitor_dict["series"] = series_usage
monitor_dict["lxc"] = lxc_list
#print(monitor_dict)
if debug == 0:
try:
client.connect("xxx.xxx.xxx.xxx")
infot = client.publish(mqtt_topic, json.dumps(monitor_dict).encode('utf-8'), 0, True)
infot.wait_for_publish()
client.disconnect()
except:
pass
if debug == 0:
time.sleep(300)
else:
print(monitor_dict)
time.sleep(5)