-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
86 lines (76 loc) · 2.37 KB
/
app.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
import requests
from requests.compat import urljoin
from requests.auth import HTTPBasicAuth
import boto3
import os
from utils.logging import logger as log
client = boto3.client('cloudwatch')
if 'RABBIT_MQ_URL' not in os.environ:
URL = 'http://localhost:15672'
log.info("Using default Admin URL %s", URL)
else:
URL = os.environ['RABBITMQ_URL']
if 'RABBIT_MQ_USERNAME' not in os.environ:
USERNAME = 'guest'
log.info("Using default USERNAME %s", USERNAME)
else:
USERNAME = os.environ['RABBITMQ_USERNAME']
if 'RABBIT_MQ_PASSWORD' not in os.environ:
PASSWORD = 'guest'
log.info("Using default PASSWORD")
else:
PASSWORD = os.environ['PASSWORD']
requestURL = urljoin(URL, '/api/queues')
def put_metric(metrics):
client.put_metric_data(
Namespace='Resource/RabbitMQ',
MetricData=metrics
)
if __name__ == '__main__':
total_messages = 0
resp = requests.get(requestURL, auth=HTTPBasicAuth(USERNAME, PASSWORD))
if resp.status_code == 200:
queues = resp.json()
count = 0
metrics = []
for q in queues:
messages = q['messages']
name = q['name']
count = count + 1
if count % 10 == 0:
put_metric(metrics)
metrics = []
count = 0
metrics.append({
'MetricName': name,
'Dimensions': [
{
'Name': 'Resource',
'Value': 'RabbitMQ'
},
],
'Value': messages,
'Unit': 'Count',
'StorageResolution': 60
})
total_messages = total_messages + messages
if len(metrics) > 0:
put_metric(metrics)
put_metric([
{
'MetricName': 'TotalMessages',
'Dimensions': [
{
'Name': 'Resource',
'Value': 'RabbitMQ'
},
],
'Value': total_messages,
'Unit': 'Count',
'StorageResolution': 60
},
]
)
log.info("Total messages: %s", total_messages)
else:
log.error('error', resp.status_code)