-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathconsole.py
117 lines (102 loc) · 3.89 KB
/
console.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
import subprocess
import platform
from os import linesep
from time import sleep
from pyspectator.convert import UnitByte
CLEAR_CMD = 'cls' if platform.system() == 'Windows' else 'clear'
def clear():
subprocess.call(CLEAR_CMD, shell=True)
def print_hr(space_before=False, space_after=False):
before = linesep if space_before else ''
after = linesep if space_after else ''
print(before + '-' * 80 + after)
class Format(object):
@staticmethod
def temperature(value):
formatted_value = ''
if isinstance(value, (int, float)):
formatted_value = str(value) + '°C'
return formatted_value
@staticmethod
def byte_value(value):
formatted_value = ''
if isinstance(value, (int, float)):
value, unit = UnitByte.auto_convert(value)
value = '{:.2f}'.format(value)
unit = UnitByte.get_name_reduction(unit)
formatted_value = value + unit
return formatted_value
@staticmethod
def percent(value):
formatted_value = ''
if isinstance(value, (int, float)):
formatted_value = str(value) + '%'
return formatted_value
def main(computer):
print('Start monitoring system...')
print_hr(space_after=True)
# Show system info for ~16 seconds
for _ in range(16):
clear()
# Display general information about computer
print('OS: ' + str(computer.os))
print('Boot time: {}; Uptime: {}'.format(
computer.boot_time, computer.uptime
))
print('')
# Display CPU info
print('CPU name: ' + str(computer.processor.name))
print('Amount of CPU cores: ' + str(computer.processor.count))
print('CPU load: ' + Format.percent(computer.processor.load))
cpu_temperature = 'unknown'
if computer.processor.temperature is not None:
cpu_temperature = Format.temperature(
computer.processor.temperature
)
print('CPU temperature: ' + cpu_temperature)
print('')
# Display network info
print('Hostname: ' + str(computer.hostname))
print('Network interface: ' + str(computer.network_interface.name))
print('MAC: ' + str(computer.network_interface.hardware_address))
print('IP: {}; Mask: {}; Gateway: {}'.format(
computer.network_interface.ip_address,
computer.network_interface.subnet_mask,
computer.network_interface.default_route
))
print('Sent data: {}; Received data: {}'.format(
Format.byte_value(computer.network_interface.bytes_sent),
Format.byte_value(computer.network_interface.bytes_recv)
))
print('')
# Display virtual memory info
print('Virtual memory: use {} from {}, {}'.format(
Format.byte_value(computer.virtual_memory.available),
Format.byte_value(computer.virtual_memory.total),
Format.percent(computer.virtual_memory.used_percent)
))
print('')
# Display nonvolatile memory info
output_format1 = '{:_^16}{:_^16}{:_^16}{:_^16}{:_^16}'
output_format2 = '{: ^16}{: ^16}{: ^16}{: ^16}{: ^16}'
print(output_format1.format('Device', 'Total', 'Use', 'Type', 'Mount'))
for dev in computer.nonvolatile_memory:
output_text = output_format2.format(
dev.device,
Format.byte_value(dev.total),
Format.percent(dev.used_percent),
dev.fstype,
dev.mountpoint
)
print(output_text)
sleep(1)
print_hr(space_before=True)
print('Shutdown monitoring system...')
if __name__ == '__main__':
# Initialize computer instance
from pyspectator.computer import Computer
COMPUTER = Computer()
# Start monitoring system
with COMPUTER:
# Start console interface
main(COMPUTER)