How to Deploy MQTT with Mosquitto and Monitor It Using Zabbix Agent2
This guide explains the MQTT protocol, shows how to install and run a Mosquitto broker on CentOS, and demonstrates how to collect MQTT messages with a custom Zabbix Agent2 plugin for real‑time monitoring in Zabbix.
MQTT Protocol Overview
MQTT (Message Queuing Telemetry Transport) is an ISO‑standard publish/subscribe protocol designed for low‑power devices and unreliable networks. It is widely used in automotive, manufacturing, telecom, oil & gas, satellite‑linked sensors, medical devices, home automation, and mobile apps due to its small footprint and low power consumption.
Components
MQTT broker – the central hub that routes messages between clients.
MQTT client – any device that connects to the broker; publishers send messages, subscribers receive them by subscribing to topics.
Deploying an MQTT Broker (Mosquitto) on CentOS
Install and build Mosquitto from source:
<code>mkdir data && cd data
wget http://mosquitto.org/files/source/mosquitto-1.6.8.tar.gz
tar -zxvf mosquitto-1.6.8.tar.gz
cd mosquitto-1.6.8
make && make install
ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
ldconfig</code>Edit
mosquitto.confto set the listening address and authentication, then start the broker:
<code>mosquitto -v</code>Testing the Broker
<code>mosquitto_sub -v -t sensor</code> <code>mosquitto_pub -t sensor -m 12</code>Subscribers will receive the published messages.
Collecting MQTT Messages with Zabbix Agent2
Zabbix Agent2 acts as an MQTT subscriber. The agent is written in Go, allowing custom plugins. The MQTT plugin implements a watcher that pushes new data to the Zabbix server.
1. Build Zabbix Agent2 with the MQTT Plugin
<code>yum install golang
git clone https://git.zabbix.com/scm/zbx/zabbix.git --depth 1 zabbix-agent2
cd zabbix-agent2
git submodule add https://github.com/v-zhuravlev/zbx_plugin_mqtt.git src/go/plugins/mqtt</code>2. Import the Plugin
<code>vi src/go/plugins/plugins.go</code> <code>package plugins
import (
_ "zabbix.com/plugins/log"
_ "zabbix.com/plugins/systemrun"
_ "zabbix.com/plugins/zabbix/async"
_ "zabbix.com/plugins/zabbix/stats"
_ "zabbix.com/plugins/zabbix/sync"
_ "zabbix.com/plugins/mqtt"
)</code>3. Compile and Install Agent2
<code>./bootstrap.sh
pushd .
cd src/go/
go mod vendor
popd
./configure --enable-agent2 --enable-static
make install</code>4. Configure the Plugin
Add entries to
zabbix_agent2.confsuch as:
<code>Plugins.MQTTSubscribe.Username = <mqtt_user>
Plugins.MQTTSubscribe.Password = <mqtt_password>
Plugins.MQTTSubscribe.ClientID = zabbix-agent2-mqtt-client
Plugins.MQTTSubscribe.Timeout = 5</code>5. Start the Agent
<code>cd /root/zabbix_agent/src/go/bin
./zabbix_agent2 -c ../conf/zabbix_agent2.conf</code>The plugin pushes received MQTT data to the Zabbix server.
6. Create a Zabbix Item
Use a key of the form:
<code>mqtt.subscribe[<MQTT broker URL>,<MQTT topic>]</code>Set the item type to “active”. The data appears in the Zabbix frontend in real time.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.