Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Rabbit MQ is down

Tags:

c#

rabbitmq

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#.

like image 435
Display name Avatar asked Oct 18 '22 19:10

Display name


1 Answers

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.

like image 167
O. Jones Avatar answered Oct 21 '22 05:10

O. Jones