forked from roverdotcom/alooma-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.py
99 lines (73 loc) · 2.21 KB
/
monitor.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
import alooma
import datadog
import datetime
import os
import time
from api_config import api
DATADOG_API_KEY = os.environ.get('DATADOG_API_KEY')
MINUTES_SLEEP = int(os.environ.get('SLEEP_INTERVAL_MINUTES', '10'))
SECONDS_SLEEP = MINUTES_SLEEP * 60
datadog.initialize(api_key=DATADOG_API_KEY)
def posix_timestamp():
d = datetime.datetime.now()
return str(int(time.mktime(d.timetuple())))
def send_metric(data):
for d in data:
metric_name = "alooma.{}".format(d['target'].lower())
values = d['datapoints']
for x in values:
x.reverse()
for v in values:
v[0] = str(v[0])
if v[1] is None:
print "Value is None. Not sending."
continue
v[1] = float(v[1])
values = [tuple(x) for x in values]
print metric_name
print values
for v in values:
if v[1] is None:
continue
print v
result = datadog.api.Metric.send(
metric=metric_name,
points=v,
type='gauge',
)
yield result
metrics = alooma.METRICS_LIST
def record_metric(m):
data = api.get_metrics_by_names(m, MINUTES_SLEEP)
print "Sending {}".format(m)
result = send_metric(data)
for r in result:
import pprint; pprint.pprint(r)
def record_all_metrics():
for m in metrics:
record_metric(m)
def record_num_inputs():
inputs = api.get_inputs()
num_inputs = len(inputs)
result = datadog.api.Metric.send(
metric='alooma.num_inputs',
points=[(posix_timestamp(), num_inputs)],
type='gauge',
)
def record_restream_stats():
restream_queue_size = api.get_restream_queue_size()
datadog.api.Metric.send(
metric='alooma.restreamable_events',
points=[(posix_timestamp(), restream_queue_size)],
type='gauge',
)
if __name__ == '__main__':
while True:
try:
record_all_metrics()
record_num_inputs()
record_restream_stats()
except Exception as e:
print e
print "Monitor process sleeping for {}m".format(MINUTES_SLEEP)
time.sleep(SECONDS_SLEEP)