mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package notifiers
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
"github.com/grafana/grafana/pkg/log"
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
|
)
|
|
|
|
func init() {
|
|
alerting.RegisterNotifier("slack", NewSlackNotifier)
|
|
}
|
|
|
|
func NewSlackNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
|
|
url := model.Settings.Get("url").MustString()
|
|
if url == "" {
|
|
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
|
|
}
|
|
|
|
return &SlackNotifier{
|
|
NotifierBase: NotifierBase{
|
|
Name: model.Name,
|
|
Type: model.Type,
|
|
},
|
|
Url: url,
|
|
log: log.New("alerting.notifier.slack"),
|
|
}, nil
|
|
}
|
|
|
|
type SlackNotifier struct {
|
|
NotifierBase
|
|
Url string
|
|
log log.Logger
|
|
}
|
|
|
|
func (this *SlackNotifier) Notify(context *alerting.EvalContext) {
|
|
this.log.Info("Executing slack notification", "ruleId", context.Rule.Id, "notification", this.Name)
|
|
|
|
rule := context.Rule
|
|
|
|
ruleLink, err := getRuleLink(rule)
|
|
if err != nil {
|
|
this.log.Error("Failed get rule link", "error", err)
|
|
return
|
|
}
|
|
|
|
stateText := string(rule.Severity)
|
|
if !context.Firing {
|
|
stateText = "ok"
|
|
}
|
|
|
|
text := fmt.Sprintf("[%s]: <%s|%s>", stateText, ruleLink, rule.Name)
|
|
|
|
body := simplejson.New()
|
|
body.Set("text", text)
|
|
|
|
data, _ := body.MarshalJSON()
|
|
cmd := &m.SendWebhook{Url: this.Url, Body: string(data)}
|
|
|
|
if err := bus.Dispatch(cmd); err != nil {
|
|
this.log.Error("Failed to send slack notification", "error", err, "webhook", this.Name)
|
|
}
|
|
}
|