Просмотр исходного кода

Merge branch 'Thib17-master'

* Thib17-master:
  alertmanager: endAt should only be used if we have the correct value
  alertmanager: code style
  alerting: reduce log level for notifiers
  Alertmanager notifier: add "metric" labels if no tags
  Alertmanager notifier: make it match the new notifier interface
  support alertmanager
bergquist 8 лет назад
Родитель
Сommit
a7d746375d

+ 1 - 1
pkg/services/alerting/notifier.go

@@ -65,7 +65,7 @@ func (n *notificationService) sendNotifications(context *EvalContext, notifiers
 
 	for _, notifier := range notifiers {
 		not := notifier //avoid updating scope variable in go routine
-		n.log.Info("Sending notification", "type", not.GetType(), "id", not.GetNotifierId(), "isDefault", not.GetIsDefault())
+		n.log.Debug("Sending notification", "type", not.GetType(), "id", not.GetNotifierId(), "isDefault", not.GetIsDefault())
 		metrics.M_Alerting_Notification_Sent.WithLabelValues(not.GetType()).Inc()
 		g.Go(func() error { return not.Notify(context) })
 	}

+ 96 - 0
pkg/services/alerting/notifiers/alertmanager.go

@@ -0,0 +1,96 @@
+package notifiers
+
+import (
+	"time"
+
+	"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(&alerting.NotifierPlugin{
+		Type:        "prometheus-alertmanager",
+		Name:        "Prometheus Alertmanager",
+		Description: "Sends alert to Prometheus Alertmanager",
+		Factory:     NewAlertmanagerNotifier,
+		OptionsTemplate: `
+      <h3 class="page-heading">Alertmanager settings</h3>
+      <div class="gf-form">
+        <span class="gf-form-label width-10">Url</span>
+        <input type="text" required class="gf-form-input max-width-26" ng-model="ctrl.model.settings.url" placeholder="http://localhost:9093"></input>
+      </div>
+    `,
+	})
+}
+
+func NewAlertmanagerNotifier(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 &AlertmanagerNotifier{
+		NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
+		Url:          url,
+		log:          log.New("alerting.notifier.prometheus-alertmanager"),
+	}, nil
+}
+
+type AlertmanagerNotifier struct {
+	NotifierBase
+	Url string
+	log log.Logger
+}
+
+func (this *AlertmanagerNotifier) ShouldNotify(evalContext *alerting.EvalContext) bool {
+	return evalContext.Rule.State == m.AlertStateAlerting
+}
+
+func (this *AlertmanagerNotifier) Notify(evalContext *alerting.EvalContext) error {
+
+	alerts := make([]interface{}, 0)
+	for _, match := range evalContext.EvalMatches {
+		alertJSON := simplejson.New()
+		alertJSON.Set("startsAt", evalContext.StartTime.UTC().Format(time.RFC3339))
+
+		if ruleUrl, err := evalContext.GetRuleUrl(); err == nil {
+			alertJSON.Set("generatorURL", ruleUrl)
+		}
+
+		if evalContext.Rule.Message != "" {
+			alertJSON.SetPath([]string{"annotations", "description"}, evalContext.Rule.Message)
+		}
+
+		tags := make(map[string]string)
+		if len(match.Tags) == 0 {
+			tags["metric"] = match.Metric
+		} else {
+			for k, v := range match.Tags {
+				tags[k] = v
+			}
+		}
+		tags["alertname"] = evalContext.Rule.Name
+		alertJSON.Set("labels", tags)
+
+		alerts = append(alerts, alertJSON)
+	}
+
+	bodyJSON := simplejson.NewFromAny(alerts)
+	body, _ := bodyJSON.MarshalJSON()
+
+	cmd := &m.SendWebhookSync{
+		Url:        this.Url + "/api/v1/alerts",
+		HttpMethod: "POST",
+		Body:       string(body),
+	}
+
+	if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
+		this.log.Error("Failed to send alertmanager", "error", err, "alertmanager", this.Name)
+		return err
+	}
+
+	return nil
+}

+ 47 - 0
pkg/services/alerting/notifiers/alertmanager_test.go

@@ -0,0 +1,47 @@
+package notifiers
+
+import (
+	"testing"
+
+	"github.com/grafana/grafana/pkg/components/simplejson"
+	m "github.com/grafana/grafana/pkg/models"
+	. "github.com/smartystreets/goconvey/convey"
+)
+
+func TestAlertmanagerNotifier(t *testing.T) {
+	Convey("Alertmanager 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:     "alertmanager",
+					Type:     "alertmanager",
+					Settings: settingsJSON,
+				}
+
+				_, err := NewAlertmanagerNotifier(model)
+				So(err, ShouldNotBeNil)
+			})
+
+			Convey("from settings", func() {
+				json := `{ "url": "http://127.0.0.1:9093/" }`
+
+				settingsJSON, _ := simplejson.NewJson([]byte(json))
+				model := &m.AlertNotification{
+					Name:     "alertmanager",
+					Type:     "alertmanager",
+					Settings: settingsJSON,
+				}
+
+				not, err := NewAlertmanagerNotifier(model)
+				alertmanagerNotifier := not.(*AlertmanagerNotifier)
+
+				So(err, ShouldBeNil)
+				So(alertmanagerNotifier.Url, ShouldEqual, "http://127.0.0.1:9093/")
+			})
+		})
+	})
+}

+ 1 - 1
pkg/services/alerting/notifiers/base.go

@@ -15,7 +15,7 @@ type NotifierBase struct {
 }
 
 func NewNotifierBase(id int64, isDefault bool, name, notifierType string, model *simplejson.Json) NotifierBase {
-	uploadImage := model.Get("uploadImage").MustBool(true)
+	uploadImage := model.Get("uploadImage").MustBool(false)
 
 	return NotifierBase{
 		Id:          id,

+ 2 - 4
pkg/services/alerting/notifiers/dingding_test.go

@@ -25,10 +25,8 @@ func TestDingDingNotifier(t *testing.T) {
 
 		})
 		Convey("settings should trigger incident", func() {
-			json := `
-			{
-  "url": "https://www.google.com"
-			}`
+			json := `{ "url": "https://www.google.com" }`
+
 			settingsJSON, _ := simplejson.NewJson([]byte(json))
 			model := &m.AlertNotification{
 				Name:     "dingding_testing",