-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_info.py
114 lines (87 loc) · 2.75 KB
/
sys_info.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-18 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
import socket
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
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 = socket.gethostbyname(socket.gethostname())
return stat
def stats(device):
# use custom font
font_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
'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")
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), "Web 172.24.1.1:5000", font=font2, fill="white")
def main():
stats(device)
time.sleep(5)
if __name__ == "__main__":
try:
device = get_device()
main()
except KeyboardInterrupt:
pass