5
votes

I am writing a C# Console app (Windows scheduled task) to monitor the status of Rabbit MQ. So in case the the queue is down (service down, connection timeout, or any other reason) it will send a notification mail. I have used RabbitMQ .Net client (version 4.1.1). Basically I am checking if the CreateConnection() is successfull.

private static void CheckRabbitMQStatus()
{
    ConnectionFactory factory = new ConnectionFactory();
    factory.Uri = "amqp://guest:guest@testserver:5672/";
    IConnection conn = null;
    try
    {
        conn = factory.CreateConnection();
        conn.Close();
        conn.Dispose();
    }
    catch (Exception ex)
    {
        if (ex.Message == "None of the specified endpoints were reachable")
        {
            //send mail MQ is down
        }
    }
}

Is this the right approach to achieve this? There are several tools and plugins available for Rabbit MQ but I want a simple solution in C#.

1
What is the concrete type of ex? Comparing the message might break in later releases ...user57508
The type of the exception is RabbitMQ.Client.Exceptions.BrokerUnreachableException -- That being said, the message is the same as of August 2020 ¯\_(ツ)_/¯anon

1 Answers

3
votes

It should work, if you have a simple message queue setup (without clustering or federation of multiple machines).

The RabbitMQ API has an aliveness check, designed for use by monitoring tools such as yours. It actually sends and receives a message. This is likely to be a more sensitive health check than yours. See the last item on here.

https://www.rabbitmq.com/monitoring.html#health-checks

RabbitMQ itself is robust. Usually the failure mode it experiences is its host being rebooted for other reasons.

Note: There's a nice DebuggableService template by Rob Three in the Windows Marketplace. This makes it pretty easy to rig your health check as a service rather than as a scheduled task. My life got easier when I refactored my MQ stuff to run as services rather than scheduled tasks.