Alerting: Add MQTT notifications receiver (#91487)

* Alerting: Add MQTT notifications receiver
* Update alerting to 9daa6239cc41dc42bff0e916c8d0d27766caa8b9 (main)
---------

Co-authored-by: Jack Baldry <jack.baldry@grafana.com>
Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>
This commit is contained in:
Alexander Akhmetov
2024-08-22 16:47:48 +02:00
committed by GitHub
co-authored by Jack Baldry brendamuir
parent 9c73916f09
commit 832bb01f36
12 changed files with 324 additions and 5 deletions
@@ -51,7 +51,7 @@ func ContactPointToContactPointExport(cp definitions.ContactPoint) (notify.APIRe
len(cp.Pagerduty) + len(cp.OnCall) + len(cp.Pushover) + len(cp.Sensugo) +
len(cp.Sns) + len(cp.Slack) + len(cp.Teams) + len(cp.Telegram) +
len(cp.Threema) + len(cp.Victorops) + len(cp.Webhook) + len(cp.Wecom) +
len(cp.Webex)
len(cp.Webex) + len(cp.Mqtt)
integration := make([]*notify.GrafanaIntegrationConfig, 0, contactPointsLength)
@@ -105,6 +105,13 @@ func ContactPointToContactPointExport(cp definitions.ContactPoint) (notify.APIRe
}
integration = append(integration, el)
}
for _, i := range cp.Mqtt {
el, err := marshallIntegration(j, "mqtt", i, i.DisableResolveMessage)
if err != nil {
errs = append(errs, err)
}
integration = append(integration, el)
}
for _, i := range cp.Opsgenie {
el, err := marshallIntegration(j, "opsgenie", i, i.DisableResolveMessage)
if err != nil {
@@ -274,6 +281,11 @@ func parseIntegration(json jsoniter.API, result *definitions.ContactPoint, recei
if err = json.Unmarshal(data, &integration); err == nil {
result.Line = append(result.Line, integration)
}
case "mqtt":
integration := definitions.MqttIntegration{DisableResolveMessage: disable}
if err = json.Unmarshal(data, &integration); err == nil {
result.Mqtt = append(result.Mqtt, integration)
}
case "opsgenie":
integration := definitions.OpsgenieIntegration{DisableResolveMessage: disable}
if err = json.Unmarshal(data, &integration); err == nil {
@@ -85,6 +85,19 @@ type LineIntegration struct {
Description *string `json:"description,omitempty" yaml:"description,omitempty" hcl:"description"`
}
type MqttIntegration struct {
DisableResolveMessage *bool `json:"-" yaml:"-" hcl:"disable_resolve_message"`
BrokerURL *string `json:"brokerUrl,omitempty" yaml:"brokerUrl,omitempty" hcl:"broker_url"`
ClientID *string `json:"clientId,omitempty" yaml:"clientId,omitempty" hcl:"client_id"`
Topic *string `json:"topic,omitempty" yaml:"topic,omitempty" hcl:"topic"`
Message *string `json:"message,omitempty" yaml:"message,omitempty" hcl:"message"`
MessageFormat *string `json:"messageFormat,omitempty" yaml:"messageFormat,omitempty" hcl:"message_format"`
Username *string `json:"username,omitempty" yaml:"username,omitempty" hcl:"username"`
Password *Secret `json:"password,omitempty" yaml:"password,omitempty" hcl:"password"`
InsecureSkipVerify *bool `json:"insecureSkipVerify,omitempty" yaml:"insecureSkipVerify,omitempty" hcl:"insecure_skip_verify"`
}
type OnCallIntegration struct {
DisableResolveMessage *bool `json:"-" yaml:"-" hcl:"disable_resolve_message"`
@@ -299,6 +312,7 @@ type ContactPoint struct {
Googlechat []GooglechatIntegration `json:"googlechat" yaml:"googlechat" hcl:"googlechat,block"`
Kafka []KafkaIntegration `json:"kafka" yaml:"kafka" hcl:"kafka,block"`
Line []LineIntegration `json:"line" yaml:"line" hcl:"line,block"`
Mqtt []MqttIntegration `json:"mqtt" yaml:"mqtt" hcl:"mqtt,block"`
Opsgenie []OpsgenieIntegration `json:"opsgenie" yaml:"opsgenie" hcl:"opsgenie,block"`
Pagerduty []PagerdutyIntegration `json:"pagerduty" yaml:"pagerduty" hcl:"pagerduty,block"`
OnCall []OnCallIntegration `json:"oncall" yaml:"oncall" hcl:"oncall,block"`
@@ -5,6 +5,7 @@ import (
"os"
"strings"
alertingMqtt "github.com/grafana/alerting/receivers/mqtt"
alertingOpsgenie "github.com/grafana/alerting/receivers/opsgenie"
alertingPagerduty "github.com/grafana/alerting/receivers/pagerduty"
alertingTemplates "github.com/grafana/alerting/templates"
@@ -1245,6 +1246,93 @@ func GetAvailableNotifiers() []*NotifierPlugin {
},
},
},
{
Type: "mqtt",
Name: "MQTT",
Description: "Sends notifications to an MQTT broker",
Heading: "MQTT settings",
Info: "The MQTT notifier sends messages to an MQTT broker. The message is sent to the topic specified in the configuration. ",
Options: []NotifierOption{
{
Label: "Broker URL",
Element: ElementTypeInput,
InputType: InputTypeText,
Placeholder: "tcp://localhost:1883",
Description: "The URL of the MQTT broker.",
PropertyName: "brokerUrl",
Required: true,
},
{
Label: "Topic",
Element: ElementTypeInput,
InputType: InputTypeText,
Placeholder: "grafana/alerts",
Description: "The topic to which the message will be sent.",
PropertyName: "topic",
Required: true,
},
{
Label: "Message format",
Element: ElementTypeSelect,
SelectOptions: []SelectOption{
{
Value: alertingMqtt.MessageFormatJSON,
Label: "json",
},
{
Value: alertingMqtt.MessageFormatText,
Label: "text",
},
},
InputType: InputTypeText,
Placeholder: "json",
Description: "The format of the message to be sent. If set to 'json', the message will be sent as a JSON object. If set to 'text', the message will be sent as a plain text string. By default json is used.",
PropertyName: "messageFormat",
Required: false,
},
{
Label: "Client ID",
Element: ElementTypeInput,
InputType: InputTypeText,
Placeholder: "",
Description: "The client ID to use when connecting to the MQTT broker. If blank, a random client ID is used.",
PropertyName: "clientId",
Required: false,
},
{
Label: "Message",
Element: ElementTypeTextArea,
Placeholder: alertingTemplates.DefaultMessageEmbed,
PropertyName: "message",
},
{
Label: "Username",
Description: "The username to use when connecting to the MQTT broker.",
Element: ElementTypeInput,
InputType: InputTypeText,
Placeholder: "",
PropertyName: "username",
Required: false,
},
{
Label: "Password",
Description: "The password to use when connecting to the MQTT broker.",
Element: ElementTypeInput,
InputType: InputTypeText,
Placeholder: "",
PropertyName: "password",
Required: false,
Secure: true,
},
{
Label: "Disable certificate verification",
Element: ElementTypeCheckbox,
Description: "Do not verify the broker's certificate chain and host name.",
PropertyName: "insecureSkipVerify",
Required: false,
},
},
},
{
Type: "opsgenie",
Name: "OpsGenie",