From 033a000797bd7d64392c49c7b278a3b887abcdf0 Mon Sep 17 00:00:00 2001 From: Christopher Fenner <9592452+CFenner@users.noreply.github.com> Date: Sun, 28 Jan 2024 22:47:42 +0100 Subject: [PATCH 1/4] Create .editorconfig --- .editorconfig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..f5a3e99 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +# EditorConfig is awesome: http://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +charset = utf-8 +end_of_line = lf +indent_style = space +indent_size = 4 +insert_final_newline = true +trim_trailing_whitespace = true +[LICENSE] +indent_style = none +indent_size = none From bbfbe9a4d8694c3211ff3f6f8535928966116ea3 Mon Sep 17 00:00:00 2001 From: Christopher Fenner <9592452+CFenner@users.noreply.github.com> Date: Sun, 28 Jan 2024 22:48:09 +0100 Subject: [PATCH 2/4] Update rpi-cpu2mqtt.py --- src/rpi-cpu2mqtt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index f9903f6..5900943 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -10,7 +10,7 @@ import time import socket import paho.mqtt.client as paho import json -import config +import config import os import argparse import update @@ -503,4 +503,4 @@ if __name__ == '__main__': if not args.service: break # if running as a service, sleep before the next iteration - time.sleep(config.service_sleep_time) \ No newline at end of file + time.sleep(config.service_sleep_time) From 772d84b2d6775b15452c6c23f101d7891327feb0 Mon Sep 17 00:00:00 2001 From: Christopher Fenner <9592452+CFenner@users.noreply.github.com> Date: Sun, 28 Jan 2024 22:50:37 +0100 Subject: [PATCH 3/4] Update rpi-cpu2mqtt.py --- src/rpi-cpu2mqtt.py | 506 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 506 insertions(+) diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index 5900943..4bcb7bb 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -4,7 +4,513 @@ # RUN sudo apt-get install python-pip # RUN pip install paho-mqtt +from __future__ import division# -*- coding: utf-8 -*- +# Python script (runs on 2 and 3) to monitor cpu load, temperature, frequency, free space etc. +# on a Raspberry Pi or Ubuntu computer and publish the data to a MQTT server. +# RUN sudo apt-get install python-pip +# RUN pip install paho-mqtt + from __future__ import division +import subprocess +import time +import socket +import paho.mqtt.client as paho +import json +import config +import os +import argparse +import update + +# get device host name - used in mqtt topic +hostname = socket.gethostname() + +def check_wifi_signal(format): + try: + full_cmd = "ls /sys/class/ieee80211/*/device/net/" + interface = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0].strip().decode("utf-8") + full_cmd = "/sbin/iwconfig {} | grep -i quality".format(interface) + wifi_signal = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0] + + if format == 'dbm': + wifi_signal = wifi_signal.decode("utf-8").strip().split(' ')[4].split('=')[1] + else: + wifi_signal = wifi_signal.decode("utf-8").strip().split(' ')[1].split('=')[1].split('/')[0] + wifi_signal = round((int(wifi_signal) / 70)* 100) + + except Exception: + wifi_signal = 0 + + return wifi_signal + + +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 + + +def check_cpu_load(): + 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, stderr=subprocess.PIPE).communicate()[0] + voltage = voltage.strip()[:-1] + except Exception: + voltage = 0 + + return voltage.decode('utf8') + + +def check_swap(): + full_cmd = "free -t |grep -i swap | awk 'NR == 1 {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 + + +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").strip() + except Exception: + cpu_temp = 0 + + return cpu_temp + + +def check_sys_clock_speed(): + full_cmd = "awk '{printf (\"%0.0f\",$1/1000); }' Date: Sun, 28 Jan 2024 22:51:05 +0100 Subject: [PATCH 4/4] Update rpi-cpu2mqtt.py --- src/rpi-cpu2mqtt.py | 506 -------------------------------------------- 1 file changed, 506 deletions(-) diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index 4bcb7bb..5bc98ef 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -4,12 +4,6 @@ # RUN sudo apt-get install python-pip # RUN pip install paho-mqtt -from __future__ import division# -*- coding: utf-8 -*- -# Python script (runs on 2 and 3) to monitor cpu load, temperature, frequency, free space etc. -# on a Raspberry Pi or Ubuntu computer and publish the data to a MQTT server. -# RUN sudo apt-get install python-pip -# RUN pip install paho-mqtt - from __future__ import division import subprocess import time @@ -510,503 +504,3 @@ if __name__ == '__main__': break # if running as a service, sleep before the next iteration time.sleep(config.service_sleep_time) - -import subprocess -import time -import socket -import paho.mqtt.client as paho -import json -import config -import os -import argparse -import update - -# get device host name - used in mqtt topic -hostname = socket.gethostname() - -def check_wifi_signal(format): - try: - full_cmd = "ls /sys/class/ieee80211/*/device/net/" - interface = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0].strip().decode("utf-8") - full_cmd = "/sbin/iwconfig {} | grep -i quality".format(interface) - wifi_signal = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0] - - if format == 'dbm': - wifi_signal = wifi_signal.decode("utf-8").strip().split(' ')[4].split('=')[1] - else: - wifi_signal = wifi_signal.decode("utf-8").strip().split(' ')[1].split('=')[1].split('/')[0] - wifi_signal = round((int(wifi_signal) / 70)* 100) - - except Exception: - wifi_signal = 0 - - return wifi_signal - - -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 - - -def check_cpu_load(): - 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, stderr=subprocess.PIPE).communicate()[0] - voltage = voltage.strip()[:-1] - except Exception: - voltage = 0 - - return voltage.decode('utf8') - - -def check_swap(): - full_cmd = "free -t |grep -i swap | awk 'NR == 1 {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 - - -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").strip() - except Exception: - cpu_temp = 0 - - return cpu_temp - - -def check_sys_clock_speed(): - full_cmd = "awk '{printf (\"%0.0f\",$1/1000); }'