-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
executable file
·169 lines (127 loc) · 4.54 KB
/
monitor.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
#!/bin/env python3
from web3 import Web3
import smtplib
import datetime
import time
import socket
import configparser
import ovh
import sys
import json
import urllib
config = configparser.ConfigParser() # on relis la config pour qu'elle puisse etre changee en live
if not config.read('config.ini') :
print ("Configuration file 'config.ini' not found or empty")
sys.exit(2)
w3 = Web3(Web3.HTTPProvider(config['eth1']['host']))
DEBUG=True
LASTSMS=None
BALANCES = {}
def do_alert(cause, datas) :
global LASTSMS, DEBUG
if config['mail']['enabled'] == "1" :
debug_print("Sending mail")
mail_alert(cause, datas)
if config['sms']['enabled'] == "1" :
if LASTSMS != cause :
debug_print("Sending sms")
sms_alert(cause, datas)
LASTSMS = cause
else :
debug_print('Cause not changed. No resending SMS')
def mail_alert(cause, datas):
sender = config['mail']['from']
receivers = config['mail']['to']
message = """From: Monitoring Ethereum <%s>
To: %s
Subject: Ethereum alert
A problem has occured on %s.
Alert : %s
Datas : %s
""" % (config['mail']['from'],config['mail']['to'],socket.getfqdn(),cause, datas)
try:
s = smtplib.SMTP( config['mail']['server'])
s.login( config['mail']['login'], config['mail']['password'])
s.sendmail(sender, receivers, message)
print ("Successfully sent email")
except smtplib.SMTPException:
print ("Error: unable to send email")
def sendSMS(dest,message) :
client = ovh.Client('ovh-eu', application_key=config['sms']['appKey'], application_secret=config['sms']['appSecret'], consumer_key=config['sms']['consumerKey'])
res = client.get('/sms')
print(res)
if len(res) == 0 :
print("No ServiceName - Cancelling send")
sys.exit(1)
smsSender = res[0]
url = '/sms/' + smsSender + '/jobs/'
if type(dest) == list :
receivers = dest
else :
receivers = [dest]
r = client.post(url,
charset='UTF-8',
coding='7bit',
message=message,
noStopClause=False,
priority='high',
receivers=receivers,
senderForResponse=True,
validityPeriod=2880
)
print (json.dumps(r, indent=4)) # pour l'affichage du résultat de la transaction.
def sms_alert(cause, datas) :
msg = "ETH1 ALERT : %s ; Datas : %s" % (cause, datas)
sendSMS(config['sms']['to'], msg)
def debug_print(msg) :
if DEBUG :
print("[%s] %s" % (datetime.datetime.now().isoformat(), msg))
def getResponse(url):
operUrl = urllib.request.urlopen(url)
if(operUrl.getcode()==200):
data = operUrl.read()
jsonData = json.loads(data)
else:
print("Error receiving data", operUrl.getcode())
return jsonData
def check_state(cur,last) :
if cur != last :
if cur == "Ok" and last != None :
debug_print("Retour a la normale")
do_alert("Eth1 is back to normal state","Last state was " + last)
return cur
last_state = None
while True :
curstate = "Ok"
debug_print('TIC')
try :
if w3.eth.syncing :
if (w3.eth.syncing['currentBlock'] - w3.eth.syncing['highestBlock'] > 1) :
do_alert("Eth1 Not Synced !", w3.eth.syncing)
debug_print("Not synced")
curstate = "No Synced"
time.sleep(60)
else :
debug_print("Sync Ok")
# Maintenant l'eth2
si = ''
for i in config['eth2']['indices'].split(',') :
si = si + 'indices=%s&' %i
debug_print(si)
r = getResponse('http://localhost:3500/eth/v1alpha1/validators/balances?%s' % si)
for b in r['balances'] :
debug_print(b)
if b['status'] != 'ACTIVE' :
do_alert("Eth2 Validator not active !", str(b))
if b['index'] not in BALANCES :
BALANCES[b['index']] = b['balance']
if BALANCES[b['index']] > b['balance'] :
do_alert('Loosing ETHERS !!!!', str(b))
BALANCES[b['index']] = b['balance']
except Exception as e :
debug_print("Exception has occured : %s - %s" % (e, e.args))
do_alert(type(e).__name__, e.args)
curstate = "Exception"
time.sleep(60)
last_state = check_state(curstate, last_state)
time.sleep(5)