Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mqtt support #950

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Mqtt support #950

wants to merge 5 commits into from

Conversation

kadaan
Copy link

@kadaan kadaan commented Dec 31, 2024

Summary

#803 - Add support for MQTT testing

MQTT Endpoints are similar to DNS Endpoints in that they are recognized by the presence of an mqtt config block. This enables the endpoint to be configured with the required topic and the optional username/password. The check will subscribe to the topic and wait for a message to come in that contains a payload that matches the BODY. While the check is waiting for incoming messages it will public a message containing the BODY as the payload. If the subscription gets the message containing the body= before the timeout publish, CONNECTED will be set to true. Otherwise, it will be false. The body can also be a text/template with support for a uuidv4 function. This enables the body to be specified like gatus check - {{ uuidv4 }} and ensures that the subscription is getting the message that was just published.

2024-12-31 at 10 28 07@2x

2024-12-31 at 10 27 06@2x

2024-12-31 at 10 27 33@2x

@TwiN Can you give me feedback on this PR?

Checklist

  • Tested and/or added tests to validate that the changes work as intended, if applicable.
  • Updated documentation in README.md, if applicable.

@zdzichu Here is an implementation of MQTT checking

@zdzichu
Copy link

zdzichu commented Jan 1, 2025

Hey, thanks for implementing this! I gave it a spin, and while basic functionality seems to work, I have some questions.

First:

[CONNECTED] resolves to true if the MQTT message was published and the same message was consumed, false otherwise`

It sounds like this is like "ping" service, checking if MQTT broker responds? Or possibly expecting a known value on configured topic? That would monitor the broker functionality itself, not the values published by other clients.

Second, I'm not sure how to configure for alerting for usecases mentioned in the original issue:

  • alert when no message was published to given topic for 5 minutes
  • alert when value published to given topic is larger than 10

Could you guide me?

@kadaan
Copy link
Author

kadaan commented Jan 1, 2025

Yeah, what I implemented is a ping to ensure that the basic MQTT infrastructure is up and running. You are correct that it will not monitor the values published by other clients. Given that, I don't think those kind of alerts can be created.

The cases you mention seem to be quite different from how Gatus does other checks. They would require a persistent checker that would be subscribing from MQTT and exposing a metric. This is more in line with how a Prometheus exporter works. I believe that external endpoints or #596 would be a better fit for those checks.

@kadaan
Copy link
Author

kadaan commented Jan 7, 2025

@TwiN Can you review?

@@ -32,6 +36,12 @@ var (

whoisClient = whois.NewClient().WithReferralCache(true)
whoisExpirationDateCache = gocache.NewCache().WithMaxSize(10000).WithDefaultTTL(24 * time.Hour)

mqttTemplateEngine = template.New("base").Funcs(template.FuncMap{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only think I'm unsure about is the fact that this uses a templating engine, while everything else leverages placeholder and functions. Maybe this should be exposed as a placeholder instead? I feel supporting [UUID4] globally (and not just for MQTT) would be more beneficial, albeit more complicated.

The UUID4 placeholder should also be unique per endpoint, meaning that if it's in the url and the body, it should be the same. Let me know if that doesn't make sense, I can give you some examples

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I will look around and see if I can just figure it out.

@@ -5,6 +5,7 @@ import (
"crypto/tls"
"crypto/x509"
"errors"
"github.com/TwiN/gatus/v5/config/endpoint/mqtt"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be separate from the standard library dependencies, namely, at the bottom. See other go files for example.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know/ping me when you resolved it 👀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants