grafana/pkg/services/alerting/notifiers/teams.go

145 lines
3.8 KiB
Go
Raw Normal View History

2017-10-30 10:31:24 -05:00
package notifiers
import (
"encoding/json"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
2017-10-30 10:31:24 -05:00
"github.com/grafana/grafana/pkg/services/alerting"
)
func init() {
alerting.RegisterNotifier(&alerting.NotifierPlugin{
Type: "teams",
Name: "Microsoft Teams",
2018-04-13 11:40:14 -05:00
Description: "Sends notifications using Incoming Webhook connector to Microsoft Teams",
Heading: "Teams settings",
2017-10-30 10:31:24 -05:00
Factory: NewTeamsNotifier,
Options: []alerting.NotifierOption{
{
Label: "URL",
Element: alerting.ElementTypeInput,
InputType: alerting.InputTypeText,
Placeholder: "Teams incoming webhook url",
PropertyName: "url",
Required: true,
},
},
2017-10-30 10:31:24 -05:00
})
}
// NewTeamsNotifier is the constructor for Teams notifier.
Encryption: Refactor securejsondata.SecureJsonData to stop relying on global functions (#38865) * Encryption: Add support to encrypt/decrypt sjd * Add datasources.Service as a proxy to datasources db operations * Encrypt ds.SecureJsonData before calling SQLStore * Move ds cache code into ds service * Fix tlsmanager tests * Fix pluginproxy tests * Remove some securejsondata.GetEncryptedJsonData usages * Add pluginsettings.Service as a proxy for plugin settings db operations * Add AlertNotificationService as a proxy for alert notification db operations * Remove some securejsondata.GetEncryptedJsonData usages * Remove more securejsondata.GetEncryptedJsonData usages * Fix lint errors * Minor fixes * Remove encryption global functions usages from ngalert * Fix lint errors * Minor fixes * Minor fixes * Remove securejsondata.DecryptedValue usage * Refactor the refactor * Remove securejsondata.DecryptedValue usage * Move securejsondata to migrations package * Move securejsondata to migrations package * Minor fix * Fix integration test * Fix integration tests * Undo undesired changes * Fix tests * Add context.Context into encryption methods * Fix tests * Fix tests * Fix tests * Trigger CI * Fix test * Add names to params of encryption service interface * Remove bus from CacheServiceImpl * Add logging * Add keys to logger Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Add missing key to logger Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Undo changes in markdown files * Fix formatting * Add context to secrets service * Rename decryptSecureJsonData to decryptSecureJsonDataFn * Name args in GetDecryptedValueFn * Add template back to NewAlertmanagerNotifier * Copy GetDecryptedValueFn to ngalert * Add logging to pluginsettings * Fix pluginsettings test Co-authored-by: Tania B <yalyna.ts@gmail.com> Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
2021-10-07 09:33:50 -05:00
func NewTeamsNotifier(model *models.AlertNotification, _ alerting.GetDecryptedValueFn) (alerting.Notifier, error) {
2017-10-30 10:31:24 -05:00
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
}
return &TeamsNotifier{
NotifierBase: NewNotifierBase(model),
URL: url,
2017-10-30 10:31:24 -05:00
log: log.New("alerting.notifier.teams"),
}, nil
}
// TeamsNotifier is responsible for sending
// alert notifications to Microsoft teams.
2017-10-30 10:31:24 -05:00
type TeamsNotifier struct {
NotifierBase
URL string
log log.Logger
2017-10-30 10:31:24 -05:00
}
// Notify send an alert notification to Microsoft teams.
func (tn *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error {
tn.log.Info("Executing teams notification", "ruleId", evalContext.Rule.ID, "notification", tn.Name)
2017-10-30 10:31:24 -05:00
ruleURL, err := evalContext.GetRuleURL()
2017-10-30 10:31:24 -05:00
if err != nil {
tn.log.Error("Failed get rule link", "error", err)
2017-10-30 10:31:24 -05:00
return err
}
fields := make([]map[string]interface{}, 0)
fieldLimitCount := 4
for index, evt := range evalContext.EvalMatches {
fields = append(fields, map[string]interface{}{
"name": evt.Metric,
"value": evt.Value,
})
if index > fieldLimitCount {
break
}
}
if evalContext.Error != nil {
fields = append(fields, map[string]interface{}{
"name": "Error message",
"value": evalContext.Error.Error(),
})
}
message := ""
if evalContext.Rule.State != models.AlertStateOK { // don't add message when going back to alert state ok.
message = evalContext.Rule.Message
2017-10-30 10:31:24 -05:00
}
2019-03-20 12:59:13 -05:00
images := make([]map[string]interface{}, 0)
if tn.NeedsImage() && evalContext.ImagePublicURL != "" {
2019-03-20 12:59:13 -05:00
images = append(images, map[string]interface{}{
"image": evalContext.ImagePublicURL,
2019-03-20 12:59:13 -05:00
})
}
2017-10-30 10:31:24 -05:00
body := map[string]interface{}{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
// summary MUST not be empty or the webhook request fails
// summary SHOULD contain some meaningful information, since it is used for mobile notifications
"summary": evalContext.GetNotificationTitle(),
2017-10-30 10:31:24 -05:00
"title": evalContext.GetNotificationTitle(),
"themeColor": evalContext.GetStateModel().Color,
"sections": []map[string]interface{}{
{
2019-03-20 12:42:48 -05:00
"title": "Details",
"facts": fields,
"images": images,
2019-03-20 12:42:48 -05:00
"text": message,
},
},
"potentialAction": []map[string]interface{}{
{
"@context": "http://schema.org",
"@type": "OpenUri",
"name": "View Rule",
"targets": []map[string]interface{}{
2018-09-07 04:06:19 -05:00
{
"os": "default", "uri": ruleURL,
},
},
},
{
"@context": "http://schema.org",
"@type": "OpenUri",
"name": "View Graph",
"targets": []map[string]interface{}{
2018-09-07 04:06:19 -05:00
{
"os": "default", "uri": evalContext.ImagePublicURL,
2017-10-30 10:31:24 -05:00
},
},
},
},
}
data, _ := json.Marshal(&body)
cmd := &models.SendWebhookSync{Url: tn.URL, Body: string(data)}
2017-10-30 10:31:24 -05:00
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
tn.log.Error("Failed to send teams notification", "error", err, "webhook", tn.Name)
2017-10-30 10:31:24 -05:00
return err
}
return nil
}