-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrabbitmon.py
More file actions
187 lines (157 loc) · 7.24 KB
/
Copy pathrabbitmon.py
File metadata and controls
187 lines (157 loc) · 7.24 KB
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
import requests
from pprint import pprint
import arrow
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk as es_bulk
import logging
import json
import uuid
import schedule
import os
import time
import yaml
from multiprocessing.pool import ThreadPool
logging.basicConfig(format="%(asctime)s - %(name)s - [ %(levelname)s ] - [%(filename)s:%(lineno)s - %(funcName)s()] - %(message)s", level=logging.INFO)
logger = logging.getLogger(__name__)
SHOME = os.path.abspath(os.path.join(os.path.dirname(__file__)))
def load_config(path=os.path.join(SHOME, 'config.yml')):
with open(path, 'r') as f:
try:
doc = yaml.load(f)
return doc
except Exception as e:
logger.exception('Unable to open yaml config at: {}, reason: {}'.format(path, e))
config = load_config()
es = Elasticsearch(config['elasticsearch']['hosts'], **config['elasticsearch'].get('args', {}))
ES_INDEX = config.get('es_index', 'rabbitmon')
_default_blacklist_fields = [
['backing_queue_status', 'delta'],
[ 'backing_queue_status', 'target_ram_count'],
['listeners', 'socket_opts', 'linger']
]
BLACKLIST_FIELDS = config.get('blacklist_fields', {})
def delete_keys_from_dict(dict_del, the_keys):
"""
Delete the keys present in the lst_keys from the dictionary.
Loops recursively over nested dictionaries.
"""
# make sure the_keys is a set to get O(1) lookups
if type(the_keys) is not set:
the_keys = set(the_keys)
for k,v in dict_del.items():
if k in the_keys:
del dict_del[k]
if isinstance(v, dict):
delete_keys_from_dict(v, the_keys)
return dict_del
def get_es_index():
return '{}-{}'.format(ES_INDEX, arrow.utcnow().format('YYYY.MM.DD'))
class RabbitMon(object):
def __init__(self, rabbit_connection):
rc = rabbit_connection
self.conn_name = rc['name']
self.base_host = rc['host']
self.base_headers = { 'content-type': 'application/json' }
self.auth_username = rc['username']
self.auth_password = rc['password']
self.basic_auth_tuple = ( self.auth_username, self.auth_password )
def get_data(self, endpoint):
try:
response = requests.get('{base}{endpoint}'.format(base=self.base_host, endpoint=endpoint), headers=self.base_headers, auth=self.basic_auth_tuple)
if response.status_code in range(200,299):
return response.json()
else:
logger.error("Unable to request endpoint: %s, connection: %s, response: %s, status_code: %s" % (endpoint, self.conn_name, response, response.status_code))
except Exception as e:
logger.exception('Unable to request endpoint: %s for connection: %s, reason: %s' % (endpoint, self.conn_name, e))
def clusterOverview(self):
overview = self.get_data('/api/overview')
for fields in BLACKLIST_FIELDS.get('clusterOverview', []):
overview = delete_keys_from_dict(overview, fields)
overview.update({ '@timestamp': arrow.utcnow().format('YYYY-MM-DDTHH:mm:ssZ'), 'rabbit_connection': self.conn_name })
es.index(index=get_es_index(), body=overview, doc_type='cluster-overview')
logger.info('All done with clusterOverview on connection: %s' % (self.conn_name))
def connectionStats(self):
connections = self.get_data('/api/connections')
connection_ammount = len(connections)
conn_stats = {
'@timestamp': arrow.utcnow().format('YYYY-MM-DDTHH:mm:ssZ'),
'rabbit_connection': self.conn_name,
'connections_current': connection_ammount
}
es.index(index=get_es_index(), body=conn_stats, doc_type='connection-stats')
items = []
for conn in connections:
for fields in BLACKLIST_FIELDS.get('connectionStats', []):
conn = delete_keys_from_dict(conn, fields)
conn.update({
'@timestamp': arrow.utcnow().format('YYYY-MM-DDTHH:mm:ssZ'),
'rabbit_connection': self.conn_name,
'_index': get_es_index(),
'_type': 'connection-stats'
})
items.append(conn)
indexit = es_bulk(es, items)
logger.info("All done with connectionStats on connection: %s, items_inserted: %s, errors: %s" % (self.conn_name, indexit[0], indexit[1]))
def consumerStats(self):
consumers = self.get_data('/api/consumers')
consumer_count = len(consumers)
consumer_stats = {
'@timestamp': arrow.utcnow().format('YYYY-MM-DDTHH:mm:ssZ'),
'rabbit_connection': self.conn_name,
'consumers_current': consumer_count
}
es.index(index=get_es_index(), body=consumer_stats, doc_type='consumer-stats')
items = []
for consumer in consumers:
for fields in BLACKLIST_FIELDS.get('consumerStats', []):
consumer = delete_keys_from_dict(consumer, fields)
consumer.update({
'@timestamp': arrow.utcnow().format('YYYY-MM-DDTHH:mm:ssZ'),
'rabbit_connection': self.conn_name,
'_index': get_es_index(),
'_type': 'consumer-stats'
})
items.append(consumer)
indexit = es_bulk(es, items)
logger.info("All done with consumerStats on connection: %s, items_inserted: %s, errors: %s" % (self.conn_name, indexit[0], indexit[1]))
def nodeStats(self):
nodes = self.get_data('/api/nodes')
if len(nodes) > 0:
for node in nodes:
for fields in BLACKLIST_FIELDS.get('nodeStats', []):
node = delete_keys_from_dict(node, fields)
node.update({ '@timestamp': arrow.utcnow().format('YYYY-MM-DDTHH:mm:ssZ'), 'rabbit_connection': self.conn_name })
es.index(index=get_es_index(), body=node, doc_type='node-stats')
logger.info('All done with nodeStats on connection: %s' % (self.conn_name))
def queueStats(self):
queues = self.get_data('/api/queues')
if queues is not None:
items = []
for queue in queues:
for fields in BLACKLIST_FIELDS.get('queueStats', []):
delete_keys_from_dict(queue, fields)
es_stuff = {
'@timestamp': arrow.utcnow().format('YYYY-MM-DDTHH:mm:ssZ'),
'rabbit_connection': self.conn_name,
'_index': get_es_index(),
'_type': 'queue-stats',
}
es_stuff.update(queue)
items.append(es_stuff)
indexit = es_bulk(es, items)
logger.info("All done with queueStats on connection: %s, items_inserted: %s, errors: %s" % (self.conn_name, indexit[0], indexit[1]))
def worker(rabbit_connection):
rm = RabbitMon(rabbit_connection)
for mod in config.get('enabled_modules', ['clusterOverview', 'nodeStats', 'queueStats']):
eval('rm.%s()' % (mod))
def main():
pool = ThreadPool(processes=3)
pool = ThreadPool()
pool.map(worker, config['rabbit_connections'])
pool.close()
pool.join()
if __name__ == '__main__':
while True:
main()
time.sleep(config.get('scrape_interval', 20))