How to check connection to the RabbitMQ message broker

Use Python script to verify that client can connect to the RabbitMQ message broker.

Prerequisites

Install Pika a pure-Python implementation of the AMQP 0-9-1 protocol.

$ sudo apt install python3-pip
$ pip3 install pika

Python script

Save the following Python script somewhere in the PATH as the check_rabbitmq_connection.py file. What I like about this script is that it's very simple and easy to extend.

#!/usr/bin/env python3
# Check connection to the RabbitMQ server
# Source: https://blog.sleeplessbeastie.eu/2017/07/10/how-to-check-connection-to-the-rabbitmq-message-broker/ 

# import parser for command-line options
import argparse
# import a pure-Python implementation of the AMQP 0-9-1 
import pika

# define and parse command-line options
parser = argparse.ArgumentParser(description='Check connection to RabbitMQ server')
parser.add_argument('--server', required=True, help='Define RabbitMQ server')
parser.add_argument('--virtual_host', default='/', help='Define virtual host')
parser.add_argument('--ssl', action='store_true', help='Enable SSL (default: %(default)s)')
parser.add_argument('--port', type=int, default=5672, help='Define port (default: %(default)s)')
parser.add_argument('--username', default='guest', help='Define username (default: %(default)s)')
parser.add_argument('--password', default='guest', help='Define password (default: %(default)s)')
args = vars(parser.parse_args())

# set amqp credentials
credentials = pika.PlainCredentials(args['username'], args['password'])
# set amqp connection parameters
parameters = pika.ConnectionParameters(host=args['server'], port=args['port'], virtual_host=args['virtual_host'], credentials=credentials, ssl=args['ssl'])

# try to establish connection and check its status
try:
  connection = pika.BlockingConnection(parameters)
  if connection.is_open:
    print('OK')
    connection.close()
    exit(0)
except Exception as error:
  print('Error:', error.__class__.__name__)
  exit(1)

Ensure that execute permissions are set.

$ sudo chmod +x check_rabbitmq_connection.py

Usage

Execute command without any parameters.

$ check_rabbitmq_connection.py
usage: check_rabbitmq_connection.py [-h] --server SERVER [--virtual_host VIRTUAL_HOST] [--ssl]
                                    [--port PORT] [--username USERNAME] [--password PASSWORD]
		                    check_rabbitmq_connection.py: error: the following arguments are required: --server

Display usage information.

$ check_rabbitmq_connection.py -h
usage: check_rabbitmq_connection.py [-h] --server SERVER [--virtual_host VIRTUAL_HOST] [--ssl]
                                    [--port PORT] [--username USERNAME] [--password PASSWORD]

Check connection to the RabbitMQ server

optional arguments:
  -h, --help            show this help message and exit
  --server SERVER       Define RabbitMQ server
  --virtual_host VIRTUAL_HOST
                        Define virtual host
  --ssl                 Enable SSL (default: False)
  --port PORT           Define port (default: 5672)
  --username USERNAME   Define username (default: guest)
  --password PASSWORD   Define password (default: guest)

Check connection to the rabbit.example.org server using default credentials that are incorrect in this case.

$ check_rabbitmq_connection.py --server rabbit.example.org
Error: ProbableAuthenticationError

Check connection to the rabbit.example.org server using defined credentials.

$ check_rabbitmq_connection.py  --server rabbit.example.org --username "ruser" --password "rpassword"
OK

Check connection to the rabbit.example.org server using defined credentials and non-existing virtual host.

$ check_rabbitmq_connection.py  --server rabbit.example.org --username "ruser" --password "rpassword" --virtual_host "hole"
Error: ProbableAccessDeniedError

Check connection to the rabbit.example.org server using defined credentials and virtual host that are correct in this case.

$ check_rabbitmq_connection.py  --server rabbit.example.org --username "ruser" --password "rpassword" --virtual_host "example"
OK

You can rely solely on the returned exit code to verify connection status.

Python rocks!

Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu