Last active
March 22, 2023 23:09
-
-
Save Lzok/1e6d35e6db1ebde66e1bd77901a5ccb5 to your computer and use it in GitHub Desktop.
Script to be used in my Raspberry Pi 4 cluster to show information about CPU Load, Ram Load and CPU Temperature.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
=============== DISCLAIMER =================== | |
The original library (blinkt) belongs to Pimoroni Company. | |
https://github.com/pimoroni/blinkt. | |
I just adapted some examples to meet my needs. | |
=============== END DISCLAIMER =============== | |
This script is to show the following information (in order) | |
- RAM Load | |
- CPU Load | |
- CPU Temperature | |
Colours used: | |
rgb(115, 0, 230) # RAM Load: Electric indigo | |
rgb(0, 115, 230) # CPU Load: True Blue | |
rgb(230, 115, 0) # CPU Temperture: Fulvous | |
""" | |
import time | |
import blinkt | |
from datetime import datetime, timedelta | |
from subprocess import PIPE, Popen | |
from sys import exit | |
try: | |
import psutil | |
except ImportError: | |
exit("This script requires the psutil module\nInstall with: pip3 install psutil") | |
blinkt.set_clear_on_exit() | |
blinkt.set_brightness(0.1) | |
def show_graph(v, r, g, b): | |
v *= blinkt.NUM_PIXELS | |
# I use reversed version because of the way I have the leds set (physically) | |
for x in reversed(range(blinkt.NUM_PIXELS)): | |
if v < 0: | |
r, g, b = 0, 0, 0 | |
else: | |
r, g, b = [int(min(v, 1.0) * c) for c in [r, g, b]] | |
blinkt.set_pixel(x, r, g, b) | |
v -= 1 | |
blinkt.show() | |
def get_ram_load(): | |
return psutil.virtual_memory().percent | |
def get_cpu_temperature(): | |
process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE) | |
output, _error = process.communicate() | |
output = output.decode() | |
pos_start = output.index('=') + 1 | |
pos_end = output.rindex("'") | |
temp = float(output[pos_start:pos_end]) | |
return temp | |
def show_info(resource, seconds = 5): | |
''' | |
Shows the desired information about the given resource | |
for the given time in seconds (default to five secs). | |
Parameters: | |
resource (str): Resource you want to get the information. cpu_load | cpu_temp | ram_load | |
seconds (int): Amount of seconds tu want to see the information shown by the leds. Default = 5 seconds. | |
''' | |
now = datetime.now() | |
later = now + timedelta(seconds=seconds) | |
while now < later: | |
v = fns_dispatcher[resource]() / 100.0 | |
show_graph(v, *colours[resource]) | |
time.sleep(0.01) | |
now = datetime.now() | |
colours = { | |
'cpu_load': [0, 115, 230], | |
'cpu_temp': [230, 115, 0], | |
'ram_load': [115, 0, 230] | |
} | |
fns_dispatcher = { | |
'cpu_load': psutil.cpu_percent, | |
'cpu_temp': get_cpu_temperature, | |
'ram_load': get_ram_load | |
} | |
while True: | |
show_info('ram_load') | |
show_info('cpu_load') | |
show_info('cpu_temp') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment