added uptime
This commit is contained in:
23
README.md
23
README.md
@@ -3,7 +3,7 @@ Python script to check the cpu load, cpu temperature, free space, used memory, s
|
||||
on a Raspberry Pi computer and publish the data to a MQTT broker.
|
||||
|
||||
I wrote this so I can monitor my raspberries at home with [home assistant](https://www.home-assistant.io/). The script was written and tested on Python 2 but it should work fine on Python 3.
|
||||
The script if very light, it takes 3 seconds as there are 5 half second sleeps in the code - due to mqtt haveing problems if I shoot the messages with no delay, this is only if you choose to send the messages separetely, now the script support a group CSV message that don't have this delay.
|
||||
The script if very light, it takes 3 seconds as there are 5 half second sleeps in the code - due to mqtt having problems if I shoot the messages with no delay, this is only if you choose to send the messages separately, now the script support a group CSV message that don't have this delay.
|
||||
|
||||
Each value measured by the script is send via a separate message for easier craetion of home assistant sensors.
|
||||
|
||||
@@ -60,23 +60,24 @@ voltage = True
|
||||
sys_clock_speed = True
|
||||
swap = False
|
||||
memory = False
|
||||
uptime = True
|
||||
```
|
||||
|
||||
If the ```group_messages``` is set to true the script will send just one message containing all values in CSV format.
|
||||
The group message looks like this:
|
||||
```
|
||||
{'used_space': '25', 'sys_clock_speed': '1500', 'cpu_temp': '43.0', 'voltage': '0.8500', 'cpu_load': '1.25', 'memory': 'False', 'swap': 'False'}
|
||||
1.3, 47.1, 12, 1.2, 600, nan, 14.1, 12
|
||||
```
|
||||
|
||||
Test the script.
|
||||
```bash
|
||||
$ /usr/bin/python /home/pi/scripts/rpi-cpu2mqtt.py
|
||||
$ /usr/bin/python /home/pi/rpi-mqtt-monitor/rpi-cpu2mqtt.py
|
||||
```
|
||||
Once you test the script there will be no output if it run OK but you should get 5 messages via the configured MQTT server (the messages count depends on your configuration).
|
||||
|
||||
Create a cron entry like this (you might need to update the path in the cron entry below, depending on where you put the script files):
|
||||
```
|
||||
*/2 * * * * /usr/bin/python /home/pi/scripts/rpi-cpu2mqtt.py
|
||||
*/2 * * * * /usr/bin/python /home/pi/rpi-mqtt-monitor/rpi-cpu2mqtt.py
|
||||
```
|
||||
# Home Assistant Integration
|
||||
|
||||
@@ -129,6 +130,12 @@ This is the sensors configuration if ```group_messages = True``` assuming your s
|
||||
value_template: '{{ value.split(",")[6] }}'
|
||||
name: rpi4 memory
|
||||
unit_of_measurement: "%"
|
||||
- platform: mqtt
|
||||
state_topic: 'masoko/rpi4'
|
||||
value_template: '{{ value.split(",")[7] }}'
|
||||
name: rpi4 uptime
|
||||
unit_of_measurement: "days"
|
||||
|
||||
```
|
||||
|
||||
This is the sensors configuration if ```group_messages = False``` assuming your sensors are separated in ```sensors.yaml``` file.
|
||||
@@ -167,6 +174,10 @@ This is the sensors configuration if ```group_messages = False``` assuming your
|
||||
state_topic: "masoko/rpi4/memory"
|
||||
name: rpi4 memory
|
||||
unit_of_measurement: "%"
|
||||
- platform: mqtt
|
||||
state_topic: "masoko/rpi4/uptime_days"
|
||||
name: rpi4 uptime
|
||||
unit_of_measurement: "days"
|
||||
```
|
||||
|
||||
Add this to your ```customize.yaml``` file to change the icons of the sensors.
|
||||
@@ -205,7 +216,7 @@ entities:
|
||||
- entity: sensor.rpi4_sys_clock_speed
|
||||
- entity: sensor.rpi4_swap
|
||||
- entity: sensor.rpi4_memory
|
||||
- entity: sensor.rpi4_uptime
|
||||
```
|
||||
# To Do
|
||||
- add uptime monitoring
|
||||
- maybe add network trafic monitoring via some third party software (for now I can't find a way to do it without additinal software)
|
||||
- maybe add network traffic monitoring via some third party software (for now I can't find a way to do it without additional software)
|
||||
|
||||
@@ -17,7 +17,7 @@ group_messages = True
|
||||
# - if you want a fix delay you can remove the randarnge function and just set the needed delay.
|
||||
random_delay = randrange(30)
|
||||
|
||||
# This is the time between sending the indivudual messages
|
||||
# This is the time between sending the individual messages
|
||||
sleep_time = 0.5
|
||||
cpu_load = True
|
||||
cpu_temp = True
|
||||
@@ -26,3 +26,4 @@ voltage = True
|
||||
sys_clock_speed = True
|
||||
swap = True
|
||||
memory = True
|
||||
uptime = True
|
||||
|
||||
@@ -57,8 +57,11 @@ def check_sys_clock_speed():
|
||||
full_cmd = "awk '{printf (\"%0.0f\",$1/1000); }' </sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"
|
||||
return subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
||||
|
||||
def check_uptime():
|
||||
full_cmd = "awk '{print int($1/3600/24)}' /proc/uptime"
|
||||
return int(subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0])
|
||||
|
||||
def publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sys_clock_speed = 0, swap = 0, memory = 0):
|
||||
def publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sys_clock_speed = 0, swap = 0, memory = 0, uptime_days = 0):
|
||||
# connect to mqtt server
|
||||
client = paho.Client()
|
||||
client.username_pw_set(config.mqtt_user, config.mqtt_password)
|
||||
@@ -86,13 +89,16 @@ def publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sy
|
||||
if config.sys_clock_speed:
|
||||
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/sys_clock_speed", sys_clock_speed, qos=1)
|
||||
time.sleep(config.sleep_time)
|
||||
if config.uptime_days:
|
||||
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/uptime_days", uptime_days, qos=1)
|
||||
time.sleep(config.sleep_time)
|
||||
# disconect from mqtt server
|
||||
client.disconnect()
|
||||
|
||||
def bulk_publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sys_clock_speed = 0, swap = 0, memory = 0):
|
||||
def bulk_publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sys_clock_speed = 0, swap = 0, memory = 0, uptime_days = 0):
|
||||
# compose the CSV message containing the measured values
|
||||
|
||||
values = cpu_load, float(cpu_temp), used_space, float(voltage), int(sys_clock_speed), swap, memory
|
||||
values = cpu_load, float(cpu_temp), used_space, float(voltage), int(sys_clock_speed), swap, memory, uptime_days
|
||||
values = str(values)[1:-1]
|
||||
|
||||
# connect to mqtt server
|
||||
@@ -106,11 +112,9 @@ def bulk_publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage =
|
||||
# disconect from mqtt server
|
||||
client.disconnect()
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# set all monitored values to False in case they are turned off in the config
|
||||
cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = False
|
||||
cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_days = False
|
||||
|
||||
# delay the execution of the script
|
||||
time.sleep(config.random_delay)
|
||||
@@ -130,9 +134,10 @@ if __name__ == '__main__':
|
||||
swap = check_swap()
|
||||
if config.memory:
|
||||
memory = check_memory()
|
||||
|
||||
if config.uptime:
|
||||
uptime_days = check_uptime()
|
||||
# Publish messages to MQTT
|
||||
if config.group_messages:
|
||||
bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory)
|
||||
bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days)
|
||||
else:
|
||||
publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory)
|
||||
publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days)
|
||||
|
||||
Reference in New Issue
Block a user