forked from axiacore/cachet-uptime-robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_status.py
252 lines (215 loc) · 7.95 KB
/
update_status.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
#!/usr/bin/env python3
import json
import sys
import time
import configparser
from urllib import request
from urllib import parse
from datetime import datetime
class UptimeRobot(object):
""" Intermediate class for setting uptime stats.
"""
def __init__(self, api_key):
self.api_key = api_key
self.base_url = 'https://api.uptimerobot.com/v2/getMonitors'
def get_monitors(self, response_times=0, logs=0, uptime_ratio=30):
"""
Returns status and response payload for all known monitors.
"""
endpoint = self.base_url
data = parse.urlencode({
'api_key': format(self.api_key),
'format': 'json',
# responseTimes - optional (defines if the response time data of each
# monitor will be returned. Should be set to 1 for getting them.
# Default is 0)
'response_times': format(response_times),
# logs - optional (defines if the logs of each monitor will be
# returned. Should be set to 1 for getting the logs. Default is 0)
'logs': format(logs),
# customUptimeRatio - optional (defines the number of days to calculate
# the uptime ratio(s) for. Ex: customUptimeRatio=7-30-45 to get the
# uptime ratios for those periods)
'custom_uptime_ratios': format(uptime_ratio)
}).encode('utf-8')
url = request.Request(
url=endpoint,
data=data,
method='POST',
headers={'content-type': "application/x-www-form-urlencoded",'cache-control': "no-cache"},
)
# Verifying in the response is jsonp in otherwise is error
response = request.urlopen(url)
content = response.read().decode('utf-8')
j_content = json.loads(content)
if j_content.get('stat'):
stat = j_content.get('stat')
if stat == 'ok':
return True, j_content
return False, j_content
class CachetHq(object):
# Uptime Robot status list
UPTIME_ROBOT_PAUSED = 0
UPTIME_ROBOT_NOT_CHECKED_YET = 1
UPTIME_ROBOT_UP = 2
UPTIME_ROBOT_SEEMS_DOWN = 8
UPTIME_ROBOT_DOWN = 9
# Cachet status list
CACHET_OPERATIONAL = 1
CACHET_PERFORMANCE_ISSUES = 2
CACHET_SEEMS_DOWN = 3
CACHET_DOWN = 4
def __init__(self, cachet_api_key, cachet_url):
self.cachet_api_key = cachet_api_key
self.cachet_url = cachet_url
def update_component(self, id_component=1, status=None):
component_status = None
# Not Checked yet and Up
if status in [self.UPTIME_ROBOT_NOT_CHECKED_YET, self.UPTIME_ROBOT_UP]:
component_status = self.CACHET_OPERATIONAL
# Seems down
elif status == self.UPTIME_ROBOT_SEEMS_DOWN:
component_status = self.CACHET_SEEMS_DOWN
# Down
elif status == self.UPTIME_ROBOT_DOWN:
component_status = self.CACHET_DOWN
if component_status:
url = '{0}/api/v1/{1}/{2}'.format(
self.cachet_url,
'components',
id_component
)
data = parse.urlencode({
'status': component_status,
}).encode('utf-8')
req = request.Request(
url=url,
data=data,
method='PUT',
headers={'X-Cachet-Token': self.cachet_api_key},
)
response = request.urlopen(req)
content = response.read().decode('utf-8')
return content
def set_data_metrics(self, value, timestamp, id_metric=1):
url = '{0}/api/v1/metrics/{1}/points'.format(
self.cachet_url,
id_metric
)
data = parse.urlencode({
'value': value,
'timestamp': timestamp,
}).encode('utf-8')
req = request.Request(
url=url,
data=data,
method='POST',
headers={'X-Cachet-Token': self.cachet_api_key},
)
response = request.urlopen(req)
return json.loads(response.read().decode('utf-8'))
def get_last_metric_point(self, id_metric):
url = '{0}/api/v1/metrics/{1}/points'.format(
self.cachet_url,
id_metric
)
req = request.Request(
url=url,
method='GET',
headers={'X-Cachet-Token': self.cachet_api_key}
)
response = request.urlopen(req)
content = response.read().decode('utf-8')
last_page = json.loads(
content
).get('meta').get('pagination').get('total_pages')
url = '{0}/api/v1/metrics/{1}/points?page={2}'.format(
self.cachet_url,
id_metric,
last_page
)
req = request.Request(
url=url,
method='GET',
headers={'X-Cachet-Token': self.cachet_api_key},
)
response = request.urlopen(req)
content = response.read().decode('utf-8')
if json.loads(content).get('data'):
data = json.loads(content).get('data')[0]
else:
data = {
'created_at': datetime.now().date().strftime(
'%Y-%m-%d %H:%M:%S'
)
}
return data
class Monitor(object):
def __init__(self, monitor_list, api_key):
self.monitor_list = monitor_list
self.api_key = api_key
def send_data_to_catchet(self, monitor):
""" Posts data to Cachet API.
Data sent is the value of last `Uptime`.
"""
try:
website_config = self.monitor_list[monitor.get('url')]
except KeyError:
print('ERROR: monitor is not valid')
sys.exit(1)
cachet = CachetHq(
cachet_api_key=website_config['cachet_api_key'],
cachet_url=website_config['cachet_url'],
)
if 'component_id' in website_config:
cachet.update_component(
website_config['component_id'],
int(monitor.get('status'))
)
metric = cachet.set_data_metrics(
monitor.get('custom_uptime_ratio'),
int(time.time()),
website_config['metric_id']
)
print('Metric created: {0}'.format(metric))
def update(self):
""" Update all monitors uptime and status.
"""
uptime_robot = UptimeRobot(self.api_key)
success, response = uptime_robot.get_monitors(response_times=1)
if success:
monitors = response.get('monitors')
for monitor in monitors:
if monitor['url'] in self.monitor_list:
print('Updating monitor {0}. URL: {1}. ID: {2}'.format(
monitor['friendly_name'],
monitor['url'],
monitor['id'],
))
self.send_data_to_catchet(monitor)
else:
print('ERROR: No data was returned from UptimeMonitor')
if __name__ == "__main__":
CONFIG = configparser.ConfigParser()
CONFIG.read(sys.argv[1])
SECTIONS = CONFIG.sections()
if not SECTIONS:
print('ERROR: File path is not valid')
sys.exit(1)
UPTIME_ROBOT_API_KEY = None
MONITOR_DICT = {}
for element in SECTIONS:
if element == 'uptimeRobot':
uptime_robot_api_key = CONFIG[element]['UptimeRobotMainApiKey']
else:
MONITOR_DICT[element] = {
'cachet_api_key': CONFIG[element]['CachetApiKey'],
'cachet_url': CONFIG[element]['CachetUrl'],
'metric_id': CONFIG[element]['MetricId'],
}
if 'ComponentId' in CONFIG[element]:
MONITOR_DICT[element].update({
'component_id': CONFIG[element]['ComponentId'],
})
MONITOR = Monitor(monitor_list=MONITOR_DICT, api_key=uptime_robot_api_key)
MONITOR.update()