/
watcher.py
327 lines (248 loc) · 10.4 KB
/
watcher.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import os
import sys
import re
import errno
import signal
import socket
import subprocess
import datetime
import logging
import pipes
import tornado.ioloop
STAT_OK = "ok"
STAT_WARNING = "warning"
STAT_CRITICAL = "critical"
DEFAULT_INTERVAL = 2000 # In milliseconds
log = logging.getLogger('pharos.watcher')
def plural_it(value, singular, plural):
return singular if value == 1 else plural
def format_timedelta(delta):
secs = delta.seconds
days = delta.days
if days:
return ' '.join((str(days), plural_it(days, "day", "days")))
if secs >= 60*60:
num_hours = secs // (60*60)
return ' '.join((str(num_hours), plural_it(num_hours, "hour", "hours")))
if secs >= 60:
num_mins = secs // 60
return ' '.join((str(num_mins), plural_it(num_mins, "minute", "minutes")))
return "a moment"
class WatcherSet(object):
def __init__(self, name, watchers):
self.name = name
self.watchers = watchers
class MetricWatcher(object):
"""base interface for what pharos will use to collect and monitor metrics"""
def __init__(self, name=None):
self.name = name
self._callbacks = []
@property
def id(self):
"""Build an id for this metric
We'll build an id based on the name by stripping out whitespace and non-alphanumeric characters
"""
id_str = re.sub(r'[\s]', '_', self.name.lower())
id_str = re.sub(r'[^\w]', '', id_str)
return id_str
@property
def status(self):
"""Returns one of our STAT_* constants to indicate the how this metric is doing"""
raise 'whaaa'
return STAT_OK
@property
def last_updated(self):
"""Indicate the last time this metric was updated.
Should return a datetime instances or None if not updated
"""
return None
@property
def duration(self):
"""Returns a value indicating how long this metric has been under the current status value
Return value is a datetime.timedelta instance
"""
return datetime.timedelta(secs=0)
@property
def value(self):
"""Provide the actual value for this metric for display"""
return None
@property
def detail(self):
"""Provide more details for drilldown in this stat"""
return None
def add_update_callback(self, callback):
"""Calls function 'callback' on the next update to this metric"""
self._callbacks.append(callback)
def _callback(self):
while self._callbacks:
try:
self._callbacks.pop()()
except Exception, e:
log.exception("Error calling callback")
def add_to_loop(self, io_loop=None):
"""Do whatever is necessary to set this metric watcher up to actually collect data
The io_loop is the tornado IOLoop instance, if None it will use the standard io loop global.
"""
pass
class CommandMetricWatcher(MetricWatcher):
"""Metric watcher that executes a command periodically
All this base class does is monitor the return value but sub-classes can easily extend it to do something with
the commands output.
"""
def __init__(self, *args, **kwargs):
self._command = kwargs.pop('command', None)
self._interval = kwargs.pop('interval', DEFAULT_INTERVAL)
super(CommandMetricWatcher, self).__init__(*args, **kwargs)
self._last_updated = None
self._returncode = None
self._status_start = None
self._status = None
self._metric_checker = None
self.set_status(STAT_OK)
@property
def command(self):
return self._command
@property
def last_updated(self):
return self._last_updated
@property
def status(self):
return self._status
@property
def value(self):
return self._returncode
@property
def duration(self):
if self._status_start is None:
return datetime.timedelta(0)
else:
return datetime.datetime.now() - self._status_start
def set_updated(self):
self._last_updated = datetime.datetime.now()
def set_status(self, status):
log.debug("Setting status for %s : %s", self.name, status)
self._callback()
if self._status != status:
self._status = status
self._status_start = datetime.datetime.now()
def _handle_output(self, output):
pass
def _handle_returncode(self, returncode):
self._returncode = returncode
if returncode == 0:
self.set_status(STAT_OK)
else:
self.set_status(STAT_CRITICAL)
self.set_updated()
def handle_exit(self):
assert self._metric_checker, "we should have an open checker, or you shouldn't have called me."
returncode = self._metric_checker.returncode
assert returncode is not None, "Process should have exited, or else why are we here"
log.debug("Process %d exited with value %d", self._metric_checker.pid, returncode)
self._metric_checker = None
self._handle_returncode(returncode)
def add_to_loop(self, io_loop=None):
# There is some amount of complexity in here that is worth explaining. What we're going to setup here
# is a periodic callback in our tornado io loop that will start a process, monitor for output and process
# the process's exit. It makes use of closures because we need internal state, but the callbacks don't have
# the luxury of arguments.
#
# There should be only one process running at once. If it hangs or does something stupid we're not going to start
# another, but we will complain in the logs.
def handle_interval():
"""Callback for our PeriodicCallback interval.
The only important *state* in this closure is 'self', which wouldn't be available normally.
"""
if self._metric_checker is not None:
if self._metric_checker.poll() is not None:
self.handle_exit()
else:
log.warning("Interval while process is still running (pid %d)", self._metric_checker.pid)
return
checker = self._metric_checker = subprocess.Popen(self.command, shell=True, stdout=subprocess.PIPE)
log.debug("Started process %d : %s", checker.pid, self.command)
def check_for_exit():
if checker.poll() is not None:
io_loop.remove_handler(checker.stdout.fileno())
self.handle_exit()
# Mmm.. another closure. This one is for our event processing
def handle_events(fd, events):
# We need to make sure we still care about the output from this checker
if self._metric_checker is None or fd != self._metric_checker.stdout.fileno():
log.warning("IO handler was still active for fd %d", fd)
io_loop.remove_handler(fd)
return
assert self._metric_checker
if events & io_loop.READ:
output = checker.stdout.read()
if len(output) == 0:
# On OSX this seems to be what happens rather than receiving an error....strange
#log.warning("Got no output from %d ?", fd)
check_for_exit()
else:
self._handle_output(output)
if events & io_loop.ERROR:
# We're definetly over this fd, but the process may not have actually exited.
# So we'll remove it from the io loop, and if exit was delayed it will get cleaned up
# on the next interval. In practice this should rarely happen.
io_loop.remove_handler(fd)
check_for_exit()
io_loop.add_handler(checker.stdout.fileno(), handle_events, io_loop.READ)
tornado.ioloop.PeriodicCallback(handle_interval, self._interval, io_loop=io_loop).start()
class PageGETMetricWatcher(CommandMetricWatcher):
CURL_COMMAND = "curl -w \"%%{time_connect} %%{time_starttransfer} %%{time_total} (%%{http_code})\" -s -o /dev/null %(url)s"
SSH_COMMAND = "ssh %(hostname)s %(command)s"
def __init__(self, *args, **kwargs):
self._url = kwargs.pop('url', None)
self._ssh_host = kwargs.pop('ssh_host', None)
self._thresholds = kwargs.pop('thresholds', None)
self._value = None
self._output = None
super(PageGETMetricWatcher, self).__init__(*args, **kwargs)
@property
def command(self):
out_cmd = self.CURL_COMMAND % {'url': self._url}
if self._ssh_host:
out_cmd = self.SSH_COMMAND % {'hostname': self._ssh_host, 'command': pipes.quote(out_cmd)}
return out_cmd
def _handle_output(self, output):
log.debug("Received %r", output)
super(PageGETMetricWatcher, self)._handle_output(output)
self._output = output
values = self._output.split()
if len(values) != 4:
self.set_status(STAT_CRITICAL)
return
connect_time, first_byte_time, total_time, status = values
self._value = total_time = float(total_time)
if status != '(200)':
self.set_status(STAT_CRITICAL)
return
if self._thresholds is None:
raise Exception('no thresholds')
return
min_val, warn_val, crit_val = self._thresholds
if total_time <= min_val:
self.set_status(STAT_WARNING)
return
if total_time >= crit_val:
self.set_status(STAT_CRITICAL)
return
if total_time >= warn_val:
self.set_status(STAT_WARNING)
return
self.set_status(STAT_OK)
def _handle_returncode(self, returncode):
if returncode != 0:
self.set_status(STAT_CRITICAL)
self.set_updated()
self._returncode = returncode
@property
def value(self):
if self._value is None:
return "unknown"
else:
return "%.4f" % self._value
@property
def detail(self):
return self._output