-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchainmon.py
More file actions
137 lines (78 loc) · 3.31 KB
/
chainmon.py
File metadata and controls
137 lines (78 loc) · 3.31 KB
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
#!/usr/bin/env python3
# coding: UTF-8
import datetime
import settings
import argparse
import pathlib
import logging
import signal
import time
import sys
from discord_webhook import DiscordWebhook
from util_email import sendEmail
from slickrpc import Proxy
from slickrpc import exc
proxy = Proxy('http://%s:%s@%s' % (settings.rpc_user, settings.rpc_pass, settings.rpc_address))
################################################################################
## ChainMonApp class ###########################################################
################################################################################
class ChainMonApp:
def __init__(self):
pass
############################################################################
def get_block_info(self):
blockchaininfo = proxy.getblockchaininfo()
bestblockhash = blockchaininfo['bestblockhash']
bestblockinfo = proxy.getblock(bestblockhash)
return (blockchaininfo['blocks'], bestblockinfo['time'])
############################################################################
def run(self):
stalls = 0
(block, block_time) = self.get_block_info()
logging.info('Blockchain monitoring starting at block {}'.format(block))
while True:
time.sleep(settings.stall_sec)
(new_block, block_time) = self.get_block_info()
if new_block == block:
stalls += 1
if bin(stalls).count('1') == 1:
message = 'Blockchain stalled at block {} for {} minutes'.format(block, (int(time.time()) - block_time) // 60)
logging.info(message)
if settings.email_enable:
sendEmail(settings.to_email, message)
if settings.twitter_enable:
pass
if settings.discord_enable:
webhook = DiscordWebhook(url=settings.discord_webhook, content=message)
response = webhook.execute()
else:
stalls = 0
block = new_block
################################################################################
def terminate(signalNumber, frame):
sys.exit()
################################################################################
### Main program ###############################################################
################################################################################
def main():
if sys.version_info[0] < 3:
raise 'Use Python 3'
pathlib.Path(settings.coin_symbol).mkdir(parents=True, exist_ok=True)
logging.basicConfig(filename = settings.coin_symbol + '/{:%Y-%m-%d}.log'.format(datetime.datetime.now()),
filemode = 'a',
level = logging.INFO,
format = '%(asctime)s - %(levelname)s : %(message)s',
datefmt = '%d/%m/%Y %H:%M:%S')
logging.info('STARTUP')
signal.signal(signal.SIGINT, terminate) # keyboard interrupt ^C
signal.signal(signal.SIGTERM, terminate) # kill [default -15]
argparser = argparse.ArgumentParser(description='Blockchain stall monitor')
command_line_args = argparser.parse_args()
logging.info('Arguments %s', vars(command_line_args))
app = ChainMonApp()
app.run()
logging.info('SHUTDOWN')
################################################################################
if __name__ == '__main__':
main()
################################################################################