-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsys_stats.py
116 lines (90 loc) · 2.98 KB
/
sys_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
#Created by Roxas
#create 06-agosto-2021
#https://github.com/TuryRx/Banana-pi-m2-zero-GPIO
import os
import sys
import time
from pathlib import Path
from datetime import datetime
if os.name != 'posix':
sys.exit('{} platform not supported'.format(os.name))
from demo_opts import get_device
from luma.core.render import canvas
from PIL import ImageFont
import subprocess
try:
import psutil
except ImportError:
print("The psutil library was not found. Run 'sudo -H pip install psutil' to install it.")
sys.exit()
# TODO: custom font bitmaps for up/down arrows
# TODO: Load histogram
def bytes2human(n):
"""
>>> bytes2human(10000)
'9K'
>>> bytes2human(100001221)
'95M'
"""
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
prefix = {}
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
for s in reversed(symbols):
if n >= prefix[s]:
value = int(float(n) / prefix[s])
return '%s%s' % (value, s)
return "%sB" % n
def cpu_usage():
av1, av2, av3 = os.getloadavg()
return "CPU Load: %.2f %.2f %.2f" \
% (av1, av2, av3)
def cpu_up():
uptime = datetime.now() - datetime.fromtimestamp(psutil.boot_time())
return "TIME UP: %s" % \
(str(uptime).split('.')[0])
def mem_usage():
cmd = "free -m | awk 'NR==2{printf " \
"\"RAM: %s/%sMB %.1f%%\", $3,$2,$3*100/$2 }'"
return "%s" \
% (subprocess.check_output(cmd, shell=True).decode("utf-8"))
def disk_usage():
cmd = "df -h | awk '$NF==\"/\"{printf " \
"\"HDD: %.1f/%.0fGB %s\", $3,$2,$5}'"
return "%s" \
% (subprocess.check_output(cmd, shell=True).decode("utf-8"))
def network_ip():
cmd = "hostname -I | cut -d\' \' -f1 | tr -d \'\\n\'"
return "IP: %s" \
% (subprocess.check_output(cmd, shell=True).decode("utf-8"))
def network_send():
stat = psutil.net_io_counters(pernic=True)['wlan0']
return "%s R:%s T:%s" % \
('',bytes2human(stat.bytes_recv), bytes2human(stat.bytes_sent))
def stats(device):
# use custom font
font_path = str(Path(__file__).resolve().parent.joinpath('fonts', 'C&C Red Alert [INET].ttf'))
font2 = ImageFont.truetype(font_path, 12)
with canvas(device) as draw:
draw.text((0, 0), cpu_usage(), font=font2, fill="white")
draw.text((0, 12), cpu_up(), font=font2, fill="white")
draw.text((0, 24), mem_usage(), font=font2, fill="white")
draw.text((0, 36), disk_usage(), font=font2, fill="white")
try:
draw.text((0, 48), network_ip(), font=font2, fill="white")
#To view Tx and Rx just uncomment
#draw.text((66, 48), network_send(), font=font2, fill="white")
except KeyError:
# no wifi enabled/available
pass
def main():
while True:
stats(device)
time.sleep(5)
if __name__ == "__main__":
try:
device = get_device()
main()
except KeyboardInterrupt:
pass