-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·451 lines (399 loc) · 16.1 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import pykeybasebot.types.chat1 as chat1
from packaging.version import Version
from datetime import datetime
from pykeybasebot import Bot
from hashlib import sha256
from web3 import Web3
import threading
import xmltodict
import requests
import asyncio
import base64
import json
import time
import config
w3 = Web3(Web3.HTTPProvider(config.IDCHAIN_RPC_URL))
issues = {}
states = {}
last_sent_alert = time.time()
keybase_bot = None
if config.KEYBASE_BOT_KEY:
keybase_bot = Bot(
username=config.KEYBASE_BOT_USERNAME,
paperkey=config.KEYBASE_BOT_KEY,
handler=None
)
def how_long(ts):
duration = time.time() - ts
if duration > 24 * 60 * 60:
int_part = int(duration / (24 * 60 * 60))
str_part = 'day' if int_part == 1 else 'days'
elif duration > 60 * 60:
int_part = int(duration / (60 * 60))
str_part = 'hour' if int_part == 1 else 'hours'
elif duration > 60:
int_part = int(duration / 60)
str_part = 'minute' if int_part == 1 else 'minutes'
else:
return ''
return f'since {int_part} {str_part} ago'
def alert(issue):
global last_sent_alert
if issue['resolved']:
msg = issue['message']
else:
msg = f"{issue['message']} {how_long(issue['started_at'])}"
print(time.strftime('%a, %d %b %Y %H:%M:%S', time.gmtime()), msg)
if config.KEYBASE_BOT_KEY:
try:
channel = chat1.ChatChannel(**config.KEYBASE_BOT_CHANNEL)
asyncio.run(keybase_bot.chat.send(channel, msg))
keybase_done = True
except Exception as e:
print('keybase error', e)
keybase_done = False
if config.TELEGRAM_BOT_KEY:
try:
payload = json.dumps(
{'chat_id': config.TELEGRAM_BOT_CHANNEL, 'text': msg})
headers = {'content-type': 'application/json',
'cache-control': 'no-cache'}
url = f'https://api.telegram.org/bot{config.TELEGRAM_BOT_KEY}/sendMessage'
requests.post(url, data=payload, headers=headers)
telegram_done = True
except Exception as e:
print('telegram error', e)
telegram_done = False
if keybase_done or telegram_done:
last_sent_alert = time.time()
return keybase_done or telegram_done
def check_issues():
for key in list(issues.keys()):
issue = issues[key]
if issue['resolved']:
res = alert(issue)
if res:
del issues[key]
continue
if issue['last_alert'] == 0:
res = alert(issue)
if res:
issue['last_alert'] = time.time()
issue['alert_number'] += 1
continue
next_interval = min(config.MIN_MSG_INTERVAL * 2 **
(issue['alert_number'] - 1), config.MAX_MSG_INTERVAL)
next_alert = issue['last_alert'] + next_interval
if next_alert <= time.time():
res = alert(issue)
if res:
issue['last_alert'] = time.time()
issue['alert_number'] += 1
if time.time() - last_sent_alert > 24 * 60 * 60 and len(issues) == 0:
res = alert({
'resolved': True,
'message': "There wasn't any issue in the past 24 hours"
})
def issue_hash(node, issue_name):
message = (node + issue_name).encode('ascii')
h = base64.b64encode(sha256(message).digest()).decode('ascii')
return h.replace('/', '_').replace('+', '-').replace('=', '')
def get_idchain_block_number():
payload = json.dumps(
{'jsonrpc': '2.0', 'method': 'eth_blockNumber', 'params': [], 'id': 1})
headers = {'content-type': 'application/json', 'cache-control': 'no-cache'}
r = requests.request('POST', config.IDCHAIN_RPC_URL,
data=payload, headers=headers)
return int(r.json()['result'], 0)
def get_eidi_balance(addr):
payload = json.dumps({
'jsonrpc': '2.0',
'method': 'eth_getBalance',
'params': [addr, 'latest'],
'id': 1
})
headers = {'content-type': 'application/json', 'cache-control': 'no-cache'}
r = requests.request('POST', config.IDCHAIN_RPC_URL,
data=payload, headers=headers)
return int(r.json()['result'], 0) / 10**18
def get_node_state(node):
global states
key = issue_hash(node['url'], 'state')
try:
r = requests.get(node['url'])
state = r.json().get('data', {})
state['senderTransactionCount'] = w3.eth.getTransactionCount(
w3.toChecksumAddress(state['consensusSenderAddress']), 'pending')
if key not in states:
states[key] = []
states[key].append(state)
states[key] = states[key][-5:]
if key in issues:
issues[key]['resolved'] = True
issues[key]['message'] = f'BrightID node {node["url"]} state issue is resolved.'
except:
state = None
if key not in issues:
issues[key] = {
'resolved': False,
'message': f'BrightID node {node["url"]} is not returning its state!',
'started_at': int(time.time()),
'last_alert': 0,
'alert_number': 0
}
return state
def check_node_balance(node, state):
consensus_sender = state.get('consensusSenderAddress')
if not consensus_sender:
consensus_sender = node['eth_address']
balance = get_eidi_balance(consensus_sender)
key = issue_hash(node['url'], 'Eidi balance')
if balance < config.BALANCE_BORDER:
if key not in issues:
issues[key] = {
'resolved': False,
'message': f'BrightID node {node["url"]} has only {round(balance, 2)} Eidi! Alert border is {config.BALANCE_BORDER} Eidi.',
'started_at': int(time.time()),
'last_alert': 0,
'alert_number': 0
}
else:
issues[key]['message'] = f'BrightID node {node["url"]} has only {round(balance, 2)} Eidi! Alert border is {config.BALANCE_BORDER} Eidi.'
else:
if key in issues:
issues[key]['resolved'] = True
issues[key]['message'] = f'BrightID node {node["url"]} Eidi balance issue is resolved.'
def check_node_receiver(node, state, block_number):
key = issue_hash(node['url'], 'consensus receiver service')
if block_number - state['lastProcessedBlock'] > config.RECEIVER_BORDER:
if key not in issues:
issues[key] = {
'resolved': False,
'message': f'BrightID node {node["url"]} consensus receiver service is not working!',
'started_at': int(time.time()),
'last_alert': 0,
'alert_number': 0
}
else:
if key in issues:
issues[key]['resolved'] = True
issues[key]['message'] = f'BrightID node {node["url"]} consensus receiver service issue is resolved.'
def check_node_scorer(node, state, block_number):
key = issue_hash(node['url'], 'scorer service')
if block_number - state['verificationsBlock'] > config.SNAPSHOT_PERIOD + config.SCORER_BORDER:
if key not in issues:
issues[key] = {
'resolved': False,
'message': f'BrightID node {node["url"]} scorer service is not working!',
'started_at': int(time.time()),
'last_alert': 0,
'alert_number': 0
}
else:
if key in issues:
issues[key]['resolved'] = True
issues[key]['message'] = f'BrightID node {node["url"]} scorer service issue is resolved.'
def check_node_sender(node):
if len(states[issue_hash(node['url'], 'state')]) < 5:
return
key = issue_hash(node['url'], 'consensus sender service')
inits = [state['initOp']
for state in states[issue_hash(node['url'], 'state')]]
sents = [state['senderTransactionCount']
for state in states[issue_hash(node['url'], 'state')]]
is_sending = sents[-1] > sents[-3]
# if inits are increasing or constant while first is not 0
if (sorted(inits) == inits and inits[0] != 0) and not is_sending:
if key not in issues:
issues[key] = {
'resolved': False,
'message': f'BrightID node {node["url"]} consensus sender service is not working!',
'started_at': int(time.time()),
'last_alert': 0,
'alert_number': 0
}
else:
if key in issues:
issues[key]['resolved'] = True
issues[key]['message'] = f'BrightID node {node["url"]} consensus sender service issue is resolved.'
def check_node_profile(node):
r = requests.get(node['profile_service_url'])
key = issue_hash(node['url'], 'profile service')
if r.status_code != 200:
if key not in issues:
issues[key] = {
'resolved': False,
'message': f'BrightID node {node["url"]} profile service is not working!',
'started_at': int(time.time()),
'last_alert': 0,
'alert_number': 0
}
else:
if key in issues:
issues[key]['resolved'] = True
issues[key]['message'] = f'BrightID node {node["url"]} profile service issue is resolved.'
def check_node_version(node):
last_release = states[issue_hash(
'http://node.brightid.org/brightid/v6/state', 'state')][0]['version']
node_release = states[issue_hash(node['url'], 'state')][0]['version']
key = issue_hash(node['url'], 'v6_version')
if Version(node_release) < Version(last_release):
if key not in issues:
issues[key] = {
'resolved': False,
'message': f'BrightID node {node["url"]} ({node_release}) is not updated! current release is {last_release}',
'started_at': int(time.time()),
'last_alert': 0,
'alert_number': 0
}
else:
if key in issues:
issues[key]['resolved'] = True
issues[key]['message'] = f'BrightID node {node["url"]} update issue is resolved.'
def check_nodes():
for node in config.NODES:
try:
state = get_node_state(node)
if state:
block_number = get_idchain_block_number()
check_node_balance(node, state)
check_node_receiver(node, state, block_number)
check_node_scorer(node, state, block_number)
check_node_sender(node)
check_node_profile(node)
check_node_version(node)
check_node_updater(node, state, block_number)
except Exception as e:
print('Error: ', node['url'], e)
def check_recovery_service():
r = requests.get(config.RECOVERY_SERVICE_URL)
key = issue_hash(config.NODE_ONE, 'recovery service')
if r.status_code != 200:
if key not in issues:
issues[key] = {
'resolved': False,
'message': f'BrightID recovery service is not working!',
'started_at': int(time.time()),
'last_alert': 0,
'alert_number': 0
}
else:
if key in issues:
issues[key]['resolved'] = True
issues[key]['message'] = f'BrightID recovery service issue is resolved.'
def check_backup_service():
key = issue_hash(config.NODE_ONE, 'backup service')
r = requests.get(config.BACKUPS_URL)
backups = xmltodict.parse(r.text)['ListBucketResult']['Contents']
times = [b['LastModified']
for b in backups if b['Key'].endswith('.tar.gz')]
last_backup = datetime.strptime(
times[-1], '%Y-%m-%dT%H:%M:%S.%fZ').timestamp()
if time.time() - last_backup > config.BACKUP_BORDER:
if key not in issues:
issues[key] = {
'resolved': False,
'message': f'BrightID official node backup service is not working!',
'started_at': int(time.time()),
'last_alert': 0,
'alert_number': 0
}
else:
if key in issues:
issues[key]['resolved'] = True
issues[key]['message'] = f'BrightID official node backup service issue is resolved.'
def check_apps_sp_balance():
apps = requests.get(f'{config.NODE_ONE}/apps').json()['data']['apps']
for app in apps:
if app['assignedSponsorships'] == 0:
continue
key = issue_hash(app['id'], 'sp balance')
border = int(app['assignedSponsorships'] * 0.05)
if app['unusedSponsorships'] < border:
if key not in issues:
issues[key] = {
'resolved': False,
'message': f'{app["id"]} has only {app["unusedSponsorships"]} unused Sponsorships!',
'started_at': int(time.time()),
'last_alert': 0,
'alert_number': 0
}
else:
issues[key]['message'] = f'{app["id"]} has only {app["unusedSponsorships"]} unused Sponsorships!'
else:
if key in issues:
issues[key]['resolved'] = True
issues[key]['message'] = f'{app["id"]} Sponsorships balance issue is resolved.'
def check_node_updater(node, state, block_number):
apps_key = issue_hash(node['url'], 'apps updater service')
if block_number - state['appsLastUpdateBlock'] > config.APPS_UPDATE_BORDER:
if apps_key not in issues:
issues[apps_key] = {
'resolved': False,
'message': f'BrightID node {node["url"]} apps updater service is not working!',
'started_at': int(time.time()),
'last_alert': 0,
'alert_number': 0
}
else:
if apps_key in issues:
issues[apps_key]['resolved'] = True
issues[apps_key]['message'] = f'BrightID node {node["url"]} apps updater service issue is resolved.'
sp_key = issue_hash(node['url'], 'sponsorships updater service')
if block_number - state['sponsorshipsLastUpdateBlock'] > config.SPONSORSHIPS_UPDATE_BORDER:
if sp_key not in issues:
issues[sp_key] = {
'resolved': False,
'message': f'BrightID node {node["url"]} sponsorships updater service is not working!',
'started_at': int(time.time()),
'last_alert': 0,
'alert_number': 0
}
else:
if sp_key in issues:
issues[sp_key]['resolved'] = True
issues[sp_key]['message'] = f'BrightID node {node["url"]} sponsorships updater service issue is resolved.'
sg_key = issue_hash(node['url'], 'seed groups updater service')
if block_number - state['seedGroupsLastUpdateBlock'] > config.SEEDGROUPS_UPDATE_BORDER:
if sg_key not in issues:
issues[sg_key] = {
'resolved': False,
'message': f'BrightID node {node["url"]} seed groups updater service is not working!',
'started_at': int(time.time()),
'last_alert': 0,
'alert_number': 0
}
else:
if sg_key in issues:
issues[sg_key]['resolved'] = True
issues[sg_key]['message'] = f'BrightID node {node["url"]} seed groups updater service issue is resolved.'
def monitor_service():
i = 0
while True:
i += 1
check_nodes()
try:
check_recovery_service()
except Exception as e:
print('Error recovery service: ', e)
if i % 20 == 0:
try:
check_backup_service()
except Exception as e:
print('Error backup service: ', e)
if i == 40:
try:
check_apps_sp_balance()
except Exception as e:
print('Error check_apps_sp_balance service: ', e)
i = 0
time.sleep(config.CHECK_INTERVAL)
def alert_service():
while True:
check_issues()
time.sleep(config.CHECK_INTERVAL)
if __name__ == '__main__':
print('START')
service1 = threading.Thread(target=monitor_service)
service1.start()
alert_service()