-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbandwidthlogger.py
123 lines (104 loc) · 4.72 KB
/
bandwidthlogger.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
#!/usr/bin/env python3
import queue
import threading
import psutil
import csv
import time
import os
import speedtest
from datetime import datetime
result = []
filename = "data.csv" # name of file where data is stored
external = "eth0" # name of network interface connected to the internet
internal = "eth1" # name of network interface connected to the LAN
def bw(direction): # function to measure bandwidth usage
interval = 3 # measurement interval
time.sleep(3) # to make sure speedtest already started
print(str(datetime.now()) + ": start bandwidth measurement.")
before = psutil.net_io_counters(pernic=True)
external_before = before[external]
internal_before = before[internal]
time.sleep(interval)
after = psutil.net_io_counters(pernic=True)
external_after = after[external]
internal_after = after[internal]
if direction=="dl":
external_rv_per_sec = (external_after[1] - external_before[1]) / interval / 1000000 * 8
internal_rv_per_sec = (internal_after[1] - internal_before[1]) / interval / 1000000 * 8
print(str(datetime.now()) + ": bandwidth measurement (download) done. external_rv_per_sec = " + str(external_rv_per_sec) + "; internal_rv_per_sec = " + str(internal_rv_per_sec))
return [external_rv_per_sec, internal_rv_per_sec]
elif direction=="ul":
external_tx_per_sec = (external_after[0] - external_before[0]) / interval / 1000000 * 8
internal_tx_per_sec = (internal_after[0] - internal_before[0]) / interval / 1000000 * 8
print(str(datetime.now()) + ": bandwidth measurement (upload) done. external_tx_per_sec = " + str(external_tx_per_sec) + "; internal_tx_per_sec = " + str(internal_tx_per_sec))
return [external_tx_per_sec, internal_tx_per_sec]
def speedtestf(direction, s): # does the speed test to use the full external network bandwidth
# import speedtest
# s = speedtest.Speedtest()
# s.get_best_server()
if direction=="dl":
print(str(datetime.now()) + ": start download speedtest")
s.download()
elif direction=="ul":
print(str(datetime.now()) + ": start upload speedtest")
s.upload()
else:
print("error")
results_dict = s.results.dict()
if direction=="dl":
print(str(datetime.now()) + ": download speedtest done. ping = " + str(results_dict['ping']) + "ms; external DL speed = " + str(results_dict['download']/1000000))
return [results_dict['ping'], results_dict['download']/1000000]
elif direction=="ul":
print(str(datetime.now()) + ": upload speedtest done. ping = " + str(results_dict['ping']) + "ms; external UL speed = " + str(results_dict['upload']/1000000))
return [results_dict['ping'], results_dict['upload']/1000000]
import speedtest
s = speedtest.Speedtest()
s.get_best_server()
# we have to use multitasking to do the bandwidth measurement during the speed test
que = queue.Queue() # the queue collects the results of the different threads
dl_thread_list = list() # two different thread lists to be able to do things sequentially
ul_thread_list = list()
dl = threading.Thread(target=lambda q, arg1, arg2: q.put(speedtestf(arg1, arg2)), args=(que, 'dl', s))
dl_thread_list.append(dl)
dl = threading.Thread(target=lambda q, arg1: q.put(bw(arg1)), args=(que, 'dl'))
dl_thread_list.append(dl)
for thread in dl_thread_list:
thread.start() # let's do some multitasking
result.append(datetime.now().strftime('%Y-%m-%d %H:%M:%S')) # timestamp is first entry in the result list
for thread in dl_thread_list:
thread.join() # wait until threads have done their work
while not que.empty():
result.extend(que.get()) # append results to result list
# same thing, this time for upload:
ul = threading.Thread(target=lambda q, arg1, arg2: q.put(speedtestf(arg1, arg2)), args=(que, 'ul', s))
ul_thread_list.append(ul)
ul = threading.Thread(target=lambda q, arg1: q.put(bw(arg1)), args=(que, 'ul'))
ul_thread_list.append(ul)
for thread in ul_thread_list:
thread.start()
for thread in ul_thread_list:
thread.join()
while not que.empty():
result.extend(que.get())
# structure of result list: timestamp;external_DLa;internal_DLa;Ping;external_DLs;external_ULa;internal_ULa;ping;external_Uls
# let's sort it in a meaningful way
avg_ping = (result[3] + result[7])/2
result.pop(7)
result.pop(3)
result.insert(1, avg_ping)
result.insert(4, result.pop(3))
diff_dl = result[2] - result[3]
if diff_dl < 0:
diff_dl = 0
result.insert(4, diff_dl)
result.insert(7, result.pop(8))
diff_ul = result[6] - result[7]
if diff_ul < 0:
diff_ul = 0
result.insert(8,diff_ul)
# structure result list: timestamp;avg_ping;external_DLa;external_DLs;external_DLo;internal_DLa;external_ULa;external_ULs;external_ULo;internal_ULa
# lets write the results to a file:
with open(filename,'a') as fout:
writer = csv.writer(fout, delimiter = ";")
writer.writerow(result)
os.system("python3 visualisation.py") # and start the visualisation script to generate the html file