forked from JavDD/dogstatd-rabbitmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
144 lines (117 loc) · 4.88 KB
/
main.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
import pika
import time
from datadog import initialize, statsd
import json
from configparser import ConfigParser
from amqpstorm import management
import signal
import logging
class GracefulKiller:
kill_now = False
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self, *args):
self.kill_now = True
logging.basicConfig(filename='logger.log', encoding='utf-8', format='%(levelname)s - %(asctime)s: %(message)s',datefmt='%Y-%m-%d %H:%M:%S',level=logging.INFO)
logging.info('service has been started')
config = ConfigParser()
config.read('configuration.ini')
host = config.get('variables','host')
port = config.get('variables','port')
virtual_host = config.get('variables','virtual_host')
queue_lists = config.get('queues','names').split(',')
# print(queue_lists)
env_lists = config.get('queues','env').split(',')
username = config.get('credentials','username')
password = config.get('credentials','password')
# print(username)
# print(password)
# print(env_lists)
options = {
'statsd_host':'localhost',
'statsd_port':8125
}
initialize(**options)
class consume_queue:
def __init__(self,queue,channel,env):
self.queue = queue
self.channel = channel
self.env = env
channel.basic_consume(queue=self.queue, on_message_callback=self.callback, auto_ack=True)
def callback(self,ch, method, properties, body):
message = body.decode('utf8').replace("'", '"')
if message.count('measurement') == 1:
if self.is_json(message):
data = json.loads(message)
for b in data:
metric_name = b['measurement']
metric_value = b['fields']['value']
tags = b['tags']
tags_list=[]
for key,value in tags.items():
tags_list.append(f'{key}:{value}')
tags_list.append(f'enviornment:{self.env}')
# tags_value=tags_list.join(',')
statsd.gauge(metric_name,float(metric_value),tags=tags_list)
# print(metric_name,float(metric_value),type(tags))
else:
messages=message.replace(']','];')
msgs = messages.split(';')
for m in msgs:
if '[{' in m:
if self.is_json(m):
data = json.loads(m)
for b in data:
metric_name = b['measurement']
metric_value = b['fields']['value']
tags = b['tags']
tags_list=[]
for key,value in tags.items():
tags_list.append(f'{key}:{value}')
tags_list.append(f'enviornment:{self.env}')
statsd.gauge(metric_name,float(metric_value),tags=tags_list)
# print(metric_name,float(metric_value),type(tags))
def is_json(self,myjson):
try:
json.loads(myjson)
except ValueError as e:
return False
return True
def on_open(connection):
connection.channel(on_open_callback=on_channel_open)
def on_channel_open(channel):
global queue_lists,env_lists
for i,a in enumerate(queue_lists):
consume_queue(a,channel,env_lists[i])
def on_close(connection):
connection.channel(on_close_callback=on_channel_close)
def on_channel_close(channel):
log_channel_close(channel)
credentials = pika.PlainCredentials('rmqstatd',':NGg6^RQ_woOhPWg=g')
connection = pika.SelectConnection(
pika.ConnectionParameters(str(host),int(port),str(virtual_host),credentials,heartbeat=30),on_open_callback=on_open)
try:
connection.ioloop.start()
except KeyboardInterrupt:
logging.warning('Script interrupted manually')
connection.close()
def log_channel_close(channel):
logging.warning('Script channel is closed, messages are not logged')
print('rabbitmq channel is closed')
if __name__ == '__main__':
killer = GracefulKiller()
while not killer.kill_now:
API = management.ManagementApi(f'{host}:{port}', username,
password, verify=True)
try:
result = API.aliveness_test(virtual_host)
if result['status'] == 'ok':
islive = True
else:
logging.warning('RabbitMQ is not alive! :(')
except management.ApiConnectionError as why:
logging.warning('Connection Error: %s' % why)
except management.ApiError as why:
logging.warning('ApiError: %s' % why)
logging.info('service has been stopped')