4

How can I programmatically, using Python code, list current queues created on a RabbitMQ broker and the number of workers connected to them? It would be the equivalent to:

rabbitmqctl list_queues name consumers
2
  • 3
    There are no standard way to get such information like created queues list with AMQP protocol, but I guess HTTP API from management plugin will fits any of your needs. Commented Jun 12, 2014 at 9:11
  • 1
    as @zaq178miami I suggest the http API, if you use a command line you could have permission problems, and you can execute the command only where RMQ is installed. Commented Jun 13, 2014 at 12:57

4 Answers 4

5

I do it this way and display all the queues and their details (messages ready, unacknowledged etc.) on a web page -

import kombu
conn = kombu.Connection(broker_url)# example 'amqp://guest:guest@localhost:5672/'
conn.connect()
client = conn.get_manager()
queues = client.get_queues('/')#assuming vhost as '/'

You will need kombu to be installed and queues will be a dictionary with keys representing the queue names. I think I got this when digging through the code of celery flower (The tool used for monitoring celery).

Update: As pointed out by @zaq178miami, you will also need the management plugin that has the http API. I had forgotten that I had enabled than in rabbitmq.

Sign up to request clarification or add additional context in comments.

4 Comments

When I try to call client.get_queues('/') I get this error message: File "/home/danielmaxx/.virtualenvs/rendit/local/lib/python2.7/site-packages/pyrabbit/http.py", line 104, in do_call raise NetworkError("Error: %s %s" % (type(out), out)) pyrabbit.http.NetworkError: Error: <class 'socket.error'> [Errno 111] Connection refused
@danielmaxx, you will need the management plugin for rabbit mq as posted in the update in my answer. The management plugin provides the http APIs that are used by pyrabbit. Once you install that you won't get the error you've mentioned.
I did install it, but still fails. Maybe has to do with permissions?
Not sure. I've run into the Connection refused error before. After digging to code of pyrabbit I was able to track it down to the http API access and installing the plugin solved it for me. I see that you have posted another answer that was provide by @asksol. Did that work for you?
1

This way did it for me:

def get_queue_info(queue_name):
    with celery.broker_connection() as conn:
        with conn.channel() as channel:
            return channel.queue_declare(queue_name, passive=True)

This will return a namedtuple with the name, number of messages waiting and consumers of that queue.

ksrini answer is correct too and can be used when you require more information about a queue.

Thanks to Ask Solem who gave me the hint.

Comments

0

As a rabbitmq client you can use pika. However it doesn't have option for list_queues. The easiest solution would be calling rabbitmqctl command from python using subprocess:

import subprocess
command = "/usr/local/sbin/rabbitmqctl list_queues name consumers"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
print process.communicate()

2 Comments

This is the solution I had and it's not bad. The only problem I have is this is a sudo command and I would be executing it from Django. Can I do that without running Django in sudo mode?
You can give permissions to run rabbitmqctl for user which is running your django app and avoid calling sudo.
0

I would use simply this: Just replace the user(default= guest), passwd(default= guest) and port with your values.

import requests
import json

def call_rabbitmq_api(host, port, user, passwd):
  url = 'https://%s:%s/api/queues' % (host, port)
  r = requests.get(url, auth=(user,passwd),verify=False)
  return r

def get_queue_name(json_list):
  res = []
  for json in json_list:
    res.append(json["name"])
  return res

if __name__ == '__main__':
  host = 'rabbitmq_host'
  port = 55672
  user = 'guest'
  passwd = 'guest'
  res = call_rabbitmq_api(host, port, user, passwd)
  print ("--- dump json ---")
  print (json.dumps(res.json(), indent=4))
  print ("--- get queue name ---")
  q_name = get_queue_name(res.json())
  print (q_name)

Referred from here: https://gist.github.com/hiroakis/5088513#file-example_rabbitmq_api-py-L2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.