Last active
March 11, 2022 03:36
-
-
Save Jamp/dc61ac1d2daa0452ff05fa1251c67900 to your computer and use it in GitHub Desktop.
Show Stats Raspberry PI with Python
This file contains hidden or 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
#!/bin/bash | |
sudo apt update && sudo apt upgrade -y | |
sudo apt install -y build-essential python3-dev python-smbus i2c-tools python3-pil python3-pip python3-setuptools python3-rpi.gpio git | |
pip install psutil netifaces requests |
This file contains hidden or 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
#!/bin/python3 | |
from os import cpu_count | |
from psutil import getloadavg, virtual_memory | |
from requests import get | |
from netifaces import interfaces, ifaddresses, AF_INET | |
from shutil import disk_usage | |
disks = [ | |
'/' | |
] | |
def hardware_stats(): | |
stats = [] | |
_, _, load15 = getloadavg() | |
cpu_usage = (load15 / cpu_count()) * 100 | |
vm = virtual_memory() | |
stats.append(['CPU', '{:0.2f}%'.format(cpu_usage)]) | |
stats.append(['RAM', '{:0.2f}%'.format(vm[2])]) | |
return stats | |
def disk_stats(mounted_point): | |
disks = [] | |
for mount in mounted_point: | |
total, used, free = disk_usage(mount) | |
disks.append([mount, [ | |
['Total', '{:0.2f}%'.format(total/(2**30))], | |
['Used', '{:0.2f}%'.format(used/(2**30))], | |
['Free', '{:0.2f}%'.format(free/(2**30))] | |
]]) | |
return disks | |
def ips(): | |
ips = [] | |
for ifaceName in interfaces(): | |
if ifaceName != 'lo': | |
addresses = [i['addr'] for i in ifaddresses(ifaceName).setdefault(AF_INET, [{'addr':'No IP addr'}] )] | |
ips.append([ifaceName, ', '.join(addresses)]) | |
internet = get('https://api.ipify.org/') | |
ips.append(['Internet', internet.text]) | |
return ips | |
s = hardware_stats() | |
d = disk_stats(disks) | |
i = ips() | |
for part, stat in s: | |
print('%s: %s' % (part, stat), end='\t') | |
print('') | |
for disk, stats in d: | |
print('%s' % disk, end=' ') | |
for mode, stat in stats: | |
print('%s: %s GiB' % (mode, stat), end=' ') | |
print('') | |
for inet, ip in i: | |
print('%s: %s' % (inet, ip), end='\t') | |
print('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment