formatting
This commit is contained in:
@@ -21,15 +21,17 @@ def check_used_space(path):
|
|||||||
used_space = int(100 - ((free_space / total_space) * 100))
|
used_space = int(100 - ((free_space / total_space) * 100))
|
||||||
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 = str(p).split("average:")[1].split(", ")[0].replace(' ', '').replace(',', '.')
|
cpu_load = str(p).split("average:")[1].split(", ")[0].replace(' ', '').replace(',', '.')
|
||||||
cpu_load = float(cpu_load)/int(cores)*100
|
cpu_load = float(cpu_load) / int(cores) * 100
|
||||||
cpu_load = round(float(cpu_load), 1)
|
cpu_load = round(float(cpu_load), 1)
|
||||||
return cpu_load
|
return cpu_load
|
||||||
|
|
||||||
|
|
||||||
def check_voltage():
|
def check_voltage():
|
||||||
try:
|
try:
|
||||||
full_cmd = "vcgencmd measure_volts | cut -f2 -d= | sed 's/000//'"
|
full_cmd = "vcgencmd measure_volts | cut -f2 -d= | sed 's/000//'"
|
||||||
@@ -39,18 +41,21 @@ def check_voltage():
|
|||||||
voltage = 0
|
voltage = 0
|
||||||
return voltage
|
return voltage
|
||||||
|
|
||||||
|
|
||||||
def check_swap():
|
def check_swap():
|
||||||
full_cmd = "free -t | awk 'NR == 3 {print $3/$2*100}'"
|
full_cmd = "free -t | awk 'NR == 3 {print $3/$2*100}'"
|
||||||
swap = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
swap = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
||||||
swap = round(float(swap.decode("utf-8").replace(",",".")), 1)
|
swap = round(float(swap.decode("utf-8").replace(",", ".")), 1)
|
||||||
return swap
|
return swap
|
||||||
|
|
||||||
|
|
||||||
def check_memory():
|
def check_memory():
|
||||||
full_cmd = "free -t | awk 'NR == 2 {print $3/$2*100}'"
|
full_cmd = "free -t | awk 'NR == 2 {print $3/$2*100}'"
|
||||||
memory = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
memory = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
||||||
memory = round(float(memory.decode("utf-8").replace(",",".")))
|
memory = round(float(memory.decode("utf-8").replace(",", ".")))
|
||||||
return memory
|
return memory
|
||||||
|
|
||||||
|
|
||||||
def check_cpu_temp():
|
def check_cpu_temp():
|
||||||
full_cmd = "cat /sys/class/thermal/thermal_zone*/temp 2> /dev/null | sed 's/\(.\)..$//' | tail -n 1"
|
full_cmd = "cat /sys/class/thermal/thermal_zone*/temp 2> /dev/null | sed 's/\(.\)..$//' | tail -n 1"
|
||||||
try:
|
try:
|
||||||
@@ -60,14 +65,17 @@ def check_cpu_temp():
|
|||||||
cpu_temp = 0
|
cpu_temp = 0
|
||||||
return cpu_temp
|
return cpu_temp
|
||||||
|
|
||||||
|
|
||||||
def check_sys_clock_speed():
|
def check_sys_clock_speed():
|
||||||
full_cmd = "awk '{printf (\"%0.0f\",$1/1000); }' </sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"
|
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]
|
return subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
||||||
|
|
||||||
|
|
||||||
def check_uptime():
|
def check_uptime():
|
||||||
full_cmd = "awk '{print int($1/3600/24)}' /proc/uptime"
|
full_cmd = "awk '{print int($1/3600/24)}' /proc/uptime"
|
||||||
return int(subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0])
|
return int(subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0])
|
||||||
|
|
||||||
|
|
||||||
def config_json(what_config):
|
def config_json(what_config):
|
||||||
data = {
|
data = {
|
||||||
"state_topic": "",
|
"state_topic": "",
|
||||||
@@ -76,8 +84,8 @@ def config_json(what_config):
|
|||||||
"unique_id": "",
|
"unique_id": "",
|
||||||
"unit_of_measurement": "",
|
"unit_of_measurement": "",
|
||||||
}
|
}
|
||||||
data["state_topic"] = config.mqtt_topic_prefix+"/"+hostname+"/"+what_config
|
data["state_topic"] = config.mqtt_topic_prefix + "/" + hostname + "/" + what_config
|
||||||
data["unique_id"] = hostname+"_"+what_config
|
data["unique_id"] = hostname + "_" + what_config
|
||||||
if what_config == "cpuload":
|
if what_config == "cpuload":
|
||||||
data["icon"] = "mdi:speedometer"
|
data["icon"] = "mdi:speedometer"
|
||||||
data["name"] = hostname + " CPU Usage"
|
data["name"] = hostname + " CPU Usage"
|
||||||
@@ -115,7 +123,9 @@ def config_json(what_config):
|
|||||||
# Return our built discovery config
|
# Return our built discovery config
|
||||||
return json.dumps(data)
|
return json.dumps(data)
|
||||||
|
|
||||||
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):
|
|
||||||
|
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
|
# connect to mqtt server
|
||||||
client = paho.Client()
|
client = paho.Client()
|
||||||
client.username_pw_set(config.mqtt_user, config.mqtt_password)
|
client.username_pw_set(config.mqtt_user, config.mqtt_password)
|
||||||
@@ -124,56 +134,67 @@ def publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sy
|
|||||||
# publish monitored values to MQTT
|
# publish monitored values to MQTT
|
||||||
if config.cpu_load:
|
if config.cpu_load:
|
||||||
if config.discovery_messages:
|
if config.discovery_messages:
|
||||||
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_cpuload/config", config_json('cpuload'), qos=0)
|
client.publish("homeassistant/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_cpuload/config",
|
||||||
|
config_json('cpuload'), qos=0)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/cpuload", cpu_load, qos=1)
|
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/cpuload", cpu_load, qos=1)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
if config.cpu_temp:
|
if config.cpu_temp:
|
||||||
if config.discovery_messages:
|
if config.discovery_messages:
|
||||||
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_cputemp/config", config_json('cputemp'), qos=0)
|
client.publish("homeassistant/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_cputemp/config",
|
||||||
|
config_json('cputemp'), qos=0)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/cputemp", cpu_temp, qos=1)
|
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/cputemp", cpu_temp, qos=1)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
if config.used_space:
|
if config.used_space:
|
||||||
if config.discovery_messages:
|
if config.discovery_messages:
|
||||||
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_diskusage/config", config_json('diskusage'), qos=0)
|
client.publish("homeassistant/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_diskusage/config",
|
||||||
|
config_json('diskusage'), qos=0)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/diskusage", used_space, qos=1)
|
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/diskusage", used_space, qos=1)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
if config.voltage:
|
if config.voltage:
|
||||||
if config.discovery_messages:
|
if config.discovery_messages:
|
||||||
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_voltage/config", config_json('voltage'), qos=0)
|
client.publish("homeassistant/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_voltage/config",
|
||||||
|
config_json('voltage'), qos=0)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/voltage", voltage, qos=1)
|
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/voltage", voltage, qos=1)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
if config.swap:
|
if config.swap:
|
||||||
if config.discovery_messages:
|
if config.discovery_messages:
|
||||||
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_swap/config", config_json('swap'), qos=0)
|
client.publish("homeassistant/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_swap/config",
|
||||||
|
config_json('swap'), qos=0)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/swap", swap, qos=1)
|
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/swap", swap, qos=1)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
if config.memory:
|
if config.memory:
|
||||||
if config.discovery_messages:
|
if config.discovery_messages:
|
||||||
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_memory/config", config_json('memory'), qos=0)
|
client.publish("homeassistant/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_memory/config",
|
||||||
|
config_json('memory'), qos=0)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/memory", memory, qos=1)
|
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/memory", memory, qos=1)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
if config.sys_clock_speed:
|
if config.sys_clock_speed:
|
||||||
if config.discovery_messages:
|
if config.discovery_messages:
|
||||||
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_sys_clock_speed/config", config_json('sys_clock_speed'), qos=0)
|
client.publish(
|
||||||
|
"homeassistant/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_sys_clock_speed/config",
|
||||||
|
config_json('sys_clock_speed'), qos=0)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/sys_clock_speed", sys_clock_speed, qos=1)
|
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/sys_clock_speed", sys_clock_speed, qos=1)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
if config.uptime:
|
if config.uptime:
|
||||||
if config.discovery_messages:
|
if config.discovery_messages:
|
||||||
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_uptime_days/config", config_json('uptime_days'), qos=0)
|
client.publish("homeassistant/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_uptime_days/config",
|
||||||
|
config_json('uptime_days'), qos=0)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/uptime_days", uptime_days, qos=1)
|
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/uptime_days", uptime_days, qos=1)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
# disconect from mqtt server
|
# disconect from mqtt server
|
||||||
client.disconnect()
|
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, uptime_days = 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
|
# compose the CSV message containing the measured values
|
||||||
|
|
||||||
values = cpu_load, float(cpu_temp), used_space, float(voltage), int(sys_clock_speed), swap, memory, uptime_days
|
values = cpu_load, float(cpu_temp), used_space, float(voltage), int(sys_clock_speed), swap, memory, uptime_days
|
||||||
@@ -185,11 +206,12 @@ def bulk_publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage =
|
|||||||
client.connect(config.mqtt_host, int(config.mqtt_port))
|
client.connect(config.mqtt_host, int(config.mqtt_port))
|
||||||
|
|
||||||
# publish monitored values to MQTT
|
# publish monitored values to MQTT
|
||||||
client.publish(config.mqtt_topic_prefix+"/"+hostname, values, qos=1)
|
client.publish(config.mqtt_topic_prefix + "/" + hostname, values, qos=1)
|
||||||
|
|
||||||
# disconect from mqtt server
|
# disconect from mqtt server
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# set all monitored values to False in case they are turned off in the config
|
# 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 = False
|
cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_days = False
|
||||||
|
|||||||
Reference in New Issue
Block a user