forked from alaub81/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystemstats-mqtt.py
executable file
·82 lines (69 loc) · 2.42 KB
/
systemstats-mqtt.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
#!/usr/bin/python3
from gpiozero import CPUTemperature
import psutil
import datetime
import paho.mqtt.client as mqtt
import ssl
# set the variables
broker = "FQDN / IP ADDRESS"
port = 8883
publish_topic = "home/DEVICENAME"
clientid = "DEVICENAME-systemstats"
username = "mosquitto"
password = "PASSWORD"
insecure = True
qos = 1
retain_message = True
# do the stuff
uptime = datetime.datetime.now() - datetime.datetime.fromtimestamp(psutil.boot_time())
uptime_min = (uptime.seconds+uptime.days*24*3600)/60
cpu_temp = CPUTemperature()
cpu_usage = psutil.cpu_percent(interval=1, percpu=False)
load_1, load_5, load_15 = psutil.getloadavg()
ram = psutil.virtual_memory()
ram_total = ram.total / 2**20 # MiB.
ram_used = ram.used / 2**20
ram_free = ram.free / 2**20
ram_percent_used = ram.percent
disk = psutil.disk_usage('/')
disk_total = disk.total / 2**30 # GiB.
disk_used = disk.used / 2**30
disk_free = disk.free / 2**30
disk_percent_used = disk.percent
def publish(topic, payload):
client.publish(publish_topic + "/" + topic,payload,qos,retain_message)
#MQTT Connection
client=mqtt.Client(clientid)
client.username_pw_set(username, password)
client.tls_set(cert_reqs=ssl.CERT_NONE) #no client certificate needed
client.tls_insecure_set(insecure)
client.connect(broker, port)
client.loop_start()
#Publish the stuff
publish("uptime", str(uptime))
publish("uptime_min", (uptime_min))
publish("cpu_temp", cpu_temp.temperature)
publish("cpu_usage", cpu_usage)
publish("load_1", load_1)
publish("load_5", load_5)
publish("load_15", load_15)
publish("ram_total", ram_total)
publish("ram_used", ram_used)
publish("ram_free", ram_free)
publish("ram_percent_used", ram_percent_used)
publish("disk_total", disk_total)
publish("disk_used", disk_used)
publish("disk_free", disk_free)
publish("disk_percent_used", disk_percent_used)
#MQTT Disconnect
client.disconnect()
client.loop_stop()
"""
print("---System Informations---")
print("System Uptime: %s - %.0f min" % (uptime, uptime_min))
print("CPU Temperatur: {:.1f}".format(cpu_temp.temperature) + " °C")
print("CPU Usage: {:.0f} %".format(cpu_usage))
print("Load average: %.2f %.2f %.2f" % (load_1, load_5, load_15))
print("Ram Total: %.1f MiB\nRam Used: %.1f MiB\nRam Free: %.1f MiB\nRam Usage: %.1f %%" % (ram_total, ram_used, ram_free, ram_percent_used))
print("Disk Total: %.1f GiB\nDisk Used: %.1f GiB\nDisk Free: %.1f GiB\nDisk Usage: %.1f %%" % (disk_total, disk_used, disk_free, disk_percent_used))
"""