-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.py
179 lines (146 loc) · 4.99 KB
/
check.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
""" Reboot host if hash value < limit"""
"""
1) Install python
apt-get install python
2). Upload host_reboot.py to a folder (\home\app\):
3) Run cmd:
python \home\app\host_reboot.py
"""
HOST_IP = 'YOURIP'
TEST_REGIME = 0 # 1- No reboot only error messages; 0 - reboot
HASH_LIMIT = 30
SERVER_URL = 'http://YourPanel.ethosdistro.com/?json=yes'
LOG_FILE = 'host_reboot.log'
#LOG_FILE = '/var/log/host_reboot.log'
INTERFACES = [
"eth0",
"eth1",
"eth2",
"wlan0",
"wlan1",
"wifi0",
"ath0",
"ath1",
"ppp0",
]
EMAIL_SUBJECT = 'HOST REBOOT'
EMAIL_FROM = '[email protected]'
EMAIL_TO = '[email protected]'
SMTP_SERVER = 'smtp.googlemail.com'
SMTP_PORT = 465
EMAIL_LOGIN='login'
EMAIL_RWD = 'password'
import requests
import json
import os
import logging,logging.handlers
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import socket
if os.name != "nt":
import fcntl
import struct
def get_interface_ip(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s',
ifname[:15]))[20:24])
url = SERVER_URL
headers = {'Accept': 'application/json'}
logger = None
def main():
""" GET URL and parse miner_hashes from JSON """
try :
# cur_ip1 = get_ip_address()
# logger.info("Cuurent host ip=" + str(cur_ip1))
cur_ip = get_lan_ip();
logger.info("Cuurent host ip=" + str(cur_ip))
HOST_IP = cur_ip
resp = requests.get(url, headers=headers)
jso = json.loads(resp.content)
rigs = jso.get('rigs')
logger.info ( 'Get JSON OK')
if rigs is not None:
for k, v in rigs.items():
v_ip = v.get('ip')
logger.info ( ' Host:' + str(v_ip) )
if v_ip is not None and HOST_IP == v_ip:
v_hashes = v.get('miner_hashes')
if v_hashes is not None:
logger.info ( ' Host_ip found OK. miner_hashes:' + str(v_hashes))
mainer_hashes = [float(i) for i in v_hashes.split()]
reboot_needed = 0
all_zero = 1
for mn in mainer_hashes:
if mn > 0:
all_zero = 0
if mn < HASH_LIMIT and mn > 0:
logger.error ( v['ip'] + ' LIMIT IS OVER ' + str(mn))
reboot_needed = 1
break
if mn == 0 and all_zero == 0:
logger.error ( v['ip'] + ' LIMIT IS OVER ' + str(mn))
reboot_needed = 1
break
if reboot_needed:
logger.error ( 'HOST WILL BE REBOOTED')
email_text = 'HOST %s WILL BE REBOOTED. miner_hashes= %s' % (v_ip, v_hashes)
if TEST_REGIME!=1:
os.system('reboot')
send_email(email_text)
else:
logger.info ( 'All is OK.')
else:
logger.error('No rigs found')
return 1
except Exception as err:
logger.error("Ant error found %s" % str(err))
return 0
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
def get_lan_ip():
ip = socket.gethostbyname(socket.gethostname())
if ip.startswith("127.") and os.name != "nt":
interfaces = INTERFACES
for ifname in interfaces:
try:
ip = get_interface_ip(ifname)
break
except IOError:
pass
return ip
def send_email(text):
COMMASPACE = ', '
msg = MIMEMultipart()
msg['Subject'] = EMAIL_SUBJECT
me = EMAIL_FROM
family = EMAIL_TO
msg['From'] = me
msg['To'] = COMMASPACE.join(family)
msg.preamble = EMAIL_SUBJECT
body = MIMEText(text)
msg.attach(body)
try:
server = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT)
server.login(EMAIL_LOGIN, EMAIL_RWD)
server.sendmail(me, family, msg.as_string())
server.quit()
logger.info("SEND email message OK." )
except Exception as err:
logger.error("SEND email error: %s" % str(err))
return 0
if __name__ == "__main__":
FORMAT = "%(asctime)-15s %(message)s"
logging.basicConfig(format=FORMAT,level=logging.INFO,datefmt='%Y-%m-%d %H:%M:%S')
logger = logging.getLogger("host_reboot")
handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes=1024000, backupCount=5)
logger.addHandler(handler)
fmt = logging.Formatter(FORMAT,datefmt='%Y-%m-%d %H:%M:%S')
handler.setFormatter(fmt)
logger.info("Start application")
ret = main()
exit (ret)