-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgraphite_rabbitmq_publish.py
executable file
·77 lines (64 loc) · 2.41 KB
/
graphite_rabbitmq_publish.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
#!/usr/bin/env python
import time
import sys
import amqplib.client_0_8 as amqp
from graphite_rabbitmq_config import RABBITMQ_BROKER_DATA, \
GRAPHITE_EXCHANGE, GRAPHITE_ROUTING_KEY
class GraphiteRabbitMQPublisher:
def __init__(self, rabbitmq_broker_data=RABBITMQ_BROKER_DATA,
exchange=GRAPHITE_EXCHANGE,
routing_key=GRAPHITE_ROUTING_KEY):
self.rabbitmq_broker_data = rabbitmq_broker_data
self.exchange = exchange
self.routing_key=routing_key
self.__channel = None
def channel(self):
if self.__channel is None:
self.conn = amqp.Connection(**self.rabbitmq_broker_data)
self.__channel = self.conn.channel()
return self.__channel
def publish(self, data, **defaults):
"""
data can be a dict {metric:value, ...}
data can be a list ["metric value", "value", "metric value timestamp", ...]
defaults can include timestamp and metric
"""
try: defaults['timestamp']
except KeyError: defaults['timestamp'] = int(time.time())
payload_lines = [ ]
if type(data) == dict:
for k in data:
payload_lines.append("%s %s %d" % (k, str(data[k]),
defaults['timestamp']))
elif type(data) == list:
for k in data:
parts = str(k).strip().split()
m = None # metric
v = None # value
t = None # timestamp
if len(parts) == 1:
m = defaults['metric']
v = k
t = defaults['timestamp']
elif len(parts) == 2:
m,v = parts[:2]
t = defaults['timestamp']
elif len(parts) == 3:
m, v, t = parts
else:
raise ArgumentError, "bad line: %s" % k
payload_lines.append("%s %s %d" % (m, v, t))
elif type(data) == str:
if not len(data.strip().split()) == 3:
raise ArgumentError, "bad line %s" % data
payload_lines.append(data)
self.channel().basic_publish(
amqp.Message("\n".join(payload_lines), delivery_mode=2),
exchange=self.exchange, routing_key=self.routing_key)
if __name__ == '__main__':
try:
assert len(sys.argv[1:]) > 0, "Nothing to send"
GraphiteRabbitMQPublisher().publish(sys.argv[1:])
except Exception, e:
print "Error: %s" % str(e)
print "Usage: %s 'metric value timestamp' ..." % sys.argv[0]