diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index f2a01c4d6bf..8eb4eb4baea 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -45,6 +45,7 @@ var ( M_Alerting_Notification_Sent_Email Counter M_Alerting_Notification_Sent_Webhook Counter M_Alerting_Notification_Sent_PagerDuty Counter + M_Alerting_Notification_Sent_Victorops Counter // Timers M_DataSource_ProxyReq_Timer Timer @@ -108,6 +109,7 @@ func initMetricVars(settings *MetricSettings) { M_Alerting_Notification_Sent_Email = RegCounter("alerting.notifications_sent", "type", "email") M_Alerting_Notification_Sent_Webhook = RegCounter("alerting.notifications_sent", "type", "webhook") M_Alerting_Notification_Sent_PagerDuty = RegCounter("alerting.notifications_sent", "type", "pagerduty") + M_Alerting_Notification_Sent_Victorops = RegCounter("alerting.notifications_sent", "type", "victorops") // Timers M_DataSource_ProxyReq_Timer = RegTimer("api.dataproxy.request.all") diff --git a/pkg/services/alerting/notifiers/victorops.go b/pkg/services/alerting/notifiers/victorops.go new file mode 100644 index 00000000000..d4dfa8c6678 --- /dev/null +++ b/pkg/services/alerting/notifiers/victorops.go @@ -0,0 +1,104 @@ +package notifiers + +import ( + "encoding/json" + "time" + + "github.com/grafana/grafana/pkg/bus" + "github.com/grafana/grafana/pkg/log" + "github.com/grafana/grafana/pkg/metrics" + "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/services/alerting" + "github.com/grafana/grafana/pkg/setting" +) + +// AlertStateCritical - Victorops uses "CRITICAL" string to indicate "Alerting" state +const AlertStateCritical = "CRITICAL" + +func init() { + alerting.RegisterNotifier("victorops", NewVictoropsNotifier) +} + +// NewVictoropsNotifier creates an instance of VictoropsNotifier that +// handles posting notifications to Victorops REST API +func NewVictoropsNotifier(model *models.AlertNotification) (alerting.Notifier, error) { + url := model.Settings.Get("url").MustString() + if url == "" { + return nil, alerting.ValidationError{Reason: "Could not find victorops url property in settings"} + } + + alertOnExecError := model.Settings.Get("alertOnExecError").MustBool() + + return &VictoropsNotifier{ + NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings), + URL: url, + AlertOnExecError: alertOnExecError, + log: log.New("alerting.notifier.victorops"), + }, nil +} + +// VictoropsNotifier defines URL property for Victorops REST API +// and handles notification process by formatting POST body according to +// Victorops specifications (http://victorops.force.com/knowledgebase/articles/Integration/Alert-Ingestion-API-Documentation/) +type VictoropsNotifier struct { + NotifierBase + URL string + AlertOnExecError bool + log log.Logger +} + +// Notify sends notification to Victorops via POST to URL endpoint +func (this *VictoropsNotifier) Notify(evalContext *alerting.EvalContext) error { + this.log.Info("Executing victorops notification", "ruleId", evalContext.Rule.Id, "notification", this.Name) + metrics.M_Alerting_Notification_Sent_Victorops.Inc(1) + + ruleUrl, err := evalContext.GetRuleUrl() + if err != nil { + this.log.Error("Failed get rule link", "error", err) + return err + } + + fields := make([]map[string]interface{}, 0) + fieldLimitCount := 4 + for index, evt := range evalContext.EvalMatches { + fields = append(fields, map[string]interface{}{ + "title": evt.Metric, + "value": evt.Value, + "short": true, + }) + if index > fieldLimitCount { + break + } + } + + if evalContext.Error != nil { + fields = append(fields, map[string]interface{}{ + "title": "Error message", + "value": evalContext.Error.Error(), + "short": false, + }) + } + + messageType := evalContext.Rule.State + if evalContext.Rule.State == models.AlertStateAlerting { // translate 'Alerting' to 'CRITICAL' (Victorops analog) + messageType = AlertStateCritical + } + + body := map[string]interface{}{ + "message_type": messageType, + "entity_id": evalContext.Rule.Name, + "timestamp": time.Now().Unix(), + "state_start_time": evalContext.StartTime.Unix(), + "state_message": evalContext.Rule.Message + "\n" + ruleUrl, + "monitoring_tool": "Grafana v" + setting.BuildVersion, + } + + data, _ := json.Marshal(&body) + cmd := &models.SendWebhookSync{Url: this.URL, Body: string(data)} + + if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil { + this.log.Error("Failed to send victorops notification", "error", err, "webhook", this.Name) + } + + return nil +} diff --git a/pkg/services/alerting/notifiers/victorops_test.go b/pkg/services/alerting/notifiers/victorops_test.go new file mode 100644 index 00000000000..6ac806a82cc --- /dev/null +++ b/pkg/services/alerting/notifiers/victorops_test.go @@ -0,0 +1,52 @@ +package notifiers + +import ( + "testing" + + "github.com/grafana/grafana/pkg/components/simplejson" + m "github.com/grafana/grafana/pkg/models" + . "github.com/smartystreets/goconvey/convey" +) + +func TestVictoropsNotifier(t *testing.T) { + Convey("Victorops notifier tests", t, func() { + + Convey("Parsing alert notification from settings", func() { + Convey("empty settings should return error", func() { + json := `{ }` + + settingsJSON, _ := simplejson.NewJson([]byte(json)) + model := &m.AlertNotification{ + Name: "victorops_testing", + Type: "victorops", + Settings: settingsJSON, + } + + _, err := NewVictoropsNotifier(model) + So(err, ShouldNotBeNil) + }) + + Convey("from settings", func() { + json := ` + { + "url": "http://google.com" + }` + + settingsJSON, _ := simplejson.NewJson([]byte(json)) + model := &m.AlertNotification{ + Name: "victorops_testing", + Type: "victorops", + Settings: settingsJSON, + } + + not, err := NewVictoropsNotifier(model) + victoropsNotifier := not.(*VictoropsNotifier) + + So(err, ShouldBeNil) + So(victoropsNotifier.Name, ShouldEqual, "victorops_testing") + So(victoropsNotifier.Type, ShouldEqual, "victorops") + So(victoropsNotifier.URL, ShouldEqual, "http://google.com") + }) + }) + }) +} diff --git a/public/app/features/alerting/alert_tab_ctrl.ts b/public/app/features/alerting/alert_tab_ctrl.ts index 6757cfc0d91..426ff62382f 100644 --- a/public/app/features/alerting/alert_tab_ctrl.ts +++ b/public/app/features/alerting/alert_tab_ctrl.ts @@ -91,6 +91,7 @@ export class AlertTabCtrl { switch (type) { case "email": return "fa fa-envelope"; case "slack": return "fa fa-slack"; + case "victorops": return "fa fa-pagelines"; case "webhook": return "fa fa-cubes"; case "pagerduty": return "fa fa-bullhorn"; } diff --git a/public/app/features/alerting/partials/notification_edit.html b/public/app/features/alerting/partials/notification_edit.html index 42f1f553d71..ac4afbf56fc 100644 --- a/public/app/features/alerting/partials/notification_edit.html +++ b/public/app/features/alerting/partials/notification_edit.html @@ -19,7 +19,7 @@