-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysinfo.py
executable file
·92 lines (67 loc) · 2.47 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
#!/usr/bin/env python3
"""Get system information"""
import datetime
import multiprocessing
import os
import psutil
import six
# http://stackoverflow.com/questions/12400256/python-converting-epoch-time-into-the-datetime
def boot():
"""Get the boot time"""
bootup = psutil.boot_time()
time_stamp = datetime.datetime.fromtimestamp(bootup).strftime("%Y-%m-%d %H:%M:%S")
six.print_("Boot time: %s" % time_stamp)
# http://stackoverflow.com/questions/1006289/how-to-find-out-the-number-of-cpus-using-python
def get_cores():
"""Get the number of CPU cores"""
return multiprocessing.cpu_count()
def cpus():
"""Get the number of cpus"""
six.print_("Total CPUs: %d" % get_cores())
try:
six.print_("Load averages: %f %f %f" % psutil.getloadavg())
six.print_("Physical CPUs: %d" % psutil.cpu_count(logical=False))
six.print_("Logical CPUs: %d" % psutil.cpu_count())
# Currently, we cannot get the CPU frequency on Mac with Apple Silicon.
if os.uname().sysname != "darwin" and os.uname().machine != "arm64":
six.print_("CPU current freq: %d Mhz" % psutil.cpu_freq().current)
six.print_("CPU min freq: %d Mhz" % psutil.cpu_freq().min)
six.print_("CPU max freq: %d Mhz" % psutil.cpu_freq().max)
except AttributeError:
pass
def get_memory():
"""Get the amount of memory"""
return psutil.virtual_memory().total
# https://stackoverflow.com/questions/12523586/python-format-size-application-converting-b-to-kb-mb-gb-tb/52379087
SIZE_UNITS = ["B", "KB", "MB", "GB", "TB", "PB"]
def get_readable_file_size(size_in_bytes):
"""Get humable friendly size"""
onek = 1024
index = 0
while size_in_bytes >= onek:
size_in_bytes /= onek
index += 1
try:
return f"{size_in_bytes} {SIZE_UNITS[index]}"
except IndexError:
return "File too large"
# http://stackoverflow.com/questions/22102999/get-total-physical-memory-from-python
def memory():
"""Get the amount of sytem memory"""
six.print_("Memory: %s" % get_readable_file_size(get_memory()))
def disks():
"""Get the amount of disk space"""
parts = psutil.disk_partitions()
try:
for part in parts:
six.print_(
"Mount: %s %f%% full"
% (part.mountpoint, psutil.disk_usage(part.mountpoint).percent)
)
except OSError as e:
print(e)
if __name__ == "__main__":
boot()
cpus()
memory()
disks()