-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
142 lines (122 loc) · 3.68 KB
/
main.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
from speedtest import Speedtest, SpeedtestResults
import timeit
import os
import json
import time
import traceback
import logging
from influxdb import InfluxDBClient
DB = {
'HOST': os.environ.get('DB_HOST'),
'PORT': int(os.environ.get('DB_PORT')),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASSWORD'),
'DATABASE': os.environ.get('DB_DATABASE')
}
SPEEDTEST = {
'INTERVAL': int(os.environ.get('INTERVAL')),
'FAIL_INTERVAL': int(os.environ.get('FAIL_INTERVAL'))
}
influx = InfluxDBClient(DB['HOST'], DB['PORT'], DB['USER'], DB['PASSWORD'], DB['DATABASE'])
def do_nothing(*args, **kwargs):
pass
class ExtendedSpeedtestResults(SpeedtestResults):
def __init__(self, download=0, upload=0, ping=0, server=None, client=None,
opener=None, secure=False, download_elapsed=0, upload_elapsed=0):
super().__init__(download, upload, ping, server, client, opener, secure)
self.download_elapsed = 0
self.upload_elapsed = 0
def json(self, pretty=False):
kwargs = {}
if pretty:
kwargs.update({
'indent': 2,
'sort_keys': True
})
return json.dumps(self.dict(), **kwargs)
def influxdb(self):
data = [
{
'measurement': 'ping',
'fields': {
# Currently not supported
# 'jitter': self.jitter,
'latency': self.ping
}
},
{
'measurement': 'download',
'fields': {
# bit to Byte to Megabit
'bandwidth': (self.download / 8) / 125000,
'bytes': self.bytes_received,
'elapsed': self.download_elapsed
}
},
{
'measurement': 'upload',
'fields': {
# bit to Byte to Megabit
'bandwidth': (self.upload / 8) / 125000,
'bytes': self.bytes_sent,
'elapsed': self.upload_elapsed
}
}
# Currently not supported
# {
# 'measurement': 'packetLoss',
# 'fields': {
# 'packetLoss': self.pktLoss
# }
# }
]
for data_part in data:
data_part['tags'] = {
'server_id': self.server['id'],
'server_name': self.server['sponsor'],
'server_location': self.server['name'] + ', ' + self.server['country']
}
data_part['time'] = self.timestamp
if influx.write_points(data) != True:
raise
class ExtendedSpeedtest(Speedtest):
def __init__(self, config=None, source_address=None, timeout=10,
secure=False, shutdown_event=None):
super().__init__(config, source_address, timeout, secure, shutdown_event)
self.results = ExtendedSpeedtestResults(
client=self.config['client'],
opener=self._opener,
secure=secure,
)
def download(self, callback=do_nothing, threads=None):
start = timeit.default_timer()
super().download(callback, threads)
self.results.download_elapsed = timeit.default_timer() - start
def upload(self, callback=do_nothing, pre_allocate=True, threads=None):
start = timeit.default_timer()
super().upload(callback, pre_allocate, threads)
self.results.upload_elapsed = timeit.default_timer() - start
def run():
servers = []
threads = None
s = ExtendedSpeedtest()
s.get_best_server()
s.download()
s.upload()
s.results.influxdb()
def main():
print('Influxdb configuration:')
print(DB)
print('Speedtest configuration:')
print(SPEEDTEST)
while (True):
try:
run()
print('Data succesfully stored')
time.sleep(SPEEDTEST['INTERVAL'])
except Exception as e:
print('ERROR during speedtest')
logging.error(traceback.format_exc())
time.sleep(SPEEDTEST['FAIL_INTERVAL'])
if __name__ == '__main__':
main()