-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path60_rabbitmq_monitor.py
executable file
·171 lines (143 loc) · 5.96 KB
/
60_rabbitmq_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
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
#!/bin/env python
# -*- coding:utf-8 -*-
import json
import socket
import requests
import time
import platform
from requests.auth import HTTPBasicAuth
__author__ = 'jinlong'
ENDPOINT = None
IP = None
STEP = 60
PORT = 15672
USER = ""
PASSWORD = ""
FALCON_AGENT_URL = "http://127.0.0.1:8899/v1/push"
class RabbitmqMonitor(object):
keys = ('messages_ready', 'messages_unacknowledged')
rates = ('ack', 'deliver', 'deliver_get', 'publish')
ts = int(time.time())
step = 60
def __init__(self, endpoint, ip, port, user, password, falcon_url):
self.endpoint = endpoint
self.ip = '127.0.0.1'
self.port = port
self.user = user
self.password = password
self.falcon_url = falcon_url
self.auth = HTTPBasicAuth(self.user, self.password)
def call_api(self, path):
"""
调用rabbitmq api
:param path: vhosts 方便以后拓展
:return:
"""
# print 'http://{0}:{1}/api/{2}'.format(self.ip, self.port, path)
response = requests.get('http://{0}:{1}/api/{2}'.format(self.ip, self.port, path), auth=self.auth)
if response.status_code == 200:
#print response.json()
return response.json()
raise Exception("rabbit mq http api server error")
def monitor_vhosts(self):
try:
data = self.call_api("vhosts")
print data
except Exception, e:
print e
return
metric = self.__parse_data(data)
print metric
self.send_2_stdout(metric)
def list_queues(self, filters=None):
'''
List all of the RabbitMQ queues, filtered against the filters provided
in .rab.auth. See README.md for more information.
'''
queues = []
if not filters:
filters = [{}]
for queue in self.call_api('queues'):
for _filter in filters:
check = [(x, y) for x, y in queue.items() if x in _filter]
shared_items = set(_filter.items()).intersection(check)
if len(shared_items) == len(_filter):
element = {'vhost': queue['vhost'],
'queue': queue['name']}
queues.append(element)
break
self.__parse_queue_data(queues)
def __parse_queue_data(self, queues):
"""
[{'vhost': 'a', 'queue': 'b'}, {'vhost':'a', 'c'}]
:param queues:
:return:
"""
metrics = []
for queue in queues:
response = requests.get('http://{0}:{1}/api/queues/{2}/{3}'.format(self.ip, self.port, queue.get('vhost'), queue.get('queue')), auth=self.auth)
if response.status_code == 200:
messages = response.json().get('messages')
vhost = queue.get("vhost")
queue_name = queue.get("queue")
q = {"endpoint": self.endpoint, 'timestamp': self.ts, 'step': self.step, 'counterType': "GAUGE",
'metric': 'rabbitmq.messages.length', 'tags': 'vhost={0},queue={1}'.format(vhost, queue_name), 'value': messages}
metrics.append(q)
##################### network parttitions #####################################
result = requests.get('http://{0}:{1}/api/nodes'.format(self.ip, self.port), auth=self.auth)
result = [network['partitions'] for network in json.loads(result.content) if network['partitions']]
if len(result) != 0:
m = {"endpoint": self.endpoint, 'timestamp': self.ts, 'step': self.step, 'counterType': "GAUGE",
'metric': 'rabbitmq.network', 'tags': 'network_monitor', 'value': 1}
metrics.append(m)
else:
m = {"endpoint": self.endpoint, 'timestamp': self.ts, 'step': self.step, 'counterType': "GAUGE",
'metric': 'rabbitmq.network', 'tags': 'network_monitor', 'value': 0}
metrics.append(m)
##################### network parttitions #####################################
if metrics:
self.send_2_stdout(metrics)
def __parse_data(self, data):
p = []
for queue in data:
# ready and unack
msg_total = 0
for key in self.keys:
print key
q = {"endpoint": self.endpoint, 'timestamp': self.ts, 'step': self.step, 'counterType': "GAUGE",
'metric': 'rabbitmq.%s' % key, 'tags': 'collect_type=plugins,name=%s,%s' % (queue['name'], ""),
'value': int(queue[key])}
msg_total += q['value']
p.append(q)
# total
q = {"endpoint": self.endpoint, 'timestamp': self.ts, 'step': self.step, 'counterType': "GAUGE",
'metric': 'rabbitmq.messages_total', 'tags': 'collect_type=plugins,name=%s,%s' % (queue['name'], ""), 'value': msg_total}
p.append(q)
# rates
for rate in self.rates:
q = {"endpoint": self.endpoint, 'timestamp': self.ts, 'step': self.step, 'counterType': "GAUGE",
'metric': 'rabbitmq.%s_rate' % rate, 'tags': 'collect_type=plugins,name=%s,%s' % (queue['name'], "")}
try:
q['value'] = int(queue['message_stats']["%s_details" % rate]['rate'])
except Exception:
q['value'] = 0
p.append(q)
return p
def send_2_falcon(self, metric):
requests.post(self.falcon_url, json=metric)
def send_2_stdout(self, metric):
print json.dumps(metric)
def main():
if platform.system() != 'Linux':
return
global ENDPOINT
global IP
with open('/usr/local/open-falcon/agent/cfg.json') as cfgfile:
config = json.load(cfgfile)
ENDPOINT = config['hostname']
IP = config['ip']
monitor = RabbitmqMonitor(ENDPOINT, IP, PORT, USER, PASSWORD,
FALCON_AGENT_URL)
monitor.list_queues()
if __name__ == "__main__":
main()