Add support for execution as service (--service,-s)
This commit is contained in:
@@ -96,13 +96,14 @@ Populate the variables for MQTT host, user, password and main topic in ```src/co
|
|||||||
|
|
||||||
You can also choose what messages are sent and what is the delay (sleep_time is only used for multiple messages) between them.
|
You can also choose what messages are sent and what is the delay (sleep_time is only used for multiple messages) between them.
|
||||||
If you are sending a grouped message, and you want to delay the execution of the script you need to use the ```random_delay``` variable which is set to 1 by default.
|
If you are sending a grouped message, and you want to delay the execution of the script you need to use the ```random_delay``` variable which is set to 1 by default.
|
||||||
This is the default configuration:
|
This is the default configuration (check the example file for more info):
|
||||||
|
|
||||||
```
|
```
|
||||||
random_delay = randrange(1)
|
random_delay = randrange(1)
|
||||||
discovery_messages = True
|
discovery_messages = True
|
||||||
group_messages = False
|
group_messages = False
|
||||||
sleep_time = 0.5
|
sleep_time = 0.5
|
||||||
|
service_sleep_time = 120
|
||||||
cpu_load = True
|
cpu_load = True
|
||||||
cpu_temp = True
|
cpu_temp = True
|
||||||
used_space = True
|
used_space = True
|
||||||
@@ -113,7 +114,6 @@ memory = True
|
|||||||
uptime = True
|
uptime = True
|
||||||
wifi_signal = False
|
wifi_signal = False
|
||||||
wifi_signal_dbm = False
|
wifi_signal_dbm = False
|
||||||
# this works only on raspbery pi version 5 with stock fan
|
|
||||||
rpi5_fan_speed = False
|
rpi5_fan_speed = False
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
14
rpi-mqtt-monitor.service
Normal file
14
rpi-mqtt-monitor.service
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=RPI MQTT Monitor
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=/home/masoko/git/rpi-mqtt-monitor/rpi_mon_env/bin/python /home/masoko/git/rpi-mqtt-monitor/src/rpi-cpu2mqtt.py --service
|
||||||
|
WorkingDirectory=/home/masoko/git/rpi-mqtt-monitor/
|
||||||
|
StandardOutput=inherit
|
||||||
|
StandardError=inherit
|
||||||
|
Restart=always
|
||||||
|
User=pi
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
@@ -30,6 +30,9 @@ qos = 0
|
|||||||
# - if you want a fixed delay you can remove the randrange function and just set the needed value.
|
# - if you want a fixed delay you can remove the randrange function and just set the needed value.
|
||||||
# random_delay = randrange(10)
|
# random_delay = randrange(10)
|
||||||
|
|
||||||
|
# this is the time between executiuons if the script is used as service (--service option)
|
||||||
|
service_sleep_time = 120
|
||||||
|
|
||||||
# This is the time between sending the individual messages
|
# This is the time between sending the individual messages
|
||||||
sleep_time = 0.1
|
sleep_time = 0.1
|
||||||
|
|
||||||
|
|||||||
@@ -360,64 +360,70 @@ def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_cl
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# parse arguments
|
# parse arguments
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('-d', action='store_true', help='Display values on screen', default=False)
|
parser.add_argument('--display', '-d', action='store_true', help='Display values on screen', default=False)
|
||||||
|
parser.add_argument('--service', '-s', action='store_true', help='Run script as a service', default=False)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
# 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 = uptime_days = wifi_signal = wifi_signal_dbm = rpi5_fan_speed = git_update = False
|
||||||
|
|
||||||
|
# delay the execution of the script
|
||||||
|
if hasattr(config, 'random_delay'): time.sleep(config.random_delay)
|
||||||
|
|
||||||
|
if hasattr(config, 'used_space_path'): used_space_path = config.used_space_path
|
||||||
|
else: used_space_path = '/'
|
||||||
|
|
||||||
|
# collect the monitored values
|
||||||
|
if config.cpu_load:
|
||||||
|
cpu_load = check_cpu_load()
|
||||||
|
if config.cpu_temp:
|
||||||
|
cpu_temp = check_cpu_temp()
|
||||||
|
if config.used_space:
|
||||||
|
used_space = check_used_space(used_space_path)
|
||||||
|
if config.voltage:
|
||||||
|
voltage = check_voltage()
|
||||||
|
if config.sys_clock_speed:
|
||||||
|
sys_clock_speed = check_sys_clock_speed()
|
||||||
|
if config.swap:
|
||||||
|
swap = check_swap()
|
||||||
|
if config.memory:
|
||||||
|
memory = check_memory()
|
||||||
|
if config.uptime:
|
||||||
|
uptime_days = check_uptime()
|
||||||
|
if config.wifi_signal:
|
||||||
|
wifi_signal = check_wifi_signal('')
|
||||||
|
if config.wifi_signal_dbm:
|
||||||
|
wifi_signal_dbm = check_wifi_signal('dbm')
|
||||||
|
if config.rpi5_fan_speed:
|
||||||
|
rpi5_fan_speed = check_rpi5_fan_speed()
|
||||||
|
if config.git_update:
|
||||||
|
git_update = check_git_update()
|
||||||
|
|
||||||
# set all monitored values to False in case they are turned off in the config
|
if args.display:
|
||||||
cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_days = wifi_signal = wifi_signal_dbm = rpi5_fan_speed = git_update = False
|
print("Hostname: " + hostname)
|
||||||
|
print("CPU Load: " + str(cpu_load))
|
||||||
|
print("CPU Temp: " + str(cpu_temp))
|
||||||
|
print("Used Space: " + str(used_space))
|
||||||
|
print("Voltage: " + str(voltage))
|
||||||
|
print("CPU Clock Speed: " + str(sys_clock_speed))
|
||||||
|
print("Swap: " + str(swap))
|
||||||
|
print("Memory: " + str(memory))
|
||||||
|
print("Uptime: " + str(uptime_days))
|
||||||
|
print("Wifi Signal: " + str(wifi_signal))
|
||||||
|
print("Wifi Signal dBm: " + str(wifi_signal_dbm))
|
||||||
|
print("RPI5 Fan Speed: " + str(rpi5_fan_speed))
|
||||||
|
print("Git Update: " + str(git_update))
|
||||||
|
print("")
|
||||||
|
|
||||||
|
# Publish messages to MQTT
|
||||||
|
if hasattr(config, 'group_messages') and config.group_messages:
|
||||||
|
bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update)
|
||||||
|
else:
|
||||||
|
publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update)
|
||||||
|
|
||||||
# delay the execution of the script
|
# if not running as a service, break the loop after one iteration
|
||||||
if hasattr(config, 'random_delay'): time.sleep(config.random_delay)
|
if not args.service:
|
||||||
|
break
|
||||||
if hasattr(config, 'used_space_path'): used_space_path = config.used_space_path
|
# if running as a service, sleep for 2 minutes before the next iteration
|
||||||
else: used_space_path = '/'
|
time.sleep(config.service_sleep_time)
|
||||||
|
|
||||||
# collect the monitored values
|
|
||||||
if config.cpu_load:
|
|
||||||
cpu_load = check_cpu_load()
|
|
||||||
if config.cpu_temp:
|
|
||||||
cpu_temp = check_cpu_temp()
|
|
||||||
if config.used_space:
|
|
||||||
used_space = check_used_space(used_space_path)
|
|
||||||
if config.voltage:
|
|
||||||
voltage = check_voltage()
|
|
||||||
if config.sys_clock_speed:
|
|
||||||
sys_clock_speed = check_sys_clock_speed()
|
|
||||||
if config.swap:
|
|
||||||
swap = check_swap()
|
|
||||||
if config.memory:
|
|
||||||
memory = check_memory()
|
|
||||||
if config.uptime:
|
|
||||||
uptime_days = check_uptime()
|
|
||||||
if config.wifi_signal:
|
|
||||||
wifi_signal = check_wifi_signal('')
|
|
||||||
if config.wifi_signal_dbm:
|
|
||||||
wifi_signal_dbm = check_wifi_signal('dbm')
|
|
||||||
if config.rpi5_fan_speed:
|
|
||||||
rpi5_fan_speed = check_rpi5_fan_speed()
|
|
||||||
if config.git_update:
|
|
||||||
git_update = check_git_update()
|
|
||||||
|
|
||||||
if args.d:
|
|
||||||
print("Hostname: " + hostname)
|
|
||||||
print("CPU Load: " + str(cpu_load))
|
|
||||||
print("CPU Temp: " + str(cpu_temp))
|
|
||||||
print("Used Space: " + str(used_space))
|
|
||||||
print("Voltage: " + str(voltage))
|
|
||||||
print("CPU Clock Speed: " + str(sys_clock_speed))
|
|
||||||
print("Swap: " + str(swap))
|
|
||||||
print("Memory: " + str(memory))
|
|
||||||
print("Uptime: " + str(uptime_days))
|
|
||||||
print("Wifi Signal: " + str(wifi_signal))
|
|
||||||
print("Wifi Signal dBm: " + str(wifi_signal_dbm))
|
|
||||||
print("RPI5 Fan Speed: " + str(rpi5_fan_speed))
|
|
||||||
print("Git Update: " + str(git_update))
|
|
||||||
|
|
||||||
|
|
||||||
# Publish messages to MQTT
|
|
||||||
if hasattr(config, 'group_messages') and config.group_messages:
|
|
||||||
bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update)
|
|
||||||
else:
|
|
||||||
publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update)
|
|
||||||
Reference in New Issue
Block a user