-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_net.py
43 lines (36 loc) · 1.27 KB
/
monitor_net.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
import psutil
import time
from datetime import datetime
import os
import sys
def comp_bandwidth(new, old, interval):
dRecv = new.bytes_recv - old.bytes_recv
dSend = new.bytes_sent - old.bytes_sent
return dRecv*8 / interval, dSend*8 / interval
def create_logfile():
log_folder = "./logs/net"
if not os.path.exists(log_folder):
os.makedirs(log_folder)
dt = datetime.fromtimestamp(time.time())
timestamp = dt.strftime("%Y%m%d-%H%M%S")
logfile = timestamp + '-net.log'
return os.path.join(log_folder, logfile)
def main():
interval = 0.1
old_net_stat = psutil.net_io_counters()
if len(sys.argv) > 1:
# second arg as output log file
logfile = sys.argv[1]
else:
logfile = create_logfile()
with open(logfile, 'w') as net_log:
while True:
new_net_stat = psutil.net_io_counters()
bandwidth = comp_bandwidth(new_net_stat, old_net_stat, interval)
print(time.time(), 'Receiving(bit/sec)', bandwidth[0], 'Sending(bit/sec)', bandwidth[1])
net_log.write("{}, {}, {}\n".format(time.time(), bandwidth[0], bandwidth[1]))
old_net_stat = new_net_stat
time.sleep(interval)
net_log.flush()
if __name__ == "__main__":
main()