Skip to content

MQTT broker (mqtt_broker) ​

Connects to an MQTT broker, signs in if you ask it to, and reports how long the connection and the broker's acceptance took. With the round trip turned on it also subscribes to a topic, publishes a short message to it, and times how long the broker takes to hand the message back. Over TLS it reads the broker's certificate from the same connection and reports how long it remains valid, so one sensor watches the broker and its certificate.

Use it to know that a broker does its job, not only that its port is open: a broker that accepts connections but has stopped routing messages (a full disk, a stuck bridge, an access list that lost its entry) fails the round trip. To read the values that devices publish, use the MQTT topic sensor.

What it looks like ​

The page of an mqtt_broker sensor in the light theme

The sensor's page after a few polls, connecting over TLS with a login and timing the round trip. In the dark theme: the sensor's page. The form for adding it, with its parameters filled in: light, dark.

Channels ​

KeyNameUnitDefault limits
connect_msConnect timemswarning above 1000, error above 5000
round_trip_msRound tripmswarning above 500, error above 3000; only with round_trip
cert_days_remainingDays until the certificate expiresdayswarning below 21, error below 7; only over TLS
chain_days_remainingDays until the earliest expiry in the chaindayswarning below 21, error below 7; only over TLS

connect_ms covers the TCP connection, the TLS handshake if there is one, and the broker's answer to the sign-in (CONNACK). round_trip_ms is measured from the moment the sensor has the broker's confirmation of the subscription and sends the message, until the same message is received. A round trip that does not come back within wait_ms is not a failed poll: the sensor is Down and the message says which topic was used.

Parameters ​

ParameterDefaultMeaning
port1883, or 8883 with security tlsTCP port.
securitynonenone (a plain connection) or tls (from the first byte). MQTT has no STARTTLS.
authenticatefalseSign in with the login credential. Without it the connection is anonymous.
allow_plaintext_loginfalseAllow authenticate without tls, which sends the password in the clear. A sensor that would do so is refused when it is saved unless this is set.
round_tripfalseAlso time a message through the broker. Needs an account that may publish to and subscribe to the topic.
round_trip_topicprobeward/roundtripThe topic for the round trip, without wildcards.
wait_ms3000How long to wait for the round trip, 100 to 60000. Never longer than the poll's timeout.
server_namethe device's host name, else its addressThe name the certificate has to be valid for, and the name sent for SNI.
verify_tlstrueVerify the certificate against the probe host's trust store.
tls_fingerprintnoneTrust exactly the certificate with this SHA-256 fingerprint; then verify_tls is not needed.
check_certificatetrueReport the two certificate channels over TLS.

Suggested interval: 60 seconds. Credential: optional login.

The login. It is the login credential (user name and password) of the sensor, its device or a group above it; see credentials. It is sent only when authenticate is on, and never appears in parameters, messages or logs. Use an account that can do no more than the check needs.

The round trip publishes at QoS 0, without the retain flag, a message of the form probeward <random>. Each poll sends a different one, and only the same one counts as the answer, so an older message left on the topic cannot make a broken broker look healthy. The message is not kept by the broker. QoS 0 means a message may be lost on a congested connection; that shows as a Down poll and the next poll tries again.

Certificates ​

The same rules as the web sensors, described in pinning a certificate: the chain has to lead to an authority the probe host trusts and has to be valid for the name, unless tls_fingerprint names the one certificate to trust. When a certificate is refused for not being trusted or not matching the name, the message carries the SHA-256 fingerprint the broker presented, so you can compare it with the broker's own and pin exactly that one. The certificate's dates are reported even when a pin is used; an expired certificate makes the sensor Down.

On the target ​

An MQTT broker (3.1.1 or 5, which accepts 3.1.1 clients) reachable from the probe on the port. For the round trip, an account allowed to publish and subscribe on one topic. Nothing is installed, and nothing is changed except the one short, unretained message of the round trip.

The sensor speaks MQTT 3.1.1 only, with a clean session and no will, no retained publishing and no keep-alive pings: the connection lives for the length of the check.

Failures ​

MessageMeaning
the broker refused the login: bad user name or password (kind auth_failed)The name or the password in the credential is wrong.
the broker refused the connection: not authorised (kind auth_failed)The account is not allowed, or the broker does not accept anonymous clients and no login is assigned.
the broker does not accept MQTT 3.1.1 (kind protocol)A broker for another protocol level.
the port answered with TLS, not MQTT; set security to tls (kind protocol)A TLS port reached without TLS.
TLS error: ... (kind protocol)The certificate was refused, or the port does not speak TLS. The text says which and how to fix it.
this sensor is set to sign in and needs a login credential (kind config)authenticate is on and no login credential is assigned or inherited.
a timeout (kind timeout), connection refused (kind unreachable)The broker did not answer, or nothing listens on the port.
the message published to "..." did not come back within ... msA successful poll that is Down: the broker takes messages but does not hand them back, or the account may not publish or subscribe there.

Verification ​

Run against Mosquitto in a container on a lab network, on the plain port and on a TLS port with a certificate from the lab's own authority: a login that is accepted, an anonymous client that is refused, a wrong password, the round trip, a name the certificate is not for (refused, with the fingerprint in the message), that fingerprint used as a pin, a pin of another certificate, and the certificate verified against its authority. The packets, the refusal codes, the round trip with an older message on the topic, the pin and a TLS port reached without TLS are also tested against a broker written for the tests, which needs no lab. The sensor's rules (channels, limits, login use, certificate channels) are tested against scripted answers.

Not seen: a broker other than Mosquitto, MQTT 5 features, a bridge, a cluster.

The live tests read their target from the environment and say so and return when it is not set:

VariableMeaning
PROBEWARD_TEST_MQTT_HOSTThe broker's address.
PROBEWARD_TEST_MQTT_USER, PROBEWARD_TEST_MQTT_PASSWORDAn account. The broker must refuse anonymous clients for the refusal test to mean something.
PROBEWARD_TEST_MQTT_TOPIC, PROBEWARD_TEST_MQTT_VALUEA retained topic that holds a plain number, and the number.
PROBEWARD_TEST_MQTT_ROUNDTRIP_TOPICOptional: a topic the account may publish to and subscribe to; turns the round trip test on.
PROBEWARD_TEST_MQTT_NAMEOptional: the name the broker's certificate is for, with TLS on port 8883. With SSL_CERT_FILE set to the file of the authority that issued it, the test that verifies the certificate runs too.
sh
cargo test -p probeward-sensors --test industrial_live mqtt -- --nocapture

AGPL-3.0-only