Commit 090594a0 by joe miller Committed by Torkel Ödegaard

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
parent 007c08f2
package notifiers package notifiers
import ( import (
"strconv"
"strings"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/metrics" "github.com/grafana/grafana/pkg/metrics"
m "github.com/grafana/grafana/pkg/models" m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting" "github.com/grafana/grafana/pkg/services/alerting"
"strconv"
"strings"
) )
func init() { func init() {
...@@ -24,6 +25,14 @@ func init() { ...@@ -24,6 +25,14 @@ func init() {
<input type="text" required class="gf-form-input max-width-26" ng-model="ctrl.model.settings.url" placeholder="http://sensu-api.local:4567/results"></input> <input type="text" required class="gf-form-input max-width-26" ng-model="ctrl.model.settings.url" placeholder="http://sensu-api.local:4567/results"></input>
</div> </div>
<div class="gf-form"> <div class="gf-form">
<span class="gf-form-label width-10">Source</span>
<input type="text" class="gf-form-input max-width-14" ng-model="ctrl.model.settings.source"></input>
</div>
<div class="gf-form">
<span class="gf-form-label width-10">Handler</span>
<input type="text" class="gf-form-input max-width-14" ng-model="ctrl.model.settings.handler" placeholder="default"></input>
</div>
<div class="gf-form">
<span class="gf-form-label width-10">Username</span> <span class="gf-form-label width-10">Username</span>
<input type="text" class="gf-form-input max-width-14" ng-model="ctrl.model.settings.username"></input> <input type="text" class="gf-form-input max-width-14" ng-model="ctrl.model.settings.username"></input>
</div> </div>
...@@ -46,7 +55,9 @@ func NewSensuNotifier(model *m.AlertNotification) (alerting.Notifier, error) { ...@@ -46,7 +55,9 @@ func NewSensuNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings), NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
Url: url, Url: url,
User: model.Settings.Get("username").MustString(), User: model.Settings.Get("username").MustString(),
Source: model.Settings.Get("source").MustString(),
Password: model.Settings.Get("password").MustString(), Password: model.Settings.Get("password").MustString(),
Handler: model.Settings.Get("handler").MustString(),
log: log.New("alerting.notifier.sensu"), log: log.New("alerting.notifier.sensu"),
}, nil }, nil
} }
...@@ -54,8 +65,10 @@ func NewSensuNotifier(model *m.AlertNotification) (alerting.Notifier, error) { ...@@ -54,8 +65,10 @@ func NewSensuNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
type SensuNotifier struct { type SensuNotifier struct {
NotifierBase NotifierBase
Url string Url string
Source string
User string User string
Password string Password string
Handler string
log log.Logger log log.Logger
} }
...@@ -67,9 +80,13 @@ func (this *SensuNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -67,9 +80,13 @@ func (this *SensuNotifier) Notify(evalContext *alerting.EvalContext) error {
bodyJSON.Set("ruleId", evalContext.Rule.Id) bodyJSON.Set("ruleId", evalContext.Rule.Id)
// Sensu alerts cannot have spaces in them // Sensu alerts cannot have spaces in them
bodyJSON.Set("name", strings.Replace(evalContext.Rule.Name, " ", "_", -1)) bodyJSON.Set("name", strings.Replace(evalContext.Rule.Name, " ", "_", -1))
// Sensu alerts require a command // Sensu alerts require a source. We set it to the user-specified value (optional),
// We set it to the grafana ruleID // else we fallback and use the grafana ruleID.
bodyJSON.Set("source", "grafana_rule_"+strconv.FormatInt(evalContext.Rule.Id, 10)) 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 // Finally, sensu expects an output
// We set it to a default output // We set it to a default output
bodyJSON.Set("output", "Grafana Metric Condition Met") bodyJSON.Set("output", "Grafana Metric Condition Met")
...@@ -83,6 +100,10 @@ func (this *SensuNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -83,6 +100,10 @@ func (this *SensuNotifier) Notify(evalContext *alerting.EvalContext) error {
bodyJSON.Set("status", 0) bodyJSON.Set("status", 0)
} }
if this.Handler != "" {
bodyJSON.Set("handler", this.Handler)
}
ruleUrl, err := evalContext.GetRuleUrl() ruleUrl, err := evalContext.GetRuleUrl()
if err == nil { if err == nil {
bodyJSON.Set("ruleUrl", ruleUrl) bodyJSON.Set("ruleUrl", ruleUrl)
......
...@@ -29,7 +29,9 @@ func TestSensuNotifier(t *testing.T) { ...@@ -29,7 +29,9 @@ func TestSensuNotifier(t *testing.T) {
Convey("from settings", func() { Convey("from settings", func() {
json := ` 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)) settingsJSON, _ := simplejson.NewJson([]byte(json))
...@@ -46,6 +48,8 @@ func TestSensuNotifier(t *testing.T) { ...@@ -46,6 +48,8 @@ func TestSensuNotifier(t *testing.T) {
So(sensuNotifier.Name, ShouldEqual, "sensu") So(sensuNotifier.Name, ShouldEqual, "sensu")
So(sensuNotifier.Type, ShouldEqual, "sensu") So(sensuNotifier.Type, ShouldEqual, "sensu")
So(sensuNotifier.Url, ShouldEqual, "http://sensu-api.example.com:4567/results") So(sensuNotifier.Url, ShouldEqual, "http://sensu-api.example.com:4567/results")
So(sensuNotifier.Source, ShouldEqual, "grafana_instance_01")
So(sensuNotifier.Handler, ShouldEqual, "myhandler")
}) })
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment