-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_node.py
executable file
·86 lines (75 loc) · 3.24 KB
/
check_node.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from dotenv import load_dotenv
import os
import requests
import json
import time
load_dotenv()
alert_treshold = int(os.getenv('ALERT_TRESHOLD'))
sleep_timeout = int(os.getenv('SLEEP_TIMEOUT'))
host_name = os.getenv('HOST_NAME')
bioauth_link = os.getenv('BIOAUTH_LINK')
test_mode = int(os.getenv('TEST_MODE'))
url_node = os.getenv('URL_NODE')
data_node = {'jsonrpc': '2.0', 'method': 'bioauth_status', 'params': [], 'id': 1}
headers_node = {'Content-Type': 'application/json'}
if test_mode:
print('Test mode is enabled')
sleep_timeout = 60
# Send alert to telegram
def send_alert(title, message):
telegram_bot_token = os.getenv('TELEGRAM_BOT_TOKEN')
telegram_chat_id = os.getenv('TELEGRAM_CHAT_ID')
url_tg = f'https://api.telegram.org/bot{telegram_bot_token}/sendMessage'
data_tg = {'parse_mode': 'markdown', 'chat_id': telegram_chat_id, 'text': f"*{title}*\n{message}"}
headers_tg = {'Content-Type': 'application/json'}
response = requests.post(url_tg, data=json.dumps(data_tg), headers=headers_tg).json()
print(response)
return response.get('ok')
if __name__ == '__main__':
while True:
time.sleep(sleep_timeout)
try:
response = requests.post(url_node, data=json.dumps(data_node), headers=headers_node).json()
except requests.exceptions.RequestException as e:
print('Node is not answer!!!')
res = send_alert(title=host_name, message='⛔ Node is not answer!!!')
print('Send alert:', res)
continue
if test_mode:
# Test response
test_response = {"jsonrpc":"2.0","result":{"Active":{"expires_at":1713262470000}},"id":1}
response = test_response
expires_at = time.time()
if response['result'] == "Inactive":
print('Bioauth status is Inactive')
message='⛔ Bioauth status is Inactive'
if bioauth_link:
message += f'\n[Bioauth_link]({bioauth_link})'
res = send_alert(title=host_name, message=message)
print('Send alert:', res)
continue
else:
result = response['result']
if 'Active' in result and 'expires_at' in result['Active']:
print('Bioauth is Active')
print('Bioauth status will expire at', result['Active']['expires_at'])
expires_at = (result['Active']['expires_at'])/1000
# Get time now
now = time.time()
diff = int(expires_at - now)
print('Diff:', diff)
if diff < 0:
print('Bioauth status has expired')
message='⛔ Bioauth status has expired'
if bioauth_link:
message += f'\n[Bioauth_link]({bioauth_link})'
res = send_alert(title=host_name, message=message)
print('Send alert:', res)
elif alert_treshold:
# Send warning once what bioauth status will expire soon
if diff < alert_treshold and diff > alert_treshold - sleep_timeout:
print('Bioauth status will expire in', diff, 'seconds')
send_alert(title=host_name, message=f'⚠️ Bioauth status will expire in {diff} seconds')
print('Send alert:', res)