-
Notifications
You must be signed in to change notification settings - Fork 1
/
how_busy_psutil.py
executable file
·66 lines (50 loc) · 1.87 KB
/
how_busy_psutil.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
#!/usr/bin/python3
# how_busy functions coded with psutil
# All numbers are returned in bytes for simplicity
import time
import psutil
def get_cpu_usage(interval=1, samples=4):
"Return cpu usage as percentage"
total = 0
for sample in range(samples):
time.sleep(interval) # Must sleep first to avoid counting python cpu usage
cpu = psutil.cpu_percent()
total += cpu
return total / samples
def get_network_usage(interval=1, samples=4, verbose=0):
'''Return total network usage in Bytes / second'''
wait = interval * samples
sent = psutil.net_io_counters().bytes_sent
recv = psutil.net_io_counters().bytes_recv
time.sleep(wait)
total = psutil.net_io_counters().bytes_sent - sent
total += psutil.net_io_counters().bytes_recv - recv
return int(total / wait)
def all_disk_usage(interval=2, reps=4, verbose=0, ignore_links=True):
'''Return total i/o for all devices in Bytes / second'''
wait = reps * interval
'''
start = psutil.disk_io_counters(perdisk=True)
time.sleep(wait)
end = psutil.disk_io_counters(perdisk=True)
total = 0
for dev in end.keys():
if ignore_links and (dev.startswith('dm-') or dev.startswith('loop')):
continue
extra = end[dev].read_bytes - start[dev].read_bytes
extra += end[dev].write_bytes - start[dev].write_bytes
print(dev, extra)
total += extra
'''
start = psutil.disk_io_counters()
time.sleep(wait)
end = psutil.disk_io_counters()
total = end.read_bytes - start.read_bytes
total += end.write_bytes - start.write_bytes
return int(total / wait)
def test():
print("psutil Disk Usage (MB/S):", all_disk_usage() / 1e6)
print("psutil Cpu Usage:", get_cpu_usage())
print("psutil Network Usage:", get_network_usage())
if __name__ == "__main__":
test()