RabbitMQ Command Line
Introduction
RabbitMQ is a powerful message broker that implements the Advanced Message Queuing Protocol (AMQP). While RabbitMQ offers a web-based management UI, the command-line tools provide more flexibility and automation capabilities for managing your RabbitMQ server. This guide will walk you through the essential command-line tools that come with RabbitMQ, helping you become proficient in managing your messaging infrastructure from the terminal.
Prerequisites
Before diving into RabbitMQ command-line tools, ensure you have:
- RabbitMQ server installed
- Command-line access to the server where RabbitMQ is running
- Basic understanding of RabbitMQ concepts (queues, exchanges, bindings)
RabbitMQ Command-Line Tools Overview
RabbitMQ provides several command-line tools, but the most commonly used ones are:
Let's explore each of these tools in detail.
The rabbitmqctl
Command
The rabbitmqctl
is the primary command-line tool for managing RabbitMQ nodes. It allows you to perform node management, user management, policy management, and monitoring operations.
Basic Syntax
rabbitmqctl [options] command [command_options]
Common Commands
1. Node Management
Check node status:
rabbitmqctl status
Example output:
Status of node rabbit@hostname ...
Runtime
OS PID: 12345
OS: Linux
Uptime (seconds): 3600
RabbitMQ version: 3.11.2
...
Stop the RabbitMQ application but leave the Erlang node running:
rabbitmqctl stop_app
Start the RabbitMQ application:
rabbitmqctl start_app
Reset a node to its virgin state (delete all data):
rabbitmqctl reset
2. User Management
List all users:
rabbitmqctl list_users
Example output:
Listing users ...
user tags
guest [administrator]
Add a new user:
rabbitmqctl add_user username password
Set user tags (roles):
rabbitmqctl set_user_tags username administrator
Set permissions for a user:
rabbitmqctl set_permissions -p vhost username ".*" ".*" ".*"
This grants the user configure, write, and read permissions on all resources in the specified vhost.
3. Queue Management
List queues:
rabbitmqctl list_queues [options]
Example output:
Listing queues ...
name messages
tasks 15
notifications 3
For more detailed information:
rabbitmqctl list_queues name messages consumers memory state
Purge a queue:
rabbitmqctl purge_queue queue_name
4. Exchange and Binding Management
List exchanges:
rabbitmqctl list_exchanges
Example output:
Listing exchanges ...
name type
amq.direct direct
amq.fanout fanout
amq.topic topic
...
List bindings:
rabbitmqctl list_bindings
The rabbitmq-diagnostics
Command
The rabbitmq-diagnostics
tool is used for diagnosing and health checking a RabbitMQ node.
Basic Syntax
rabbitmq-diagnostics [options] command [command_options]
Common Commands
Check if the RabbitMQ server is running:
rabbitmq-diagnostics ping
Example output:
Ping succeeded
Get a complete health check:
rabbitmq-diagnostics check_running
rabbitmq-diagnostics check_port_connectivity
rabbitmq-diagnostics check_virtual_hosts
Get memory usage report:
rabbitmq-diagnostics memory_breakdown
Example output:
Breakdown of memory usage by category in bytes:
connection_readers: 0
connection_writers: 0
connection_channels: 0
connection_other: 0
queue_procs: 0
queue_slave_procs: 0
...
The rabbitmq-plugins
Command
The rabbitmq-plugins
tool is used to manage plugins for the RabbitMQ server.
Basic Syntax
rabbitmq-plugins [options] command [command_options]
Common Commands
List all plugins:
rabbitmq-plugins list
Example output:
Listing plugins with pattern ".*" ...
Configured: E = explicitly enabled; e = implicitly enabled
| Status: * = running on rabbit@hostname
|/
[E*] rabbitmq_management 3.11.2
[e*] rabbitmq_management_agent 3.11.2
[e*] rabbitmq_web_dispatch 3.11.2
...
Enable a plugin:
rabbitmq-plugins enable rabbitmq_management
Disable a plugin:
rabbitmq-plugins disable rabbitmq_management
The rabbitmqadmin
Command
The rabbitmqadmin
is a Python-based command-line tool that uses the RabbitMQ HTTP API. It's not installed by default but comes with the management plugin.
Installing rabbitmqadmin
First, enable the management plugin:
rabbitmq-plugins enable rabbitmq_management
Then download the rabbitmqadmin script:
curl -O http://localhost:15672/cli/rabbitmqadmin
chmod +x rabbitmqadmin
mv rabbitmqadmin /usr/local/bin/
Basic Syntax
rabbitmqadmin [options] command [command_options]
Common Commands
List queues:
rabbitmqadmin list queues
Create a queue:
rabbitmqadmin declare queue name=my_queue durable=true
Publish a message:
rabbitmqadmin publish exchange=amq.default routing_key=my_queue payload="Hello, World!"
Get a message from a queue:
rabbitmqadmin get queue=my_queue
Example output:
+-------------+----------+---------------+--------------+------------------+-------------+
| routing_key | exchange | message_count | payload | payload_encoding | redelivered |
+-------------+----------+---------------+--------------+------------------+-------------+
| my_queue | | 0 | Hello, World!| string | False |
+-------------+----------+---------------+--------------+------------------+-------------+
Delete a queue:
rabbitmqadmin delete queue name=my_queue
Practical Examples
Example 1: Setting Up a New RabbitMQ Instance
# Start the RabbitMQ server
sudo service rabbitmq-server start
# Check if it's running
rabbitmq-diagnostics ping
# Enable the management plugin
rabbitmq-plugins enable rabbitmq_management
# Create a new admin user
rabbitmqctl add_user admin StrongPassword123
rabbitmqctl set_user_tags admin administrator
rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
# Delete the default guest user (security best practice)
rabbitmqctl delete_user guest
Example 2: Monitoring Queue Activity
Create a simple Bash script to monitor queue activity:
#!/bin/bash
# save as monitor_queues.sh
while true; do
echo "Queue Status at $(date)"
rabbitmqctl list_queues name messages consumers
echo "------------------------"
sleep 10
done
Make it executable and run:
chmod +x monitor_queues.sh
./monitor_queues.sh
Example 3: Setting Up High Availability
Setting up a mirrored queue for high availability:
# Set policy for mirroring all queues starting with 'ha.'
rabbitmqctl set_policy ha-all "^ha\." '{"ha-mode":"all"}' --apply-to queues
Troubleshooting with Command-Line Tools
Problem: RabbitMQ Node Not Starting
# Check for errors in logs
rabbitmq-diagnostics log_tail
# Check for Erlang VM issues
rabbitmq-diagnostics erlang_cookie_sources
# Check file permissions and ownership
rabbitmq-diagnostics status
Problem: Performance Issues
# Check memory consumption
rabbitmq-diagnostics memory_breakdown
# Check for slow consumers
rabbitmqctl list_queues name messages consumers consumer_utilisation
Problem: Connection Issues
# Check connections
rabbitmqctl list_connections
# Check for blocked connections
rabbitmqctl list_connections state
# Check listener status
rabbitmq-diagnostics listeners
Summary
RabbitMQ command-line tools are essential for effectively managing, monitoring, and troubleshooting your message broker. We've covered the four main tools:
rabbitmqctl
for general managementrabbitmq-diagnostics
for health checking and troubleshootingrabbitmq-plugins
for plugin managementrabbitmqadmin
for HTTP-API based operations
Mastering these tools enables you to automate RabbitMQ operations, integrate with CI/CD pipelines, and effectively manage your messaging infrastructure in production environments.
Exercises
- Create a script that monitors queue depth and alerts when it exceeds a threshold.
- Set up a user with limited permissions that can only publish to specific exchanges.
- Create a queue with a message TTL and dead-letter exchange configuration using the command line.
- Write a script that regularly backs up RabbitMQ definitions.
Additional Resources
Happy messaging with RabbitMQ!
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)