This repository has been archived by the owner on Sep 16, 2018. It is now read-only.
/
metricmon.py
69 lines (56 loc) · 1.73 KB
/
metricmon.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
#!/usr/bin/env python
import checks
import json
import logging
import pika
import threading
import sched
import time
PERIOD = 30
START_PERIOD = 5
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
def connectMq():
rabbitmq = json.loads(
open('/etc/sensu/conf.d/rabbitmq.json', 'r').read())['rabbitmq']
credentials = pika.PlainCredentials(rabbitmq['user'], rabbitmq['password'])
connection = pika.BlockingConnection(
pika.ConnectionParameters(rabbitmq['host'],
virtual_host=rabbitmq['vhost'], credentials=credentials))
return connection.channel()
def keepalive():
keepalive = {
'name': 'metricmon',
'timestamp': int(time.time())
}
logging.debug('Sending keepalive %s', keepalive)
mq.basic_publish(exchange='', routing_key='keepalives', body=json.dumps(keepalive))
def pollMetrics():
scheduler.enter(PERIOD, 1, pollMetrics, ())
keepalive()
logging.debug('Polling metrics ..')
results = checker.run()
if not results:
return
logging.info('Publishing %d results', len(results))
for (target, instance), check, code, message in results:
result = {
'client': 'metricmon',
'check': {
'name': '%s-%s-%s' % (target, instance, check),
'issued': int(time.time()),
'output': message,
'status': code,
'standalone': True
}
}
logging.debug('Publishing: %s', result)
mq.basic_publish(exchange='', routing_key='results',
body=json.dumps(result))
if __name__ == '__main__':
mq = connectMq()
checker = checks.Checks()
scheduler = sched.scheduler(time.time, time.sleep)
scheduler.enter(START_PERIOD, 1, pollMetrics, ())
keepalive()
scheduler.run()
connection.close()