Pika
RabbitMQ is a message queuing system. A python tool called Pika is able to communicate with RabbitMQ and can prove useful for retrieving messages from error queues.
PRE-REQUISITE
You will need to install Pika. One of the easiest ways to do with is via pip.
INSTALLATION
To install pika you will need a python environment installed with pip. With pip installed you can simply run the following command :-
pip install pika
USAGE
In this example we are going to provide the code to extract all messages from an ERROR_QUEUE and compress them inside a gzip container. To execute it copy the code below and save into a file called pull_messages.py.
python pull_messages.py
————-——--CODE—————-—--
————-—---CODE———————--
Once you see all of the required messages have been consumed from the RabbitMQ ERROR_QUEUE you can then press CTRL + C to exit the python script. A file called ERROR_QUEUE.gz will now exist in the current directory. You can simply extract the file or read it with a tool like zcat and less.
zcat ERROR_QUEUE.gz | less
SCRIPT EXPLANATION
Import both gzip and pika.
Build variables containing the credentials and host details.
Create the connection and channel.
Define the call (in this case we want the message and all of the properties of the message) and destination file.
Create a variable containing the queue details.
Consume from the queue only stopping when there is a keyboard interupt.