-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlatformInfo.py
206 lines (174 loc) · 5.4 KB
/
PlatformInfo.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import platform
import threading
import time
import psutil
def obj2dict(f):
return dict(f._asdict())
class CPU(object):
@staticmethod
def get_info():
cpu_info = dict()
if platform.system() == "Linux":
cpu_info["name"] = platform.machine()
'''
command = "cat /proc/cpuinfo"
all_info = subprocess.check_output(command, shell=True).strip()
for line in all_info.split("\n"):
if "model name" in line:
cpu_info["name"]= re.sub(".*model name.*:", "", line, 1)
break
'''
else:
cpu_info["name"] = platform.processor()
cpu_info["physicalCore"] = psutil.cpu_count(False)
cpu_info["logicalCore"] = psutil.cpu_count()
if not cpu_info["physicalCore"]:
cpu_info["physicalCore"] = cpu_info["logicalCore"]
return cpu_info
@staticmethod
def get_status():
t0 = time.time()
ret = dict()
def percent(ret):
ret["cpupercent"] = psutil.cpu_percent(interval=0.49, percpu=True)
def times(ret):
cputimes = psutil.cpu_times_percent(interval=0.49, percpu=True)
ret["cputimes"] = []
for i in cputimes:
ret["cputimes"].append(obj2dict(i))
threads = []
threads.append(threading.Thread(target=percent, daemon=True, args=(ret,)))
threads.append(threading.Thread(target=times, daemon=True, args=(ret,)))
for i in threads:
i.start()
i.join()
ret["status"] = obj2dict(psutil.cpu_stats())
return ret
def get_platform_info():
ret = obj2dict(platform.uname())
ret["runningTime"] = (time.time() - psutil.boot_time())
return ret
def M(val):
return val / 1048576.
def K(val):
return val / 1024.
def G(val):
return val / 1073741824.
def T(val):
return val / 1099511627776.
class Memory(object):
@staticmethod
def get_mem_info(u="M"):
unit = M
if u == "K" or u == "k":
unit = K
if u == "G" or u == "g":
unit = G
if u == "T" or u == "t":
unit = T
vmem = psutil.virtual_memory()
total = vmem.total
available = vmem.available
percent = vmem.percent
used = vmem.used
free = vmem.free
memInfo = dict()
memInfo["total"] = unit(total)
memInfo["available"] = unit(available)
memInfo["percent"] = percent
memInfo["used"] = unit(used)
memInfo["free"] = unit(free)
return memInfo
@staticmethod
def get_swap_info(u="M"):
unit = M
if u == "K" or u == "k":
unit = K
if u == "G" or u == "g":
unit = G
if u == "T" or u == "t":
unit = T
vmem = psutil.swap_memory()
total = vmem.total
percent = vmem.percent
used = vmem.used
free = vmem.free
memInfo = dict()
memInfo["total"] = unit(total)
memInfo["percent"] = percent
memInfo["used"] = unit(used)
memInfo["free"] = unit(free)
return memInfo
class Disk(object):
@staticmethod
def get_disk(u="M"):
unit = M
if u == "K" or u == "k":
unit = K
if u == "G" or u == "g":
unit = G
if u == "T" or u == "t":
unit = T
all_disk_info = []
all_disk = psutil.disk_partitions()
for d in all_disk:
try:
this_disk = obj2dict(d)
thisUsage = psutil.disk_usage(d.mountpoint)
this_disk["total"] = unit(thisUsage.total)
this_disk["used"] = unit(thisUsage.used)
this_disk["free"] = unit(thisUsage.free)
this_disk["percent"] = thisUsage.percent
all_disk_info.append(this_disk)
except FileNotFoundError:
continue
return all_disk_info
@staticmethod
def get_io():
io = psutil.disk_io_counters(perdisk=True)
for i in io:
this_io = io[i]
io[i] = obj2dict(this_io)
return io
class Network(object):
@staticmethod
def get_if():
io = psutil.net_io_counters(pernic=True)
IF = psutil.net_if_stats()
if_info = dict()
for i in io:
info = obj2dict(io[i])
if i in IF:
info.update(obj2dict(IF[i]))
if_info[i] = info
return if_info
@staticmethod
def get_addrs():
if_addrs = psutil.net_if_addrs()
for i in if_addrs:
new_list = []
for j in if_addrs[i]:
new_list.append(obj2dict(j))
if_addrs[i] = new_list
return if_addrs
def get_adaptable_unit(val):
"""
:param val: a volume of storage or memory with a unit MB (mega Bytes)
:return: a tuple (double: new val, string: unit)
"""
large_unit = ["MB", "GB", "TB"]
small_unit = ["MB", "KB", "B"]
step = 1.
if val < 1000:
unit = small_unit
step = 1 / 1024.
else:
unit = large_unit
step = 1024.
for u in unit:
if val < 1000:
return val, u
val = val / step
def unit_convector(val, unit, target_unit):
convector = {"B": 1, "KB": 1024, "MB": 1048576, "GB": 1073741824, "TB": 1099511627776}
return val * convector[unit] / convector[target_unit]