Skip to content

Instantly share code, notes, and snippets.

@akshendra
Last active June 12, 2018 13:56
Show Gist options
  • Save akshendra/4858e2c00c3cc2a4971ecaefd82384c9 to your computer and use it in GitHub Desktop.
Save akshendra/4858e2c00c3cc2a4971ecaefd82384c9 to your computer and use it in GitHub Desktop.
Send RabbitMQ queues data to cloudwatch
#!/usr/bin/env python3
'''
Send rabbitmq queue data to cloudwatch
'''
import boto3
import os
import json
import requests
queues_to_monitor = os.environ['MONITOR_QUEUES'].split(',')
rabbit_user = os.environ['RABBIT_USER']
rabbit_password = os.environ['RABBIT_PASSWORD']
machine = os.environ['MACHINE_NAME']
region = os.environ['AWS_REGION']
cloudwatch = boto3.client('cloudwatch')
def extract(data):
"""Extract useful data"""
name = data['name']
return {
'name': name,
'info': {
'PublishRate': data['message_stats']['publish_details']['rate'] if 'message_stats' in data else 0,
'Ready': data['messages_ready_ram'],
'Unacknowledged': data['messages_unacknowledged_ram'],
'Total': data['messages_ram'],
}
}
def get_queues_info(host, port, username, password):
url = 'http://{}:{}/api/queues'.format(host, port)
response = requests.get(url, auth=(username, password))
data = json.loads(response.text)
queues = [extract(queue) for queue in data]
return queues
def make_matrix(name, queue, value, unit):
return {
'MetricName': name,
'Dimensions': [
{
'Name': 'Machine',
'Value': machine
},
{
'Name': 'Queue',
'Value': queue
},
],
'Value': value,
'Unit': unit,
}
def send_multi_metrics(metrics, unit='Count',
namespace='EC2/Rabbit'):
data = []
for metric in metrics:
queue = metric['name']
if queue not in queues_to_monitor:
continue
print(queue)
info = metric['info']
print(info['PublishRate'])
for key, value in info.items():
data.append(make_matrix(key, queue, value, unit))
if (len(data) > 0):
cloudwatch.put_metric_data(
Namespace=namespace,
MetricData=data
)
if __name__ == '__main__':
rabbit_data = get_queues_info('localhost', 15672, rabbit_user, rabbit_password)
length = len(rabbit_data)
index = 0
while index < length:
start = index
end = index + 5 if index + 5 <= length else length
data = rabbit_data[start:end]
send_multi_metrics(data)
index = index + 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment