added uptime
This commit is contained in:
@@ -9,15 +9,15 @@ mqtt_topic_prefix = "rpi-MQTT-monitor"
|
||||
|
||||
# Messages configuration
|
||||
|
||||
# If this is set to True the script will send just one message containing all values
|
||||
# If this is set to True the script will send just one message containing all values
|
||||
group_messages = True
|
||||
|
||||
# Random delay in seconds before measuring the values
|
||||
# Random delay in seconds before measuring the values
|
||||
# - this is used for de-synchronizing message if you run this script on many hosts, set this to 0 for no delay.
|
||||
# - 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
|
||||
@@ -25,4 +25,5 @@ used_space = True
|
||||
voltage = True
|
||||
sys_clock_speed = True
|
||||
swap = True
|
||||
memory = True
|
||||
memory = True
|
||||
uptime = True
|
||||
|
||||
@@ -23,12 +23,12 @@ def check_used_space(path):
|
||||
def check_cpu_load():
|
||||
# bash command to get cpu load from uptime command
|
||||
p = subprocess.Popen("uptime", shell=True, stdout=subprocess.PIPE).communicate()[0]
|
||||
cores = subprocess.Popen("nproc", shell=True, stdout=subprocess.PIPE).communicate()[0]
|
||||
cores = subprocess.Popen("nproc", shell=True, stdout=subprocess.PIPE).communicate()[0]
|
||||
cpu_load = p.split("average:")[1].split(",")[0].replace(' ', '')
|
||||
cpu_load = float(cpu_load)/int(cores)*100
|
||||
cpu_load = round(float(cpu_load), 1)
|
||||
return cpu_load
|
||||
|
||||
|
||||
def check_voltage():
|
||||
full_cmd = "vcgencmd measure_volts | cut -f2 -d= | sed 's/000//'"
|
||||
voltage = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
||||
@@ -45,20 +45,23 @@ def check_memory():
|
||||
full_cmd = "free -t | awk 'NR == 2 {print $3/$2*100}'"
|
||||
memory = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
||||
memory = round(float(memory), 1)
|
||||
return memory
|
||||
|
||||
return memory
|
||||
|
||||
def check_cpu_temp():
|
||||
full_cmd = "vcgencmd measure_temp"
|
||||
p = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
||||
cpu_temp = p.replace('\n', ' ').replace('\r', '').split("=")[1].split("'")[0]
|
||||
return cpu_temp
|
||||
|
||||
|
||||
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 publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sys_clock_speed = 0, swap = 0, memory = 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, uptime_days = 0):
|
||||
# connect to mqtt server
|
||||
client = paho.Client()
|
||||
client.username_pw_set(config.mqtt_user, config.mqtt_password)
|
||||
@@ -86,36 +89,37 @@ 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
|
||||
client = paho.Client()
|
||||
client.username_pw_set(config.mqtt_user, config.mqtt_password)
|
||||
client.connect(config.mqtt_host, config.mqtt_port)
|
||||
|
||||
|
||||
# publish monitored values to MQTT
|
||||
client.publish(config.mqtt_topic_prefix+"/"+hostname, values, qos=1)
|
||||
|
||||
|
||||
# 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)
|
||||
|
||||
# collect the monitored values
|
||||
|
||||
# collect the monitored values
|
||||
if config.cpu_load:
|
||||
cpu_load = check_cpu_load()
|
||||
if config.cpu_temp:
|
||||
@@ -123,16 +127,17 @@ if __name__ == '__main__':
|
||||
if config.used_space:
|
||||
used_space = check_used_space('/')
|
||||
if config.voltage:
|
||||
voltage = check_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()
|
||||
# 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