-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFsnNodeHealth.py
319 lines (294 loc) · 14.5 KB
/
FsnNodeHealth.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
#!/usr/bin/env python3
#
import os
import time
import smtplib
import sys
import socket
import signal
from email.message import EmailMessage
import urllib.request
import json
import datetime
#
####################################################################################################################
#
# Author: Marcel Cure
# Date: 2nd July 2019
#
# Purpose: To continually check that Fusion nodes are up and running and to send email alerts if necessary
#
# Please note: I do not accept responsibility for problems that arise from use of this programme, including if it fails
# to detect that your node is not functioning. Use at your own peril!
#
# You need a gmail account to use this programme.
#
####################################################################################################################
#
# If you want to buy me a beer you can send a few FSN to 0xaa8c70e134a5A88aBD0E390F2B479bc31C70Fee1
# If you do tip me, feel free to ask for customizations!
#
# I will be adding more functionality, including a health check of the docker image on the nodes
#
####################################################################################################################
#
# START OF USER CONFIGURABLE SECTION
#
tdelay = 30 # Time in seconds between trying pings to hostIP. OK to leave 'as is' Don't make it less than 20s (i.e. more than the BLOCK TIME of Fusion
# Make sure that tdelay is the same in fusion_health_server_VPS.py
email_time = 7200 # Time in seconds which is the minimum time that consecutive emails can be sent. In an error condition expect an email every email_time seconds
#
maxconnerr = 5 # Max No. of tries to connect to docker before sending email warning. OK to leave 'as is'
mining_import_gap = 5 # We want the latest imported block to be close to the latest mined block. This is the acceptable difference
mining_latest_gap = 2 # We want the latest mined block to be close to the block_height. This is the acceptable difference
#
lowest_free_mem = 500 # in Mb lowest allowable free memory
lowest_free_disk = 10 # on Gb lowest allowable free disk space in / partition
#
hostIP = [
'000.000.000.000', # Mainnet IP address. Put your own one here
# '123.123.123.123', # Comment out the above entry and uncomment this dummy non-existent one to test your email response
]
#
pub_key = '0x0000000000000000000000000000000000000000' # Your Fusion public key for your mining wallet
#
# Define the port on which you want to connect
PORT = 50505 # OK to leave 'as is' unless you are using this port for something else! Make sure that is is visible through the firewalls of VPS AND your PC.
mail_user = '[email protected]' # Put your gmail address here
#
# You must create a Google app password. Go to https://myaccount.google.com/ then select 'Security',
# then 'App Passwords' Generate a password and use it below.
#
mail_password = 'xxxxxxxxxxxxxxxx' # Create a simple app password from your Google account. Put it here
sent_from = ' '
to = ['[email protected]',] # email address that you want to send alert to
#
#
fusion_output_file = 'fusion_mining_info.dat' # If you set this file, then data from your VPS will be logged and you can look at it with Excel or LibreOffice
#
# THE END OF USER CONFIGURABLE SECTION
#
######################################################################################################################
#
#
#
def is_connected():
try:
# connect to the host -- tells us if the host is actually
# reachable
socket.create_connection(("www.google.com", 80))
return True
except OSError:
pass
return False
#
def send_fusion_email(subject, body):
print(subject,' ',body)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(mail_user, mail_password)
#
msg = EmailMessage()
msg.set_content(body)
msg['Subject'] = subject
msg['From'] = mail_user
msg['To'] = to
#
#
try:
server.send_message(msg)
return(1)
except:
return(0)
#
server.close()
def fusion_rewards():
#
#
try:
response = urllib.request.urlopen('https://api.fusionnetwork.io/balances/'+ pub_key)
except:
return('-1')
apifsn = response.read()
apifsn = apifsn.decode("utf-8")
api = json.loads(apifsn)
#
# print(api[0])
# print(api[0]['rewardEarn'])
#
return(api[0]['rewardEarn'])
######################################################################################################################################
#
connerr = 0
old_block_import = 0
old_block_mining = 0
last_email_time = 0
#
if len(fusion_output_file) > 1:
if os.path.isfile(fusion_output_file):
fp = open(fusion_output_file,"a+")
else:
fp = open(fusion_output_file,"w+")
fp.write("Date Imported Block Mined Block Block Height Free Mem (Mb) Free Disk (Mb) Fusion Rewards\n")
#
#
hostname = hostIP[0]
#
while(1):
if is_connected():
if sys.platform[0:5] == 'linux':
response = os.system('ping -c 1 ' + hostname)
elif sys.platform[0:3] == 'win':
response = os.system('ping -n 1 ' + hostname)
else:
print('Only Linux and Windows supported')
sys.exit()
if response == 0:
print(hostname + ' is up')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.connect((hostname, PORT))
s.sendall(b'Hello VPS')
except:
connerr = connerr + 1
print('Could not connect to fusion node monitor, try ',connerr, ' of ',maxconnerr)
if connerr == maxconnerr:
subject = 'Fusion Monitor on VPS is Down' # email subject header
body = ['hostname ' + hostname + ' Fusion Monitor on VPS is down']
#
if last_email_time%email_time == 0:
if send_fusion_email(subject, body):
print('Fusion Fusion Monitor on VPS is down. Email warning sent')
last_email_time = last_email_time + tdelay*maxconnerr
else:
print('Could not send email')
connerr = 0
break
#
while(1):
try:
barray = s.recv(1024)
s.sendall(bytes('received','utf-8'))
line = barray.decode("utf-8")
tm = datetime.datetime.now()
except KeyboardInterrupt:
print('Bye')
sys.exit()
except:
print('Did not receive data from VPS fusion docker')
break
if line.find('Connection established') == 0:
print('Connection established to ' + hostname)
else:
a = line.split()
block_import = int(a[0])
block_mining = int(a[1])
latest_block = int(a[2])
free_mem = int(a[3])
free_disk = int(a[4])
tmstr = "%2.2u/%2.2u/%4.4u %2.2u:%2.2u:%2.2u"%(tm.day,tm.month,tm.year,tm.hour,tm.minute,tm.second)
print(tmstr, ' block_import = ',block_import,' block_mining = ',block_mining, ' latest_block = ',\
latest_block,' free_mem = ',free_mem, ' Mb ',' free_disk = ', free_disk,' kb ',' block_rewards = ', fusion_rewards())
#
#
if len(fusion_output_file) > 1: # Log output if file name is set, eles ignore
fp.write('%s %17u %17u %17u %17u %17u %10.3f\n'\
%(tmstr,block_import,block_mining,latest_block,free_mem,free_disk/(1024),float(fusion_rewards())))
fp.flush()
#
#
if block_mining > block_import + mining_import_gap:
print('Warning: Mining more than ',mining_import_gap,' blocks ahead of the last imported block. Could be that chain is slow!')
subject = 'Fusion Chain Poor Mining Performance' # email subject header
#body = 'hostname ' + hostname + ' Warning: Mining more than ',str(mining_import_gap),' blocks ahead of the last imported block. Could be that chain is slow!'
body = 'hostname ' + hostname + ' Warning: Block height is too far ahead of the last mined block. Could be that chain is slow!'
#
#
if last_email_time%email_time == 0:
if send_fusion_email(subject, body):
print('Fusion Chain Poor Mining Performance. Email warning sent')
last_email_time = last_email_time + tdelay
else:
print('Could not send email')
if latest_block > block_mining + mining_latest_gap:
print('Warning: The block height is more than ',mining_latest_gap,' blocks ahead of the last mined block. Could be that chain is slow!')
subject = 'Fusion Chain Poor Mining Performance' # email subject header
body = 'hostname ' + hostname + ' Warning: Block height is more than ',str(mining_latest_gap),' blocks ahead of the last mined block. Could be that chain is slow!'
#
if last_email_time%email_time == 0:
if send_fusion_email(subject, body):
print('Fusion Chain Poor Mining Performance. Email warning sent')
last_email_time = last_email_time + tdelay
else:
print('Could not send email')
if block_mining == old_block_mining:
print('Warning: The latest mined block is not increasing')
subject = 'Fusion Chain Poor Mining Performance' # email subject header
body = 'hostname ' + hostname + ' Warning: The latest mined block is not increasing'
#
if last_email_time%email_time == 0:
if send_fusion_email(subject, body):
print('Fusion Chain Poor Mining Performance. Email warning sent')
last_email_time = last_email_time + tdelay
else:
print('Could not send email')
else:
old_block_mining = block_mining
if block_import == old_block_import:
print('Warning: The latest imported block is not increasing')
subject = 'Fusion Chain Poor Mining Performance' # email subject header
body = 'hostname ' + hostname + ' Warning: The latest imported block is not increasing'
#
if last_email_time%email_time == 0:
if send_fusion_email(subject, body):
print('Fusion Chain Poor Mining Performance. Email warning sent')
last_email_time = last_email_time + tdelay
else:
print('Could not send email')
else:
old_block_import = block_import
if free_mem < lowest_free_mem:
print('Warning: The available free memory is below the allowable limit')
subject = 'Fusion Low Memory Warning' # email subject header
body = 'hostname ' + hostname + ' Warning: The available free memory is below the allowable limit'
#
if last_email_time%email_time == 0:
if send_fusion_email(subject, body):
print('Fusion Low Memory Warning. Email warning sent')
last_email_time = last_email_time + tdelay
else:
print('Could not send email')
if free_disk/(1024*1024) < lowest_free_disk:
print('Warning: The available free disk space is below the allowable limit')
subject = 'Fusion Low Disk Space Warning' # email subject header
body = 'hostname ' + hostname + ' Warning: The available free disk space is below the allowable limit'
#
if last_email_time%email_time == 0:
if send_fusion_email(subject, body):
print('Fusion Low Disk Space Warning. Email warning sent')
last_email_time = last_email_time + tdelay
else:
print('Could not send email')
else:
print(hostname + ' is down')
try:
if is_connected(): # Only send email if YOUR internet is working, but hostname is unreachable.
subject = 'Fusion Node Down' # email subject header
body = 'hostname '+hostIP[0]+' is down'
#
if last_email_time%email_time == 0:
if send_fusion_email(subject, body):
print('Fusion host is down. Email warning sent')
last_email_time = last_email_time + tdelay
else:
print('Could not send email')
#
except:
print('Fusion host is down OR your internet is not working and could not send an email warning. Check configuration')
#
else:
print('Your internet is not working. Retrying...')
time.sleep(tdelay)
# sys.exit("Quitting program")
#ERROR[08-02|23:09:59.574] Failed to prepare header for mining err="Miner doesn't have ticket at block height 221402"