Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RabbitMQ Management Plugin Check #330

Merged
merged 3 commits into from
Jan 8, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions checks.d/rabbitmq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do from util import json instead it will support more version of python because we handle simplejson, stdlib json, etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I also noticed there is checks.libs.httplib2 is this preferred over urllib2?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using urllib2 here is fine. We used httplib2 for one check so that timeouts would be compatible with versions >= 2.4, but that's not needed in this case, as far as I can tell.

import urllib2
import urlparse

from checks import AgentCheck


class RabbitMQ(AgentCheck):
"""This check is for gathering statistics from the RabbitMQ
Management Plugin (http://www.rabbitmq.com/management.html)
"""
def check(self, instance):
# make sure 'rabbitmq_api_url; is present
if 'rabbitmq_api_url' not in instance:
self.log.info('Skipping instance "rabbitmq_api_url" not found')
return

# get parameters
base_url = instance['rabbitmq_api_url']
username = instance.get('rabbitmq_user', 'guest')
password = instance.get('rabbitmq_pass', 'guest')

# setup urllib2 for Basic Auth
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm='RabbitMQ Management', uri=base_url, user=username, passwd=password)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)

self.get_queue_stats(base_url)
self.get_node_stats(base_url)

def get_queue_stats(self, base_url):
url = urlparse.urljoin(base_url, 'queues')
stats = []
try:
stats = json.loads(urllib2.urlopen(url).read())
except urllib2.URLError, e:
self.log.info('Cannot open RabbitMQ API url: %s', url)
except ValueError, e:
self.log.info('Cannot parse JSON response from API url: %s', url)

for node in stats:
tags = []
tags.append('rabbitmq_node:%s' % node['node'])
tags.append('rabbitmq_queue:%s' % node['name'])
tags.append('rabbitmq_vhost:%s' % node['vhost'])
tags.append('rabbitmq_policy:%s' % node['policy'])

self.gauge('rabbitmq.queue.active_consumers', int(node['active_consumers']), tags=tags)
self.gauge('rabbitmq.queue.consumers', int(node['consumers']), tags=tags)
self.gauge('rabbitmq.queue.memory', int(node['memory']), tags=tags)
self.gauge('rabbitmq.queue.messages', int(node['messages']), tags=tags)
self.gauge('rabbitmq.queue.messages_ready', int(node['messages_ready']), tags=tags)
self.gauge('rabbitmq.queue.messages_unacknowledged', int(node['messages_unacknowledged']), tags=tags)

def get_node_stats(self, base_url):
url = urlparse.urljoin(base_url, 'nodes')
stats = []
try:
stats = json.loads(urllib2.urlopen(url).read())
except urllib2.URLError, e:
self.log.info('Cannot open RabbitMQ API url: %s', url)
except ValueError, e:
self.log.info('Cannot parse JSON response from API url: %s', url)

for node in stats:
tags = []
tags.append('rabbitmq_node:%s' % node['name'])

self.gauge('rabbitmq.node.disk_free', int(node['disk_free']), tags=tags)
self.gauge('rabbitmq.node.disk_free_limit', int(node['disk_free_limit']), tags=tags)
self.gauge('rabbitmq.node.fd_total', int(node['fd_total']), tags=tags)
self.gauge('rabbitmq.node.fd_used', int(node['fd_used']), tags=tags)
self.gauge('rabbitmq.node.mem_limit', int(node['mem_limit']), tags=tags)
self.gauge('rabbitmq.node.mem_used', int(node['mem_used']), tags=tags)
self.gauge('rabbitmq.node.proc_total', int(node['proc_total']), tags=tags)
self.gauge('rabbitmq.node.proc_used', int(node['proc_used']), tags=tags)
self.gauge('rabbitmq.node.processors', int(node['processors']), tags=tags)
self.gauge('rabbitmq.node.run_queue', int(node['run_queue']), tags=tags)
self.gauge('rabbitmq.node.sockets_total', int(node['sockets_total']), tags=tags)
self.gauge('rabbitmq.node.sockets_used', int(node['sockets_used']), tags=tags)
10 changes: 10 additions & 0 deletions conf.d/rabbitmq.yaml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
init_config:

instances:
# for every instance a 'rabbitmq_api_url' must be provided, pointing to the api
# url of the RabbitMQ Managment Plugin (http://www.rabbitmq.com/management.html)
# optional: 'rabbitmq_user' (default: guest) and 'rabbitmq_pass' (default: guest)

- rabbitmq_api_url: http://localhost:15672/api/
rabbitmq_user: guest
rabbitmq_pass: guest