-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysinfo.py
137 lines (107 loc) · 3.82 KB
/
sysinfo.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-17 Richard Hull and contributors
# See LICENSE.rst for details.
# PYTHON_ARGCOMPLETE_OK
"""
Display basic system information.
Needs psutil (+ dependencies) installed::
$ sudo apt-get install python-dev
$ sudo -H pip install psutil
"""
import os
import sys
import time
from datetime import datetime
if os.name != 'posix':
sys.exit('{} platform not supported'.format(os.name))
from luma.core.interface.serial import i2c, spi
from luma.core.render import canvas
from luma.oled.device import ssd1306, ssd1331, ssd1351
#serial = i2c(port=1, address=0x3C)
#device = ssd1306(serial, rotate=0)
serial = spi(device=0, port=0)
device = ssd1351(serial)
from PIL import ImageFont
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():
# load average, uptime
uptime = datetime.now() - datetime.fromtimestamp(psutil.boot_time())
av1, av2, av3 = os.getloadavg()
return "Ld:%.1f %.1f %.1f Up: %s" \
% (av1, av2, av3, str(uptime).split('.')[0])
def mem_usage():
usage = psutil.virtual_memory()
return "Mem: %s %.0f%%" \
% (bytes2human(usage.used), 100 - usage.percent)
def disk_usage(dir):
usage = psutil.disk_usage(dir)
return "SD: %s %.0f%%" \
% (bytes2human(usage.used), usage.percent)
def network(iface):
stat = psutil.net_io_counters(pernic=True)[iface]
return "%s: Tx%s, Rx%s" % \
(iface, bytes2human(stat.bytes_sent), bytes2human(stat.bytes_recv))
def get_ip(iface):
for nic, addrs in psutil.net_if_addrs().items():
if nic == iface:
for addr in addrs:
if addr.family == 2:
return "%s IP: %s" % (iface, addr.address)
def stats(device):
# use custom font
# font_path = os.path.abspath(os.path.join(os.path.dirname(__file__),'..',
# 'luma.examples','examples','fonts', 'C&C Red Alert [INET].ttf'))
# font2 = ImageFont.truetype(font_path, 12)
# font_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
# 'fonts/mareib_reg/MARKEN__.TTF'))
# font2 = ImageFont.truetype(font_path, 8)
# font_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
# 'fonts/everyday/Everyday.ttf'))
# font2 = ImageFont.truetype(font_path, 10)
font_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
'fonts/mun_ext/MUNIE___.TTF'))
font2 = ImageFont.truetype(font_path, 8)
with canvas(device) as draw:
draw.text((0, 0), cpu_usage(), font=font2, fill="white")
if device.height >= 32:
draw.text((0, 14), mem_usage(), font=font2, fill="white")
if device.height >= 64:
draw.text((0, 26), disk_usage('/'), font=font2, fill="white")
try:
draw.text((0, 38), network('wlan0'), font=font2, fill="white")
except KeyError:
# no wifi enabled/available
pass
draw.text((0, 50), get_ip('wlan0'), font=font2, fill="white")
def main():
while True:
stats(device)
time.sleep(5)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass