Python Broker Checker

Python Broker checker

I have developed a number of tools for monitoring MQTT brokers that use node-red.

The reason for using node-red is that it is easy to use, and comes with a dashboard.

However when it comes to monitoring multiple brokers it gets complicated.

My solution it to do the monitoring using Python and then use node-red to log and display the results.

Python MQTT Broker Checker

The broker check monitors to state of a list of MQTT brokers by periodically trying to connect to the broker.

The script makes use of threads so that a failed connections doesn’t cause a big delay.

If it can connect then it flags the broker as UP otherwise the broker is DOWN.

In addition it also logs the connection time interval as this is a measure of how responsive the broker is.

The broker list can be reloaded as an array of dictionary objects. An example is shown below:

{
“broker_name”:”192.168.1.23″,
“port”:1883,
“username”:”steve”,
“password”:”password”,
“SSL”:False
}

In addition you can add/remove brokers dynamically be sending commands using MQTT.

The tool reports the status of the broker on the topic

public/brokers/alerts

it receives commands on the topic

public/brokers/commands/[add/remove]

A command response is sent on the topic

public/brokers/responses

When sending commands the payload is a JSONĀ  object like that shown below and also in the script.

Currently script defaults are

  • Maximum threads 4
  • Loop interval 30secs
  • Max connection time 20secs

The results are sent to an MQTT report broker at the end of each cycle and they can be retrieved by a suitable monitor.

This is what the connection report looks like:

broker-monitor-report

Here are some command examples screen shots using MQTT Explorer

broker-add

Removing a broker from the Lists

broker-remove

 

Main Loop

This is a while loop which usually runs indefinitely (count=0). The code is show below:
broker-check-main

The broker check is started in a separate thread.Once the maximum number of threads has been started then the script waits (while loop).

Code Download

download

Questions

If you have any questions require code explanation then let me know and I will assist.

If you have any suggestions,maybe you would like to see the data displayed on a simple web page without node-red. Or perhaps you want email alerts using python etc then please let me know and I will try to incorporate your suggestion into the project

Part 2

In Part 2 we will look at using node-red to display the broker status on a Dashboard.

Please rate? And use Comments to let me know more

Leave a Reply

Your email address will not be published. Required fields are marked *