-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathethos.py
212 lines (181 loc) · 7.52 KB
/
ethos.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
import requests
import time
import json
from datetime import datetime
import timeago
import sys
# Details of the ethOS Dashboard
ETHOS_DASH_URL = ''
ETHOS_AILMENT_URL = ''
params = dict( json = 'yes')
headers = {'user-agent': 'Ethos_monitor/0.0.1'}
content_type = {'Content-type': 'application/x-www-form-urlencoded'}
# PushOver details
PUSHOVER_TOKEN = ''
PUSHOVER_USER_KEY = ''
PUSHOVER_API_URL = ''
WAIT_INTERVAL = 60
# for stotring all the no error conditions
no_error_conditions = []
ailments = []
json_data = {}
push_message = ""
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# Function to read the script data files
def load_data_files():
try:
#read the settings.json and no_error_conditions file
with open('./data/settings.json', 'r') as settings_data:
# Its a JSON! so load it as one!
data_file = json.load(settings_data)
with open('./data/no_error_conditions.txt', 'r') as conditions_data:
# its just a plain old txt file
condition_file = conditions_data.read()
set_data(data_file, condition_file)
except Exception as error:
print(bcolors.FAIL+'ERROR : Unable to read the settings file.. Closing the monitor..'+bcolors.ENDC)
print(error)
exit()
# Function to set the data values
def set_data(settings_data, conditions_data):
# Need to set the value for all the global variables mentioned below
global ETHOS_DASH_URL, ETHOS_AILMENT_URL
global PUSHOVER_TOKEN, PUSHOVER_USER_KEY, PUSHOVER_API_URL
global WAIT_INTERVAL, no_error_conditions
try:
ETHOS_DASH_URL = settings_data["ETHOS_DASH_URL"]
ETHOS_AILMENT_URL = settings_data["ETHOS_AILMENT_URL"]
PUSHOVER_TOKEN = settings_data["PUSHOVER_DATA"]["PUSHOVER_TOKEN"]
PUSHOVER_USER_KEY = settings_data["PUSHOVER_DATA"]["PUSHOVER_USER_KEY"]
PUSHOVER_API_URL = settings_data["PUSHOVER_DATA"]["PUSHOVER_API_URL"]
WAIT_INTERVAL = int(settings_data["WAIT_INTERVAL"])
no_error_conditions = str(conditions_data).split()
print('\nETHOS MONITOR - V1.0 by haxzie')
print('Visit /~https://github.com/haxzie/ethos-monitor-with-pushover for more details')
print(time.ctime()+'\tOptions loaded successfully!\n\n')
except Exception as error:
print('ERROR : MISSING DATA in the data files. Check log file for details')
with open('./log.txt', 'a') as log_file:
log_file.write(error + '\n' + settings_data + '\n' + conditions_data + '\n')
print(error)
exit()
# Function to send PushOver notification
def pushover(message):
# Create the PushOver specified API request Paramteres
parameters = dict(
token = PUSHOVER_TOKEN,
user = PUSHOVER_USER_KEY,
url = ETHOS_AILMENT_URL,
message = message
)
try:
# Send the notification details to pushover
push_response = requests.post(PUSHOVER_API_URL, params = parameters, headers = headers )
if push_response.status_code == 200:
push_status = push_response.json()
if push_status['status'] == 1:
print(time.ctime()+bcolors.OKGREEN+'\tPush notifications sent!'+bcolors.ENDC)
else:
print(time.ctime()+'\tERROR : Unable to send notification, PushOver Error')
except Exception as e:
print(time.ctime()+'\tERROR : Unable to send notification request to pushover')
print(e)
# Function to create a message and pass it to pushover
def send_message(recovered_count):
message = ''
if len(ailments) <= 25:
for rig in ailments:
message += '\n'+ str(rig[1]) +'('+ str(rig[0]) +') : '+ str(rig[2])
else:
for i in range(0, 24):
rig = ailments[i]
message += '\n'+ str(rig[1]) +'('+ str(rig[0]) +') : '+ str(rig[2])
message += '\n+'+str((len(ailments)-24))+' more ailments'
# Prepare the message
message += '\n\nTotal Ailments : ' + str(len(ailments))
if recovered_count > 0:
message += '\n'+str(recovered_count)+' Rigs has been recovered'
# Push the message
pushover(message)
# Function to print ailment details
def print_ailments_details():
now = datetime.now()
for ailment in ailments:
print(str(ailment[1])+'('+str(ailment[0])+')\t'+str(ailment[2])+'\t'+(timeago.format(ailment[3], now)))
# Function to check if a given rig is in ailments
def is_in_ailment(key):
for rig in ailments:
if rig[0] == key:
return True
return False
# Function to remove a rig from ailments
def remove_ailment(key):
try:
for index, rig in enumerate(ailments):
if rig[0] == key:
ailments.pop(index)
except Exception as e:
print('ERROR: at remove_ailment()\n'+str(e))
# The function to check the condition of each rig
def check_condition():
""" Get the json data from the api and check the data recieved """
# Check this variable to keep track of any new ailments
new_ailments = False
recovered_count = 0
try:
# get the API response from the ethOS panel
print('\n'+time.ctime()+'\tRequesting response...')
r = requests.get(ETHOS_DASH_URL,
params = params,
headers = headers)
# Okay! things are fine in the higher level we got a response. Yaay!
if r.status_code == 200:
print(time.ctime() + bcolors.OKBLUE+'\tResponse OK! site Loaded!'+bcolors.ENDC)
try:
json_data = r.json()
for key in json_data['rigs']:
# Check if any of the rig has the condition
# which is not mentioned in the no_error_condition file
if json_data['rigs'][key]['condition'] not in no_error_conditions:
# a new rig is in error condition, send the notification! ASAP!!!
if not is_in_ailment(key):
new_ailments = True
rack_loc = json_data['rigs'][key]['rack_loc']
condition = json_data['rigs'][key]['condition']
# add the new ailment to list
ailments.insert(0, [key, rack_loc, condition, datetime.now()])
elif is_in_ailment(key):
# Remove from the aliment list, rig has been recovered
remove_ailment(key)
recovered_count += 1
print(bcolors.OKGREEN+key+' has been recovered'+bcolors.ENDC)
# send the pushover notification
# if there any new ailments
if new_ailments:
# Print the status of ailments
print_ailments_details()
send_message(recovered_count)
except Exception as e:
print(bcolors.FAIL+'ERROR: Invalid JSON'+bcolors.ENDC)
print(str(e))
except:
print(bcolors.WARNING+'ERROR: Request timed out'+bcolors.ENDC)
print('is '+ETHOS_DASH_URL+' live?')
pass
# The main loop to keep doing the task
def main_loop():
load_data_files()
# To infinity and Beyond!
while True:
check_condition()
time.sleep(WAIT_INTERVAL)
if __name__ == '__main__':
main_loop()