-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator_checker.py
166 lines (139 loc) · 5.26 KB
/
validator_checker.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
from __future__ import print_function
from emailClient import email_client
from PyQt5 import QtWidgets
import sys
import queue
import time
from web3 import Web3
import configparser
import sched, time
import psutil
import os
import shutil
import subprocess
from Fuse_Explorer_API.account import Account
from sys import platform
import contractABI
statusTypes = ['RAM', 'HDD', 'CPU', 'ETHBalance', 'FuseBalance', 'validating', 'dockerRunning']
subKeys = ['value', 'report', 'timeStamp']
recieveKeywords = ['KILL', 'STATUS', 'THRESHOLD_SET', 'THRESHOLD_GET']
class Main():
status = {}
def checkSystemAttributes(self):
total, used, free = shutil.disk_usage("/")
self.table['HDD']['value'] = free/(1024*1024)
self.table['RAM']['value'] = psutil.virtual_memory().available/(1024*1024)
oneMin, fiveMin, tenMin = [x / psutil.cpu_count() * 100 for x in psutil.getloadavg()]
self.table['CPU']['value'] = fiveMin
fuseBalance = self.web3Fuse.eth.getBalance(self.address)
if fuseBalance is not None:
self.table['FuseBalance']['value'] = float(int(fuseBalance) * 1e-18)
activeValidator = self.fuseConsensusContract.functions.getValidators().call()
validating = self.fuseConsensusContract.functions.isValidator(self.address).call()
if(self.address in activeValidator):
if(validating):
self.table['validating']['value'] = "True"
self.table['validating']['report'] = 0
else:
self.table['validating']['value'] = "False"
self.table['validating']['report'] = 1
if platform == "linux" or platform == "linux2":
s = subprocess.check_output('docker ps', shell=True)
if str(s).find('fusenet') != -1:
self.table['dockerRunning']['value'] = 1
else:
self.table['dockerRunning']['value'] = 0
for key in self.table:
if key != "ETHBalance" and key in self.ThresholdDict and self.ThresholdDict[key] != '0':
if key == 'CPU':
if self.table[key]['value'] > (type(self.table[key]['value'])(self.ThresholdDict[key])):
self.table[key]['report'] = 1
else:
self.table[key]['report'] = 0
self.table[key]['timeStamp'] = 0
else:
if self.table[key]['value'] < (type(self.table[key]['value'])(self.ThresholdDict[key])):
self.table[key]['report'] = 1
else:
self.table[key]['report'] = 0
self.table[key]['timeStamp'] = 0
self.sendErrorReport()
def checkEthBalance(self):
self.table['ETHBalance']['value'] = float(self.web3Eth.eth.getBalance(self.address) / 1e18)
if self.table['ETHBalance']['value'] < (type(self.table['ETHBalance']['value'])(self.ThresholdDict['ETHBalance'])):
self.table['ETHBalance']['report'] = 1
else:
self.table['ETHBalance']['report'] = 0
self.table['ETHBalance']['timeStamp'] = 0
self.sendErrorReport()
def sendErrorReport(self):
for key in self.table:
if self.table[key]['report'] != 0:
#resend every hour
if int(time.time()) - self.table[key]['timeStamp'] >= 60*60:
Report = {}
Report['type'] = key
Report['value'] = self.table[key]['value']
Report['reportType'] = 'error'
self.sendQueue.put(Report)
self.table[key]['timeStamp'] = int(time.time())
def checkIndoundEmails(self):
if (self.receiveQueue.qsize() != 0):
message = self.receiveQueue.get()
subject = message.subject
Report = {}
Report['reportType'] = subject
if subject == 'KILL':
exit(0)
elif subject == 'STATUS':
string = ''
for key in self.table:
string += key + '=' + str(self.table[key]['value']) + '\n'
Report['value'] = string
self.sendQueue.put(Report)
elif subject == 'THRESHOLD_GET':
string = ''
for key in self.ThresholdDict:
string += key + '=' + str(self.ThresholdDict[key]) + '\n'
Report['value'] = string
self.sendQueue.put(Report)
elif subject == 'THRESHOLD_SET':
snippet = message.snippet
split = snippet.split('=')
if(len(split) > 1):
type = split[0]
if type in statusTypes:
self.ThresholdDict[type] = split[1]
def periodic(self, scheduler, interval, action, actionargs=()):
scheduler.enter(interval, 1, self.periodic,
(scheduler, interval, action, actionargs))
action(*actionargs)
def main(self):
self.table = {}
for keys in statusTypes:
self.table[keys] = {}
for subKeysItr in subKeys:
self.table[keys][subKeysItr] = 0
s = sched.scheduler(time.time, time.sleep)
config = configparser.ConfigParser()
config.optionxform=str
config.read("config.ini")
self.sendQueue = queue.Queue()
self.receiveQueue = queue.Queue()
ui = email_client(self.sendQueue, self.receiveQueue, dict(config['SETUP']))
self.ThresholdDict = dict(config.items('THRESHOLDS'))
for keys in self.ThresholdDict:
if keys not in statusTypes:
print("Error in config file ", keys, " not a valid type")
exit(1)
self.web3Eth = Web3(Web3.HTTPProvider(config['SETUP']['infura']))
self.web3Fuse = Web3(Web3.HTTPProvider(contractABI.RPC_ADDRESS))
self.fuseConsensusContract = self.web3Fuse.eth.contract(abi=contractABI.CONSENSUS_ABI, address=contractABI.CONSENSUS_ADDRESS)
self.address = config['SETUP']['address']
self.apiFuseAccount = Account(address=config['SETUP']['address'])
self.periodic(s, 10, self.checkSystemAttributes)
self.periodic(s, 10, self.checkIndoundEmails)
self.periodic(s, 60*60, self.checkEthBalance)
s.run()
if __name__ == "__main__":
Main().main()