2016-07-27 12:09:55 +02:00
|
|
|
package notifiers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2016-08-11 21:12:39 +02:00
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
2016-07-27 12:09:55 +02:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
alerting.RegisterNotifier("webhook", NewWebHookNotifier)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewWebHookNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
|
|
|
|
|
url := model.Settings.Get("url").MustString()
|
|
|
|
|
if url == "" {
|
2016-07-27 16:29:28 +02:00
|
|
|
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
|
2016-07-27 12:09:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &WebhookNotifier{
|
2016-10-11 10:13:19 +02:00
|
|
|
NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
|
2016-09-06 13:19:05 +02:00
|
|
|
Url: url,
|
|
|
|
|
User: model.Settings.Get("user").MustString(),
|
|
|
|
|
Password: model.Settings.Get("password").MustString(),
|
2016-10-18 16:18:16 +02:00
|
|
|
HttpMethod: model.Settings.Get("httpMethod").MustString("POST"),
|
2016-09-06 13:19:05 +02:00
|
|
|
log: log.New("alerting.notifier.webhook"),
|
2016-07-27 12:09:55 +02:00
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type WebhookNotifier struct {
|
|
|
|
|
NotifierBase
|
2016-10-18 16:18:16 +02:00
|
|
|
Url string
|
|
|
|
|
User string
|
|
|
|
|
Password string
|
|
|
|
|
HttpMethod string
|
|
|
|
|
log log.Logger
|
2016-07-27 12:09:55 +02:00
|
|
|
}
|
|
|
|
|
|
2016-10-03 09:38:03 +02:00
|
|
|
func (this *WebhookNotifier) Notify(evalContext *alerting.EvalContext) error {
|
2016-07-27 12:09:55 +02:00
|
|
|
this.log.Info("Sending webhook")
|
2016-08-11 21:12:39 +02:00
|
|
|
metrics.M_Alerting_Notification_Sent_Webhook.Inc(1)
|
2016-07-27 12:09:55 +02:00
|
|
|
|
|
|
|
|
bodyJSON := simplejson.New()
|
2016-10-03 09:38:03 +02:00
|
|
|
bodyJSON.Set("title", evalContext.GetNotificationTitle())
|
|
|
|
|
bodyJSON.Set("ruleId", evalContext.Rule.Id)
|
|
|
|
|
bodyJSON.Set("ruleName", evalContext.Rule.Name)
|
|
|
|
|
bodyJSON.Set("state", evalContext.Rule.State)
|
|
|
|
|
bodyJSON.Set("evalMatches", evalContext.EvalMatches)
|
2016-08-22 15:11:27 +02:00
|
|
|
|
2016-10-03 09:38:03 +02:00
|
|
|
ruleUrl, err := evalContext.GetRuleUrl()
|
2016-08-22 15:11:27 +02:00
|
|
|
if err == nil {
|
2016-11-08 14:53:13 +01:00
|
|
|
bodyJSON.Set("ruleUrl", ruleUrl)
|
2016-08-22 15:11:27 +02:00
|
|
|
}
|
|
|
|
|
|
2016-10-03 09:38:03 +02:00
|
|
|
if evalContext.ImagePublicUrl != "" {
|
2016-11-08 14:53:13 +01:00
|
|
|
bodyJSON.Set("imageUrl", evalContext.ImagePublicUrl)
|
2016-08-22 15:11:27 +02:00
|
|
|
}
|
2016-07-27 12:09:55 +02:00
|
|
|
|
|
|
|
|
body, _ := bodyJSON.MarshalJSON()
|
|
|
|
|
|
2016-10-03 09:38:03 +02:00
|
|
|
cmd := &m.SendWebhookSync{
|
2016-10-18 16:18:16 +02:00
|
|
|
Url: this.Url,
|
|
|
|
|
User: this.User,
|
|
|
|
|
Password: this.Password,
|
|
|
|
|
Body: string(body),
|
|
|
|
|
HttpMethod: this.HttpMethod,
|
2016-07-27 12:09:55 +02:00
|
|
|
}
|
|
|
|
|
|
2016-10-03 17:03:21 +02:00
|
|
|
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
|
2016-07-27 12:09:55 +02:00
|
|
|
this.log.Error("Failed to send webhook", "error", err, "webhook", this.Name)
|
|
|
|
|
}
|
2016-10-03 09:38:03 +02:00
|
|
|
|
|
|
|
|
return nil
|
2016-07-27 12:09:55 +02:00
|
|
|
}
|