/
system_status.py
198 lines (160 loc) · 6.31 KB
/
system_status.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
'''
Get system running status including CPU load, memory usage, network traffic.
'''
import time
import psutil
import six
class SystemStatus(object):
INCLUDED_PARTITIONS = set(['/', '/writable'])
def __init__(self):
self.timestamp = time.time()
self.cpu_load = []
self.mem = dict(total = 0,
available = 0,
free = 0,
cached = 0,
buffers = 0)
self.disk_partitions = {}
self.network = {}
partitions = psutil.disk_partitions()
for p in partitions:
if p.mountpoint in self.INCLUDED_PARTITIONS:
usage = psutil.disk_usage(p.mountpoint)
self.disk_partitions[p.mountpoint] = {
'total': usage.total,
'used': usage.used
}
def getStatus(self, max_age=0.8):
"""
Get current system status.
max_age: maximum tolerable age of cached status information. Set to
None to force a refresh regardless of cache age.
Returns a dictionary with fields 'cpu_load', 'mem', 'disk', and
'network'.
"""
timestamp = time.time()
if (max_age is None or timestamp > self.timestamp + max_age):
self.timestamp = timestamp
self.refreshCpuLoad()
self.refreshMemoryInfo()
self.refreshDiskInfo()
self.refreshNetworkTraffic()
result = {
'cpu_load': self.cpu_load,
'mem': self.mem,
'disk': self.disk_partitions,
'network': self.network
}
return result
def refreshCpuLoad(self):
self.cpu_load = map(int, psutil.cpu_percent(percpu=True))
def refreshMemoryInfo(self):
mem = psutil.virtual_memory()
self.mem['total'] = mem.total
self.mem['available'] = mem.available
self.mem['free'] = mem.free
self.mem['cached'] = mem.cached
self.mem['buffers'] = mem.buffers
def refreshDiskInfo(self):
for key, value in six.iteritems(self.disk_partitions):
usage = psutil.disk_usage(key)
self.disk_partitions[key]['total'] = usage.total
self.disk_partitions[key]['used'] = usage.used
def refreshNetworkTraffic(self):
excluded_interfaces = set(["lo", 'br-lan', 'docker0', 'wlan0'])
interfaces = {}
stats = psutil.net_if_stats()
for key, value in six.iteritems(stats):
if key in excluded_interfaces:
continue
# ignore virtual ethernet interfaces
if key.startswith('veth'):
continue
# Quick fix - ignore interfaces with dots because our server
# refuses to accept keys with dots.
if '.' in key:
continue
interfaces[key] = {
'isup': value.isup,
'speed': value.speed,
'mtu': value.mtu
}
addresses = psutil.net_if_addrs()
for key, value in six.iteritems(addresses):
if key not in interfaces:
continue
for i in value:
if i.family == 2:
interfaces[key]['ipv4'] = i.address
interfaces[key]['netmask'] = i.netmask
elif i.family == 17:
interfaces[key]['mac'] = i.address
traffic = psutil.net_io_counters(pernic=True)
for key, value in six.iteritems(traffic):
if key not in interfaces:
continue
interfaces[key]['bytes_sent'] = value.bytes_sent
interfaces[key]['bytes_recv'] = value.bytes_recv
interfaces[key]['packets_sent'] = value.packets_sent
interfaces[key]['packets_recv'] = value.packets_recv
interfaces[key]['errin'] = value.errin
interfaces[key]['errout'] = value.errout
interfaces[key]['dropin'] = value.dropin
interfaces[key]['dropout'] = value.dropout
self.network = interfaces
@classmethod
def getNetworkInfo(cls):
interfaces = {}
stats = psutil.net_if_stats()
for key, value in six.iteritems(stats):
interfaces[key] = value.__dict__
interfaces[key]['addresses'] = []
interfaces[key]['io_counters'] = None
addresses = psutil.net_if_addrs()
for key, value in six.iteritems(addresses):
if key not in interfaces:
continue
for addr in value:
interfaces[key]['addresses'].append(addr.__dict__)
traffic = psutil.net_io_counters(pernic=True)
for key, value in six.iteritems(traffic):
if key not in interfaces:
continue
interfaces[key]['io_counters'] = value.__dict__
return interfaces
@classmethod
def getProcessInfo(cls, pid):
proc = psutil.Process(pid=pid)
with proc.oneshot():
proc_info = {
'cpu_num': proc.cpu_num(),
'cpu_times': proc.cpu_times().__dict__,
'create_time': proc.create_time(),
'memory_info': proc.memory_info().__dict__,
'num_ctx_switches': proc.num_ctx_switches().__dict__,
'num_threads': proc.num_threads()
}
return proc_info
@classmethod
def getSystemInfo(cls):
system = {
'boot_time': psutil.boot_time(),
'cpu_count': psutil.cpu_count(),
'cpu_stats': psutil.cpu_stats().__dict__,
'cpu_times': [k.__dict__ for k in psutil.cpu_times(percpu=True)],
'disk_io_counters': psutil.disk_io_counters().__dict__,
'disk_usage': [],
'net_io_counters': psutil.net_io_counters().__dict__,
'swap_memory': psutil.swap_memory().__dict__,
'virtual_memory': psutil.virtual_memory().__dict__
}
partitions = psutil.disk_partitions()
for p in partitions:
if p.mountpoint in cls.INCLUDED_PARTITIONS:
usage = psutil.disk_usage(p.mountpoint)
system['disk_usage'].append({
'mountpoint': p.mountpoint,
'total': usage.total,
'used': usage.used
})
return system