diff --git a/docs/notif/mqtt.md b/docs/notif/mqtt.md index 414f4dca..24f6a74f 100644 --- a/docs/notif/mqtt.md +++ b/docs/notif/mqtt.md @@ -1,6 +1,6 @@ -# Mqtt notifications +# MQTT notifications -You can send notifications to any mqtt compatible server with the following settings. +You can send notifications to any MQTT compatible server with the following settings. ## Configuration @@ -12,8 +12,8 @@ You can send notifications to any mqtt compatible server with the following sett port: 1883 username: guest password: guest - topic: docker/diun client: diun + topic: docker/diun qos: 0 ``` @@ -21,13 +21,13 @@ You can send notifications to any mqtt compatible server with the following sett |--------------------|---------------|---------------| | `host`[^1] | `localhost` | MQTT server host | | `port`[^1] | `1883` | MQTT server port | -| `client`[^1] | `diun-client` | Name of the client which connects to the server | -| `topic`[^1] | `docker/diun` | Topic the message will be sent to | | `username` | | MQTT username | | `usernameFile` | | Use content of secret file as MQTT username if `username` not defined | | `password` | | MQTT password | | `passwordFile` | | Use content of secret file as MQTT password if `password` not defined | -| `qos` | `0` | Topic the message will be sent to | +| `client`[^1] | | Client id to be used by this client when connecting to the MQTT broker | +| `topic`[^1] | | Topic the message will be sent to | +| `qos` | `0` | Ensured message delivery at specified Quality of Service (QoS) | ## Sample diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 2ecd27de..a4769959 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -121,6 +121,15 @@ func TestLoadFile(t *testing.T) { RoomID: "!abcdefGHIjklmno:matrix.org", MsgType: model.NotifMatrixMsgTypeNotice, }, + Mqtt: &model.NotifMqtt{ + Host: "localhost", + Port: 1883, + Username: "guest", + Password: "guest", + Client: "diun", + Topic: "docker/diun", + QoS: 0, + }, RocketChat: &model.NotifRocketChat{ Endpoint: "http://rocket.foo.com:3000", Channel: "#general", diff --git a/internal/config/fixtures/config.test.yml b/internal/config/fixtures/config.test.yml index c77c445a..b9709387 100644 --- a/internal/config/fixtures/config.test.yml +++ b/internal/config/fixtures/config.test.yml @@ -42,6 +42,14 @@ notif: user: "@foo:matrix.org" password: bar roomID: "!abcdefGHIjklmno:matrix.org" + mqtt: + host: "localhost" + port: 1883 + username: "guest" + password: "guest" + client: "diun" + topic: "docker/diun" + qos: 0 rocketchat: endpoint: http://rocket.foo.com:3000 channel: "#general" diff --git a/internal/config/fixtures/config.validate.yml b/internal/config/fixtures/config.validate.yml index 7eac3310..43a7a5d3 100644 --- a/internal/config/fixtures/config.validate.yml +++ b/internal/config/fixtures/config.validate.yml @@ -42,6 +42,14 @@ notif: user: "@foo:matrix.org" password: bar roomID: "!abcdefGHIjklmno:matrix.org" + mqtt: + host: "localhost" + port: 1883 + username: "guest" + password: "guest" + client: "diun" + topic: "docker/diun" + qos: 0 rocketchat: endpoint: http://rocket.foo.com:3000 channel: "#general" diff --git a/internal/model/notif.go b/internal/model/notif.go index 154ee6d7..79bfc16a 100644 --- a/internal/model/notif.go +++ b/internal/model/notif.go @@ -29,7 +29,7 @@ type Notif struct { Gotify *NotifGotify `yaml:"gotify,omitempty" json:"gotify,omitempty"` Mail *NotifMail `yaml:"mail,omitempty" json:"mail,omitempty"` Matrix *NotifMatrix `yaml:"matrix,omitempty" json:"matrix,omitempty"` - Mqtt *NotifMqtt `yaml:"mqtt,omitempty" json:"mqtt,omitempty"` + Mqtt *NotifMqtt `yaml:"mqtt,omitempty" json:"mqtt,omitempty"` RocketChat *NotifRocketChat `yaml:"rocketchat,omitempty" json:"rocketchat,omitempty"` Script *NotifScript `yaml:"script,omitempty" json:"script,omitempty"` Slack *NotifSlack `yaml:"slack,omitempty" json:"slack,omitempty"` diff --git a/internal/model/notif_mqtt.go b/internal/model/notif_mqtt.go index 393058df..65a460c3 100644 --- a/internal/model/notif_mqtt.go +++ b/internal/model/notif_mqtt.go @@ -1,14 +1,14 @@ package model type NotifMqtt struct { + Host string `yaml:"host,omitempty" json:"host,omitempty" validate:"required"` + Port int `yaml:"port,omitempty" json:"port,omitempty" validate:"required,min=1"` Username string `yaml:"username,omitempty" json:"username,omitempty" validate:"omitempty"` UsernameFile string `yaml:"usernameFile,omitempty" json:"usernameFile,omitempty" validate:"omitempty,file"` Password string `yaml:"password,omitempty" json:"password,omitempty" validate:"omitempty"` PasswordFile string `yaml:"passwordFile,omitempty" json:"passwordFile,omitempty" validate:"omitempty,file"` - Host string `yaml:"host,omitempty" json:"host,omitempty" validate:"required"` - Port int `yaml:"port,omitempty" json:"port,omitempty" validate:"required,min=1"` - Topic string `yaml:"topic,omitempty" json:"topic,omitempty" validate:"required"` Client string `yaml:"client,omitempty" json:"client,omitempty" validate:"required"` + Topic string `yaml:"topic,omitempty" json:"topic,omitempty" validate:"required"` QoS int `yaml:"qos,omitempty" json:"qos,omitempty" validate:"omitempty"` } @@ -23,7 +23,5 @@ func (s *NotifMqtt) GetDefaults() *NotifMqtt { func (s *NotifMqtt) SetDefaults() { s.Host = "localhost" s.Port = 1883 - s.Topic = "docker/diun" - s.Client = "diun-client" s.QoS = 0 } diff --git a/internal/notif/client.go b/internal/notif/client.go index 6e40b120..db214e4a 100644 --- a/internal/notif/client.go +++ b/internal/notif/client.go @@ -1,7 +1,6 @@ package notif import ( - "github.com/crazy-max/diun/v4/internal/notif/mqtt" "strings" "github.com/crazy-max/diun/v4/internal/model" @@ -10,6 +9,7 @@ import ( "github.com/crazy-max/diun/v4/internal/notif/gotify" "github.com/crazy-max/diun/v4/internal/notif/mail" "github.com/crazy-max/diun/v4/internal/notif/matrix" + "github.com/crazy-max/diun/v4/internal/notif/mqtt" "github.com/crazy-max/diun/v4/internal/notif/notifier" "github.com/crazy-max/diun/v4/internal/notif/rocketchat" "github.com/crazy-max/diun/v4/internal/notif/script" diff --git a/internal/notif/mqtt/client.go b/internal/notif/mqtt/client.go index 1f83f967..eebbdd2d 100644 --- a/internal/notif/mqtt/client.go +++ b/internal/notif/mqtt/client.go @@ -3,11 +3,11 @@ package mqtt import ( "encoding/json" "fmt" - "github.com/crazy-max/diun/v4/pkg/utl" "time" "github.com/crazy-max/diun/v4/internal/model" "github.com/crazy-max/diun/v4/internal/notif/notifier" + "github.com/crazy-max/diun/v4/pkg/utl" MQTT "github.com/eclipse/paho.mqtt.golang" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -16,8 +16,8 @@ import ( // Client represents an active mqtt notification object type Client struct { *notifier.Notifier - cfg *model.NotifMqtt - meta model.Meta + cfg *model.NotifMqtt + meta model.Meta logger zerolog.Logger } @@ -25,8 +25,8 @@ type Client struct { func New(config *model.NotifMqtt, meta model.Meta) notifier.Notifier { return notifier.Notifier{ Handler: &Client{ - cfg: config, - meta: meta, + cfg: config, + meta: meta, logger: log.With().Str("notif", "mqtt").Logger(), }, } @@ -59,8 +59,6 @@ func (c *Client) Send(entry model.NotifEntry) error { return token.Error() } - log.Debug().Msgf("Connected to broker: %s", broker) - message, err := json.Marshal(struct { Version string `json:"diun_version"` Hostname string `json:"hostname"` @@ -86,9 +84,7 @@ func (c *Client) Send(entry model.NotifEntry) error { return err } - log.Debug().Msgf("Publishing to topic: %s", c.cfg.Topic) token := client.Publish(c.cfg.Topic, byte(c.cfg.QoS), false, message) token.Wait() - return token.Error() } diff --git a/mkdocs.yml b/mkdocs.yml index 695181b0..8e0bfaf8 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -94,6 +94,7 @@ nav: - Gotify: notif/gotify.md - Mail: notif/mail.md - Matrix: notif/matrix.md + - MQTT: notif/mqtt.md - Rocket.Chat: notif/rocketchat.md - Script: notif/script.md - Slack: notif/slack.md