-
Notifications
You must be signed in to change notification settings - Fork 1
/
runforever.py
executable file
·264 lines (227 loc) · 9.4 KB
/
runforever.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python3
"""
This will run forever and check the time to see if it should run again.
When it runs it runs a speed test and dpu port status test.
We then fetch the results and save them locally.
"""
import time
import datetime
import os
import json
import random
import sqlite3
import subprocess
import logging
import signal
import psutil
import aussiebb.portal as portal
logging.basicConfig(level=logging.INFO)
_DEBUG=False
if os.environ.get('FLASK_ENV') == 'development':
logging.basicConfig(level='DEBUG')
_DEBUG=True
def runsql(query):
"""run the supplied query"""
conn = sqlite3.connect('aussiebbmgt.db')
cursor = conn.cursor()
results = cursor.execute(query)
conn.commit()
response = results.fetchall()
conn.close()
return response
def getsettings():
"""get all the settings"""
_settings={}
# read in the cadence
try:
_settings['cadence'] = runsql("select value from settings where key='cadence'")[0][0]
except: #pylint: disable=bare-except
runsql("insert into settings(key,value) values ('cadence', 24)")
_settings['cadence'] = runsql("select value from settings where key='cadence'")[0][0]
# read in the random minute and/or set it
try:
_settings['minute'] = runsql("select value from settings where key='minute'")[0][0]
except: #pylint: disable=bare-except
value = random.randrange(0, 59)
key = 'minute'
# pylint: disable=line-too-long
runsql("insert into settings(key,value) values ('%s', '%s') on conflict(key) do update set value='%s' where key='%s'" % (key, value, value, key))
# pylint: enable=line-too-long
_settings['minute'] = runsql("select value from settings where key='minute'")[0][0]
# read in username and password
_settings['username'] = runsql("select value from settings where key='aussiebb_username'")[0][0]
_settings['password'] = runsql("select value from settings where key='aussiebb_password'")[0][0]
# read in the fttcsyncmonitor 1/enabled by default
try:
_settings['fttcsyncmonitor'] = runsql("select value from settings where key='fttcsyncmonitor'")[0][0]
except: #pylint: disable=bare-except
_settings['fttcsyncmonitor'] = 1
return _settings
def runtests(): #pylint: disable=too-many-locals
"""Run the dpu port status and speed tests"""
_settings = getsettings()
# DPU Port Status / FttC Sync Speed
if _settings['fttcsyncmonitor'] == 1:
abbportal = portal.AussiePortal(
_settings['username'],
_settings['password'],
debug=_DEBUG)
customer = abbportal.customer()
services = []
for service_type in customer['services']:
for service in customer['services'][service_type]:
service_id = service['service_id']
services.append((service_type, service_id))
dpu = abbportal.dpuportstatus(service_id)
logging.info(dpu)
status = dpu['status']
testid = dpu['id']
while status == "InProgress":
time.sleep(30)
testresult = abbportal.testresult(service_id, testid)
status = testresult['status']
logging.info(testresult)
# run the speed test
try:
p_speedtest = subprocess.run(
['/usr/bin/abb-speedtest', '-j'],
capture_output=True,
check=True,
timeout=600
)
except subprocess.TimeoutExpired:
logging.error('speed test timedout')
for proc in psutil.process_iter():
if proc.name() == 'chrome':
proc.kill()
except subprocess.CalledProcessError:
logging.error('speed test failed')
for proc in psutil.process_iter():
if proc.name() == 'chrome':
proc.kill()
os.system('/bin/rm -rf /tmp/lighthouse.X*')
for line in p_speedtest.stdout.decode("utf-8").split('\n'):
try:
result = json.loads(line)
break
except json.JSONDecodeError:
pass
speedid = int(runsql("select id from speedtestresults order by id desc limit 1")[0][0]) + 1
insertline = ("insert into speedtestresults values ('%s', '%s', '%s', '%s', '%s', '%s')"
% (speedid,
result['location'],
result['ping'],
int(round(float(result['download'])*1024,0)),
int(round(float(result['upload'])*1024,0)),
result['isodate']))
logging.info(insertline)
runsql(insertline)
# pylint: disable=too-many-locals, too-many-branches
def saveresults():
"""
Get the results from AussieBB and save the locally.
Aussie doesn't keep all of them, so it's better we do.
"""
_settings = getsettings()
abbportal = portal.AussiePortal(
_settings['username'],
_settings['password'],
debug=_DEBUG)
customer = abbportal.customer()
services = []
for service_type in customer['services']:
for service in customer['services'][service_type]:
service_id = service['service_id']
services.append((service_type, service_id))
if _settings['fttcsyncmonitor'] == 1:
# get and populate all the line sync / dpu port status test results
tests = abbportal.tests(service_id)
results = runsql('select id from dpuportstatusresults')
ids = []
for i in results:
ids.append(i[0])
for result in tests:
if (result['type']) == 'DPU Port Status':
if result['id'] not in ids:
output = abbportal.testresult(service_id, result['id'])
linerate = output['output']['accessLineRate']
if linerate in ('N/A', "Not Found", None):
lineup = 0
linedown = 0
else:
linedown = linerate.split('/')[0].replace('>','')
lineup = linerate.split('/')[1].split(' ')[0]
# pylint: disable=line-too-long
insertline = ("insert into dpuportstatusresults values ('%s', '%s', '%s', '%s', '%s', %s, %s, '%s', '%s')"
# pylint: enable=line-too-long
% (output['id'],
output['result'],
output['output']['syncState'],
output['output']['operationalState'],
output['output']['reversePowerState'],
lineup,linedown,
output['completed_at'],
output['status']))
logging.info(insertline)
runsql(insertline)
# get and populate all the spped test results
tests = abbportal.speedtestresults(service_id)
results = runsql('select id from speedtestresults')
ids = []
for i in results:
ids.append(i[0])
for result in tests:
if result['id'] not in ids:
completed_at = result['date']
# if we have a Z at the end remove it.
if completed_at[-1] == 'Z':
completed_at = completed_at[:-1]
# This little chunk of code tries to dedup
time_completed = datetime.datetime.fromisoformat(completed_at)
# pylint: disable=line-too-long
query = ("select id from speedtestresults where date > '%s' and date < '%s'"
% ( str((time_completed - datetime.timedelta(minutes=1)).strftime('%Y-%m-%dT%H:%M:%SZ')),
str((time_completed - datetime.timedelta(minutes=-1)).strftime('%Y-%m-%dT%H:%M:%SZ'))))
# pylint: enable=line-too-long
for result_to_delete in runsql(query):
delquery = ("delete from speedtestresults where id=%s" % result_to_delete[0])
logging.info(delquery)
runsql(delquery)
# now do the insert of the saved data to use the real id
insertline = ("insert into speedtestresults values ('%s', '%s', '%s', '%s', '%s', '%s')"
% (result['id'],
result['server'],
result['latencyMs'],
result['downloadSpeedKbps'],
result['uploadSpeedKbps'],
result['date']))
logging.info(insertline)
runsql(insertline)
# pylint: enable=too-many-locals, too-many-branches
def timer_expired(signum, frame): #pylint: disable=unused-argument
"""Do something when we the timer expires."""
logging.error("execution timer expired.")
raise Exception("we ran out of time")
logging.info("starting runforever")
while True:
time.sleep(60)
settings = getsettings()
logging.debug(settings)
logging.debug(datetime.datetime.now())
# is it the minute?
if datetime.datetime.now().minute != int(settings['minute']):
logging.debug("wrong minute")
continue
# is it the hour?
if datetime.datetime.now().hour%int(settings['cadence']) != 0:
logging.debug("wrong hour")
continue
logging.debug("it's the right time, let's run the tests and get the results")
signal.signal(signal.SIGALRM, timer_expired)
signal.alarm(300)
try:
runtests()
saveresults()
except Exception as error: #pylint: disable=broad-except
logging.error(error)
signal.alarm(0)