-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathinflux.py
More file actions
197 lines (181 loc) · 7.39 KB
/
Copy pathinflux.py
File metadata and controls
197 lines (181 loc) · 7.39 KB
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
import sys, random, time
from urllib3.exceptions import NewConnectionError
from influxdb import InfluxDBClient
import speedflux
from requests.exceptions import ConnectionError
class Influx:
def __init__(self, config):
self.config = config
self._client = None
self.retries = 0
self.init_db()
@property
def client(self):
if not self._client:
self._client = InfluxDBClient(
self.config.INFLUX_DB_ADDRESS,
self.config.INFLUX_DB_PORT,
self.config.INFLUX_DB_USER,
self.config.INFLUX_DB_PASSWORD,
None)
speedflux.LOG.debug("Client extablished")
return self._client
def init_db(self, backoff_in_seconds = 2):
try:
speedflux.LOG.debug("Intitializing Influx Database")
databases = self.client.get_list_database()
if len(list(filter(
lambda x: x['name'] ==
self.config.INFLUX_DB_DATABASE, databases))) == 0:
self.client.create_database(
self.config.INFLUX_DB_DATABASE) # Create if
else:
# Switch to if does exist.
self.client.switch_database(self.config.INFLUX_DB_DATABASE)
self.initilized = True
except (ConnectionError, NewConnectionError) as bad_host:
if self.retries == 3:
speedflux.LOG.error(
"Database Init failed for 3rd time. Exiting")
sys.exit()
sleep = (backoff_in_seconds * 2 ** self.retries + random.uniform(0, 1))
self.retries += 1
speedflux.LOG.error(
"Connection to influx host was refused. This likely "
"means that the DB is down or INFLUX_DB_ADDRESS is "
f"incorrect. It's currently '{self.config.INFLUX_DB_ADDRESS}'")
speedflux.LOG.error("Full Error follows\n")
speedflux.LOG.error(bad_host)
speedflux.LOG.info(f"Backing off before retrying. Sleeping for {sleep} seconds")
time.sleep(sleep)
speedflux.LOG.error(f"Retry {self.retries}: Initiliazing DB.")
self.init_db()
def format_data(self, data):
influx_data = [
{
'measurement': 'ping',
'time': data['timestamp'],
'fields': {
'jitter': data['ping'].get('jitter', 0),
'latency': data['ping'].get('latency', 0)
}
},
{
'measurement': 'download',
'time': data['timestamp'],
'fields': {
# Byte to Megabit
'bandwidth': data['download'].get('bandwidth', 0) / 125000,
'bytes': data['download'].get('bytes', 0),
'elapsed': data['download']['elapsed']
}
},
{
'measurement': 'upload',
'time': data['timestamp'],
'fields': {
# Byte to Megabit
'bandwidth': data['upload'].get('bandwidth', 0) / 125000,
'bytes': data['upload']['bytes'],
'elapsed': data['upload']['elapsed']
}
},
{
'measurement': 'packetLoss',
'time': data['timestamp'],
'fields': {
'packetLoss': int(data.get('packetLoss', 0))
}
},
{
'measurement': 'speeds',
'time': data['timestamp'],
'fields': {
'jitter': data['ping'].get('jitter', 0),
'latency': data['ping'].get('latency', 0),
'packetLoss': int(data.get('packetLoss', 0)),
# Byte to Megabit
'bandwidth_down': data['download'].get(
'bandwidth', 0) / 125000,
'bytes_down': data['download'].get(
'bytes', 0),
'elapsed_down': data['download'].get(
'elapsed'),
# Byte to Megabit
'bandwidth_up': data['upload'].get(
'bandwidth', 0) / 125000,
'bytes_up': data['upload'].get(
'bytes', 0),
'elapsed_up': data['upload'].get(
'elapsed')
}
}
]
tags = self.tag_selection(data)
if tags is not None:
for measurement in influx_data:
measurement['tags'] = tags
return influx_data
def write(self, data, data_type='Speedtest'):
try:
if self.client.write_points(data):
speedflux.LOG.info(F"{data_type} data written successfully")
speedflux.LOG.debug(F"Wrote `{data}` to Influx")
self.retries = 0
else:
raise Exception(F"{data_type} write points did not complete")
except (ConnectionError, NewConnectionError, Exception) as \
bad_connection:
if self.retries == 3:
speedflux.LOG.error(
'Max retries exceeded for write. Check that database'
' is on and can receive data')
speedflux.LOG.error('Exiting')
sys.exit()
speedflux.LOG.error("Connection error occurred during write")
speedflux.LOG.error(bad_connection)
self.retries += 1
speedflux.LOG.error("Reinitiating database and retrying.")
self.init_db()
self.write(data, data_type)
except Exception as err:
speedflux.LOG.error(F"{err}")
def tag_selection(self, data):
tags = self.config.INFLUX_DB_TAGS
options = {}
# tag_switch takes in _data and attaches CLIoutput to more readable ids
tag_switch = {
'namespace': self.config.NAMESPACE,
'isp': data['isp'],
'interface': data['interface']['name'],
'internal_ip': data['interface']['internalIp'],
'interface_mac': data['interface']['macAddr'],
'vpn_enabled': (
False if data['interface']['isVpn'] == 'false' else True),
'external_ip': data['interface']['externalIp'],
'server_id': data['server']['id'],
'server_name': data['server']['name'],
'server_location': data['server']['location'],
'server_country': data['server']['country'],
'server_host': data['server']['host'],
'server_port': data['server']['port'],
'server_ip': data['server']['ip'],
'speedtest_id': data['result']['id'],
'speedtest_url': data['result']['url']
}
if tags is None:
tags = 'namespace'
elif '*' in tags:
return tag_switch
else:
tags = 'namespace, ' + tags
tags = tags.split(',')
for tag in tags:
# split the tag string, strip and add selected tags to {options}
# with corresponding tag_switch data
tag = tag.strip()
options[tag] = tag_switch[tag]
return options
def process_data(self, data):
data = self.format_data(data)
self.write(data)