added option to turn off messages from config
This commit is contained in:
@@ -8,10 +8,10 @@ import subprocess, time, socket, os
|
|||||||
import paho.mqtt.client as paho
|
import paho.mqtt.client as paho
|
||||||
import config
|
import config
|
||||||
|
|
||||||
#get device host name - used in mqtt topic
|
# get device host name - used in mqtt topic
|
||||||
hostname = socket.gethostname()
|
hostname = socket.gethostname()
|
||||||
|
|
||||||
#mqtt server configuration
|
# mqtt server configuration
|
||||||
mqtt_host = config.mqtt_host
|
mqtt_host = config.mqtt_host
|
||||||
mqtt_user = config.mqtt_user
|
mqtt_user = config.mqtt_user
|
||||||
mqtt_password = config.mqtt_password
|
mqtt_password = config.mqtt_password
|
||||||
@@ -26,7 +26,7 @@ def check_used_space(path):
|
|||||||
return used_space
|
return used_space
|
||||||
|
|
||||||
def check_cpu_load():
|
def check_cpu_load():
|
||||||
#bash command to get cpu load from uptime command
|
# bash command to get cpu load from uptime command
|
||||||
p = subprocess.Popen("uptime", shell=True, stdout=subprocess.PIPE).communicate()[0]
|
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 = p.split("average:")[1].split(",")[0].replace(' ', '')
|
||||||
@@ -62,39 +62,56 @@ def check_sys_clock_speed():
|
|||||||
return subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
return subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
||||||
|
|
||||||
|
|
||||||
def publish_to_mqtt (cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory):
|
def publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sys_clock_speed = 0, swap = 0, memory = 0):
|
||||||
#connect to mqtt server
|
# connect to mqtt server
|
||||||
client = paho.Client()
|
client = paho.Client()
|
||||||
client.username_pw_set(mqtt_user, mqtt_password)
|
client.username_pw_set(mqtt_user, mqtt_password)
|
||||||
client.connect(mqtt_host, mqtt_port)
|
client.connect(mqtt_host, mqtt_port)
|
||||||
|
|
||||||
#publish monitored values to MQTT
|
# publish monitored values to MQTT
|
||||||
client.publish(mqtt_topic_prefix+"/"+hostname+"/cpuload", cpu_load, qos=1)
|
if config.cpu_load:
|
||||||
time.sleep(1)
|
client.publish(mqtt_topic_prefix+"/"+hostname+"/cpuload", cpu_load, qos=1)
|
||||||
client.publish(mqtt_topic_prefix+"/"+hostname+"/cputemp", cpu_temp, qos=1)
|
time.sleep(config.sleep_time)
|
||||||
time.sleep(1)
|
if config.cpu_temp:
|
||||||
client.publish(mqtt_topic_prefix+"/"+hostname+"/diskusage", used_space, qos=1)
|
client.publish(mqtt_topic_prefix+"/"+hostname+"/cputemp", cpu_temp, qos=1)
|
||||||
time.sleep(1)
|
time.sleep(config.sleep_time)
|
||||||
client.publish(mqtt_topic_prefix+"/"+hostname+"/voltage", voltage, qos=1)
|
if config.used_space:
|
||||||
time.sleep(1)
|
client.publish(mqtt_topic_prefix+"/"+hostname+"/diskusage", used_space, qos=1)
|
||||||
client.publish(mqtt_topic_prefix+"/"+hostname+"/swap", swap, qos=1)
|
time.sleep(config.sleep_time)
|
||||||
time.sleep(1)
|
if config.voltage:
|
||||||
client.publish(mqtt_topic_prefix+"/"+hostname+"/memory", memory, qos=1)
|
client.publish(mqtt_topic_prefix+"/"+hostname+"/voltage", voltage, qos=1)
|
||||||
time.sleep(1)
|
time.sleep(config.sleep_time)
|
||||||
client.publish(mqtt_topic_prefix+"/"+hostname+"/sys_clock_speed", sys_clock_speed, qos=1)
|
if config.swap:
|
||||||
time.sleep(1)
|
client.publish(mqtt_topic_prefix+"/"+hostname+"/swap", swap, qos=1)
|
||||||
#disconect from mqtt server
|
time.sleep(config.sleep_time)
|
||||||
|
if config.memory:
|
||||||
|
client.publish(mqtt_topic_prefix+"/"+hostname+"/memory", memory, qos=1)
|
||||||
|
time.sleep(config.sleep_time)
|
||||||
|
if config.sys_clock_speed:
|
||||||
|
client.publish(mqtt_topic_prefix+"/"+hostname+"/sys_clock_speed", sys_clock_speed, qos=1)
|
||||||
|
time.sleep(config.sleep_time)
|
||||||
|
# disconect from mqtt server
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
#collect the monitored values
|
# set all monitored values to False in case they are turned off in the config
|
||||||
cpu_load = check_cpu_load()
|
cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = False
|
||||||
cpu_temp = check_cpu_temp()
|
|
||||||
used_space = check_used_space('/')
|
# collect the monitored values
|
||||||
voltage = check_voltage()
|
if config.cpu_load:
|
||||||
sys_clock_speed = check_sys_clock_speed()
|
cpu_load = check_cpu_load()
|
||||||
swap = check_swap()
|
if config.cpu_temp:
|
||||||
memory = check_memory()
|
cpu_temp = check_cpu_temp()
|
||||||
|
if config.used_space:
|
||||||
|
used_space = check_used_space('/')
|
||||||
|
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()
|
||||||
|
|
||||||
#Publish messages to MQTT
|
# Publish messages to MQTT
|
||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user