From b62fa8e47c753dc1cf7457a2eecb3acc5b0e05ef Mon Sep 17 00:00:00 2001 From: Hristo Jelev Date: Tue, 16 Nov 2021 23:25:26 +0200 Subject: [PATCH] formatting --- src/rpi-cpu2mqtt.py | 378 +++++++++++++++++++++++--------------------- 1 file changed, 200 insertions(+), 178 deletions(-) diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index 6f1178a..9da9a90 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -15,207 +15,229 @@ hostname = socket.gethostname() def check_used_space(path): - st = os.statvfs(path) - free_space = st.f_bavail * st.f_frsize - total_space = st.f_blocks * st.f_frsize - used_space = int(100 - ((free_space / total_space) * 100)) - return used_space + st = os.statvfs(path) + free_space = st.f_bavail * st.f_frsize + total_space = st.f_blocks * st.f_frsize + used_space = int(100 - ((free_space / total_space) * 100)) + return used_space + 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] - cpu_load = str(p).split("average:")[1].split(", ")[0].replace(' ', '').replace(',', '.') - cpu_load = float(cpu_load)/int(cores)*100 - cpu_load = round(float(cpu_load), 1) - return 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] + cpu_load = str(p).split("average:")[1].split(", ")[0].replace(' ', '').replace(',', '.') + cpu_load = float(cpu_load) / int(cores) * 100 + cpu_load = round(float(cpu_load), 1) + return cpu_load + def check_voltage(): - try: - full_cmd = "vcgencmd measure_volts | cut -f2 -d= | sed 's/000//'" - voltage = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0] - voltage = voltage.strip()[:-1] - except: - voltage = 0 - return voltage + try: + full_cmd = "vcgencmd measure_volts | cut -f2 -d= | sed 's/000//'" + voltage = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0] + voltage = voltage.strip()[:-1] + except: + voltage = 0 + return voltage + def check_swap(): - full_cmd = "free -t | awk 'NR == 3 {print $3/$2*100}'" - swap = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0] - swap = round(float(swap.decode("utf-8").replace(",",".")), 1) - return swap + full_cmd = "free -t | awk 'NR == 3 {print $3/$2*100}'" + swap = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0] + swap = round(float(swap.decode("utf-8").replace(",", ".")), 1) + return swap + 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.decode("utf-8").replace(",","."))) - return 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.decode("utf-8").replace(",", "."))) + return memory + def check_cpu_temp(): - full_cmd = "cat /sys/class/thermal/thermal_zone*/temp 2> /dev/null | sed 's/\(.\)..$//' | tail -n 1" - try: - p = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0] - cpu_temp = p.decode("utf-8").replace('\n', ' ').replace('\r', '') - except: - cpu_temp = 0 - return cpu_temp + full_cmd = "cat /sys/class/thermal/thermal_zone*/temp 2> /dev/null | sed 's/\(.\)..$//' | tail -n 1" + try: + p = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0] + cpu_temp = p.decode("utf-8").replace('\n', ' ').replace('\r', '') + except: + cpu_temp = 0 + return cpu_temp + def check_sys_clock_speed(): - full_cmd = "awk '{printf (\"%0.0f\",$1/1000); }'