From 090594a0bc1079c9c345905cb85c4ed5b8ef2ffe Mon Sep 17 00:00:00 2001 From: joe miller Date: Wed, 24 May 2017 04:42:24 -0700 Subject: [PATCH] support setting the source and handler attribute in sensu notifications (#8405) * support setting the handler attribute in sensu alert notifications * allow the user to set the source attribute of Sensu notifications --- pkg/services/alerting/notifiers/sensu.go | 31 ++++++++++++++++--- pkg/services/alerting/notifiers/sensu_test.go | 6 +++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/pkg/services/alerting/notifiers/sensu.go b/pkg/services/alerting/notifiers/sensu.go index dbe31f4cf84..4f3930f5567 100644 --- a/pkg/services/alerting/notifiers/sensu.go +++ b/pkg/services/alerting/notifiers/sensu.go @@ -1,14 +1,15 @@ package notifiers import ( + "strconv" + "strings" + "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/metrics" m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/alerting" - "strconv" - "strings" ) func init() { @@ -23,6 +24,14 @@ func init() { Url +
+ Source + +
+
+ Handler + +
Username @@ -46,7 +55,9 @@ func NewSensuNotifier(model *m.AlertNotification) (alerting.Notifier, error) { NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings), Url: url, User: model.Settings.Get("username").MustString(), + Source: model.Settings.Get("source").MustString(), Password: model.Settings.Get("password").MustString(), + Handler: model.Settings.Get("handler").MustString(), log: log.New("alerting.notifier.sensu"), }, nil } @@ -54,8 +65,10 @@ func NewSensuNotifier(model *m.AlertNotification) (alerting.Notifier, error) { type SensuNotifier struct { NotifierBase Url string + Source string User string Password string + Handler string log log.Logger } @@ -67,9 +80,13 @@ func (this *SensuNotifier) Notify(evalContext *alerting.EvalContext) error { bodyJSON.Set("ruleId", evalContext.Rule.Id) // Sensu alerts cannot have spaces in them bodyJSON.Set("name", strings.Replace(evalContext.Rule.Name, " ", "_", -1)) - // Sensu alerts require a command - // We set it to the grafana ruleID - bodyJSON.Set("source", "grafana_rule_"+strconv.FormatInt(evalContext.Rule.Id, 10)) + // Sensu alerts require a source. We set it to the user-specified value (optional), + // else we fallback and use the grafana ruleID. + if this.Source != "" { + bodyJSON.Set("source", this.Source) + } else { + bodyJSON.Set("source", "grafana_rule_"+strconv.FormatInt(evalContext.Rule.Id, 10)) + } // Finally, sensu expects an output // We set it to a default output bodyJSON.Set("output", "Grafana Metric Condition Met") @@ -83,6 +100,10 @@ func (this *SensuNotifier) Notify(evalContext *alerting.EvalContext) error { bodyJSON.Set("status", 0) } + if this.Handler != "" { + bodyJSON.Set("handler", this.Handler) + } + ruleUrl, err := evalContext.GetRuleUrl() if err == nil { bodyJSON.Set("ruleUrl", ruleUrl) diff --git a/pkg/services/alerting/notifiers/sensu_test.go b/pkg/services/alerting/notifiers/sensu_test.go index ffbdcfaf15c..40e3b1e1cc3 100644 --- a/pkg/services/alerting/notifiers/sensu_test.go +++ b/pkg/services/alerting/notifiers/sensu_test.go @@ -29,7 +29,9 @@ func TestSensuNotifier(t *testing.T) { Convey("from settings", func() { json := ` { - "url": "http://sensu-api.example.com:4567/results" + "url": "http://sensu-api.example.com:4567/results", + "source": "grafana_instance_01", + "handler": "myhandler" }` settingsJSON, _ := simplejson.NewJson([]byte(json)) @@ -46,6 +48,8 @@ func TestSensuNotifier(t *testing.T) { So(sensuNotifier.Name, ShouldEqual, "sensu") So(sensuNotifier.Type, ShouldEqual, "sensu") So(sensuNotifier.Url, ShouldEqual, "http://sensu-api.example.com:4567/results") + So(sensuNotifier.Source, ShouldEqual, "grafana_instance_01") + So(sensuNotifier.Handler, ShouldEqual, "myhandler") }) }) })