diff --git a/pkg/services/ngalert/notifier/alertmanager.go b/pkg/services/ngalert/notifier/alertmanager.go index 405e6393577..4397080b88b 100644 --- a/pkg/services/ngalert/notifier/alertmanager.go +++ b/pkg/services/ngalert/notifier/alertmanager.go @@ -426,6 +426,8 @@ func (am *Alertmanager) buildReceiverIntegrations(receiver *apimodels.PostableAp n, err = channels.NewWebHookNotifier(cfg, tmpl) case "sensugo": n, err = channels.NewSensuGoNotifier(cfg, tmpl) + case "discord": + n, err = channels.NewDiscordNotifier(cfg, tmpl) case "alertmanager": n, err = channels.NewAlertmanagerNotifier(cfg, tmpl) default: diff --git a/pkg/services/ngalert/notifier/available_channels.go b/pkg/services/ngalert/notifier/available_channels.go index 57d278c198f..a54987f8521 100644 --- a/pkg/services/ngalert/notifier/available_channels.go +++ b/pkg/services/ngalert/notifier/available_channels.go @@ -596,5 +596,29 @@ func GetAvailableNotifiers() []*alerting.NotifierPlugin { }, }, }, + { + Type: "discord", + Name: "Discord", + Heading: "Discord settings", + Description: "Sends notifications to Discord", + Options: []alerting.NotifierOption{ + { + Label: "Message Content", + Description: "Mention a group using @ or a user using <@ID> when notifying in a channel", + Element: alerting.ElementTypeInput, + InputType: alerting.InputTypeText, + Placeholder: `{{ template "default.message" . }}`, + PropertyName: "message", + }, + { + Label: "Webhook URL", + Element: alerting.ElementTypeInput, + InputType: alerting.InputTypeText, + Placeholder: "Discord webhook URL", + PropertyName: "url", + Required: true, + }, + }, + }, } } diff --git a/pkg/services/ngalert/notifier/channels/discord.go b/pkg/services/ngalert/notifier/channels/discord.go new file mode 100644 index 00000000000..c6e8e547e0b --- /dev/null +++ b/pkg/services/ngalert/notifier/channels/discord.go @@ -0,0 +1,122 @@ +package channels + +import ( + "context" + "encoding/json" + "fmt" + "net/url" + "path" + "strconv" + "strings" + + gokit_log "github.com/go-kit/kit/log" + "github.com/prometheus/alertmanager/notify" + "github.com/prometheus/alertmanager/template" + "github.com/prometheus/alertmanager/types" + + "github.com/grafana/grafana/pkg/bus" + "github.com/grafana/grafana/pkg/components/simplejson" + "github.com/grafana/grafana/pkg/infra/log" + "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/services/alerting" + old_notifiers "github.com/grafana/grafana/pkg/services/alerting/notifiers" + "github.com/grafana/grafana/pkg/setting" +) + +type DiscordNotifier struct { + old_notifiers.NotifierBase + log log.Logger + tmpl *template.Template + Content string + WebhookURL string +} + +func NewDiscordNotifier(model *NotificationChannelConfig, t *template.Template) (*DiscordNotifier, error) { + if model.Settings == nil { + return nil, alerting.ValidationError{Reason: "No Settings Supplied"} + } + + discordURL := model.Settings.Get("url").MustString() + if discordURL == "" { + return nil, alerting.ValidationError{Reason: "Could not find webhook url property in settings"} + } + + content := model.Settings.Get("message").MustString(`{{ template "default.message" . }}`) + + return &DiscordNotifier{ + NotifierBase: old_notifiers.NewNotifierBase(&models.AlertNotification{ + Uid: model.UID, + Name: model.Name, + Type: model.Type, + DisableResolveMessage: model.DisableResolveMessage, + Settings: model.Settings, + SecureSettings: model.SecureSettings, + }), + Content: content, + WebhookURL: discordURL, + log: log.New("alerting.notifier.discord"), + tmpl: t, + }, nil +} + +func (d DiscordNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { + data := notify.GetTemplateData(ctx, d.tmpl, as, gokit_log.NewNopLogger()) + alerts := types.Alerts(as...) + + bodyJSON := simplejson.New() + bodyJSON.Set("username", "Grafana") + + var tmplErr error + tmpl := notify.TmplText(d.tmpl, data, &tmplErr) + if d.Content != "" { + bodyJSON.Set("content", tmpl(d.Content)) + } + + footer := map[string]interface{}{ + "text": "Grafana v" + setting.BuildVersion, + "icon_url": "https://grafana.com/assets/img/fav32.png", + } + + embed := simplejson.New() + embed.Set("title", tmpl(`{{ template "default.title" . }}`)) + embed.Set("footer", footer) + embed.Set("type", "rich") + + color, _ := strconv.ParseInt(strings.TrimLeft(getAlertStatusColor(alerts.Status()), "#"), 16, 0) + embed.Set("color", color) + + u, err := url.Parse(d.tmpl.ExternalURL.String()) + if err != nil { + return false, fmt.Errorf("failed to parse external URL: %w", err) + } + u.Path = path.Join(u.Path, "/alerting/list") + ruleURL := u.String() + embed.Set("url", ruleURL) + + bodyJSON.Set("embeds", []interface{}{embed}) + + if tmplErr != nil { + return false, fmt.Errorf("failed to template discord message: %w", tmplErr) + } + + body, err := json.Marshal(bodyJSON) + if err != nil { + return false, err + } + cmd := &models.SendWebhookSync{ + Url: d.WebhookURL, + HttpMethod: "POST", + ContentType: "application/json", + Body: string(body), + } + + if err := bus.DispatchCtx(ctx, cmd); err != nil { + d.log.Error("Failed to send notification to Discord", "error", err) + return false, err + } + return true, nil +} + +func (d DiscordNotifier) SendResolved() bool { + return !d.GetDisableResolveMessage() +} diff --git a/pkg/services/ngalert/notifier/channels/discord_test.go b/pkg/services/ngalert/notifier/channels/discord_test.go new file mode 100644 index 00000000000..7f1efa41780 --- /dev/null +++ b/pkg/services/ngalert/notifier/channels/discord_test.go @@ -0,0 +1,158 @@ +package channels + +import ( + "context" + "encoding/json" + "errors" + "net/url" + "testing" + + "github.com/prometheus/alertmanager/notify" + "github.com/prometheus/alertmanager/types" + "github.com/prometheus/common/model" + "github.com/stretchr/testify/require" + + "github.com/grafana/grafana/pkg/bus" + "github.com/grafana/grafana/pkg/components/simplejson" + "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/services/alerting" +) + +func TestDiscordNotifier(t *testing.T) { + tmpl := templateForTests(t) + + externalURL, err := url.Parse("http://localhost") + require.NoError(t, err) + tmpl.ExternalURL = externalURL + + cases := []struct { + name string + settings string + alerts []*types.Alert + expMsg map[string]interface{} + expInitError error + expMsgError error + }{ + { + name: "Default config with one alert", + settings: `{"url": "http://localhost"}`, + alerts: []*types.Alert{ + { + Alert: model.Alert{ + Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, + Annotations: model.LabelSet{"ann1": "annv1"}, + }, + }, + }, + expMsg: map[string]interface{}{ + "content": "\n**Firing**\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSource: \n\n\n\n\n", + "embeds": []interface{}{map[string]interface{}{ + "color": 1.4037554e+07, + "footer": map[string]interface{}{ + "icon_url": "https://grafana.com/assets/img/fav32.png", + "text": "Grafana v", + }, + "title": "[FIRING:1] (val1)", + "url": "http://localhost/alerting/list", + "type": "rich", + }}, + "username": "Grafana", + }, + expInitError: nil, + expMsgError: nil, + }, + { + name: "Custom config with multiple alerts", + settings: `{ + "url": "http://localhost", + "message": "{{ len .Alerts.Firing }} alerts are firing, {{ len .Alerts.Resolved }} are resolved" + }`, + alerts: []*types.Alert{ + { + Alert: model.Alert{ + Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"}, + Annotations: model.LabelSet{"ann1": "annv1"}, + }, + }, { + Alert: model.Alert{ + Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val2"}, + Annotations: model.LabelSet{"ann1": "annv2"}, + }, + }, + }, + expMsg: map[string]interface{}{ + "content": "2 alerts are firing, 0 are resolved", + "embeds": []interface{}{map[string]interface{}{ + "color": 1.4037554e+07, + "footer": map[string]interface{}{ + "icon_url": "https://grafana.com/assets/img/fav32.png", + "text": "Grafana v", + }, + "title": "[FIRING:2] ", + "url": "http://localhost/alerting/list", + "type": "rich", + }}, + "username": "Grafana", + }, + expInitError: nil, + expMsgError: nil, + }, + { + name: "Error in initialization", + settings: `{}`, + expInitError: alerting.ValidationError{Reason: "Could not find webhook url property in settings"}, + }, + { + name: "Error in building messsage", + settings: `{ + "url": "http://localhost", + "message": "{{ .Status }" + }`, + expMsgError: errors.New("failed to template discord message: template: :1: unexpected \"}\" in operand"), + }, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + settingsJson, err := simplejson.NewJson([]byte(c.settings)) + require.NoError(t, err) + + m := &NotificationChannelConfig{ + Name: "discord_testing", + Type: "discord", + Settings: settingsJson, + } + + dn, err := NewDiscordNotifier(m, tmpl) + if c.expInitError != nil { + require.Error(t, err) + require.Equal(t, c.expInitError.Error(), err.Error()) + return + } + require.NoError(t, err) + + body := "" + bus.AddHandlerCtx("test", func(ctx context.Context, webhook *models.SendWebhookSync) error { + body = webhook.Body + return nil + }) + + ctx := notify.WithGroupKey(context.Background(), "alertname") + ctx = notify.WithGroupLabels(ctx, model.LabelSet{"alertname": ""}) + ok, err := dn.Notify(ctx, c.alerts...) + if c.expMsgError != nil { + require.False(t, ok) + require.Error(t, err) + require.Equal(t, c.expMsgError.Error(), err.Error()) + return + } + require.NoError(t, err) + require.True(t, ok) + + expBody, err := json.Marshal(c.expMsg) + require.NoError(t, err) + + require.JSONEq(t, string(expBody), body) + }) + } +} diff --git a/pkg/tests/api/alerting/api_available_channel_test.go b/pkg/tests/api/alerting/api_available_channel_test.go index 1916f656184..45de805a252 100644 --- a/pkg/tests/api/alerting/api_available_channel_test.go +++ b/pkg/tests/api/alerting/api_available_channel_test.go @@ -1223,6 +1223,47 @@ var expAvailableChannelJsonOutput = ` "secure": false } ] + }, + { + "type": "discord", + "name": "Discord", + "heading": "Discord settings", + "description": "Sends notifications to Discord", + "info": "", + "options": [ + { + "label": "Message Content", + "description": "Mention a group using @ or a user using <@ID> when notifying in a channel", + "element": "input", + "inputType": "text", + "placeholder": "{{ template \"default.message\" . }}", + "propertyName": "message", + "selectOptions": null, + "showWhen": { + "field": "", + "is": "" + }, + "required": false, + "validationRule": "", + "secure": false + }, + { + "label": "Webhook URL", + "description": "", + "element": "input", + "inputType": "text", + "placeholder": "Discord webhook URL", + "propertyName": "url", + "selectOptions": null, + "showWhen": { + "field": "", + "is": "" + }, + "required": true, + "validationRule": "", + "secure": false + } + ] } ] `