feat(alerting): add support for email notifications
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/alerting/alertstates"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
type NotifierImpl struct {
|
||||
@@ -50,18 +51,28 @@ type EmailNotifier struct {
|
||||
|
||||
func (this *EmailNotifier) Dispatch(alertResult *AlertResult) {
|
||||
this.log.Info("Sending email")
|
||||
cmd := &m.SendEmailCommand{
|
||||
Data: map[string]interface{}{
|
||||
"Description": alertResult.Description,
|
||||
"TriggeredAlerts": alertResult.TriggeredAlerts,
|
||||
},
|
||||
To: []string{this.To},
|
||||
Info: "Alert result",
|
||||
Massive: false,
|
||||
Template: "",
|
||||
grafanaUrl := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
|
||||
if setting.AppSubUrl != "" {
|
||||
grafanaUrl += "/" + setting.AppSubUrl
|
||||
}
|
||||
|
||||
bus.Dispatch(cmd)
|
||||
cmd := &m.SendEmailCommand{
|
||||
Data: map[string]interface{}{
|
||||
"Name": "Name",
|
||||
"State": alertResult.State,
|
||||
"Description": alertResult.Description,
|
||||
"TriggeredAlerts": alertResult.TriggeredAlerts,
|
||||
"DashboardLink": grafanaUrl + "/dashboard/db/alerting",
|
||||
"AlertPageUrl": grafanaUrl + "/alerting",
|
||||
},
|
||||
To: []string{this.To},
|
||||
Template: "alert_notification.html",
|
||||
}
|
||||
|
||||
err := bus.Dispatch(cmd)
|
||||
if err != nil {
|
||||
this.log.Error("Could not send alert notification as email", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
type WebhookNotifier struct {
|
||||
|
||||
@@ -9,6 +9,12 @@ import (
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
type testTriggeredAlert struct {
|
||||
ActualValue float64
|
||||
Name string
|
||||
State string
|
||||
}
|
||||
|
||||
func TestNotifications(t *testing.T) {
|
||||
|
||||
Convey("Given the notifications service", t, func() {
|
||||
@@ -34,6 +40,83 @@ func TestNotifications(t *testing.T) {
|
||||
So(sentMsg.Subject, ShouldEqual, "Reset your Grafana password - asd@asd.com")
|
||||
So(sentMsg.Body, ShouldNotContainSubstring, "Subject")
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Alert notifications", func() {
|
||||
Convey("When sending reset email password", func() {
|
||||
cmd := &m.SendEmailCommand{
|
||||
Data: map[string]interface{}{
|
||||
"Name": "Name",
|
||||
"State": "Critical",
|
||||
"Description": "Description",
|
||||
"DashboardLink": "http://localhost:3000/dashboard/db/alerting",
|
||||
"AlertPageUrl": "http://localhost:3000/alerting",
|
||||
"TriggeredAlerts": []testTriggeredAlert{
|
||||
{Name: "desktop", State: "Critical", ActualValue: 13},
|
||||
{Name: "mobile", State: "Warn", ActualValue: 5},
|
||||
},
|
||||
},
|
||||
To: []string{"asd@asd.com "},
|
||||
Template: "alert_notification.html",
|
||||
}
|
||||
|
||||
err := sendEmailCommandHandler(cmd)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(sentMsg.Body, ShouldContainSubstring, "Alertstate: Critical")
|
||||
So(sentMsg.Body, ShouldContainSubstring, "http://localhost:3000/dashboard/db/alerting")
|
||||
So(sentMsg.Body, ShouldContainSubstring, "Critical")
|
||||
So(sentMsg.Body, ShouldContainSubstring, "Warn")
|
||||
So(sentMsg.Body, ShouldContainSubstring, "mobile")
|
||||
So(sentMsg.Body, ShouldContainSubstring, "desktop")
|
||||
|
||||
So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Critical ] ")
|
||||
})
|
||||
|
||||
Convey("given critical", func() {
|
||||
cmd := &m.SendEmailCommand{
|
||||
Data: map[string]interface{}{
|
||||
"Name": "Name",
|
||||
"State": "Warn",
|
||||
"Description": "Description",
|
||||
"DashboardLink": "http://localhost:3000/dashboard/db/alerting",
|
||||
"AlertPageUrl": "http://localhost:3000/alerting",
|
||||
"TriggeredAlerts": []testTriggeredAlert{
|
||||
{Name: "desktop", State: "Critical", ActualValue: 13},
|
||||
{Name: "mobile", State: "Warn", ActualValue: 5},
|
||||
},
|
||||
},
|
||||
To: []string{"asd@asd.com "},
|
||||
Template: "alert_notification.html",
|
||||
}
|
||||
|
||||
err := sendEmailCommandHandler(cmd)
|
||||
So(err, ShouldBeNil)
|
||||
So(sentMsg.Body, ShouldContainSubstring, "Alertstate: Warn")
|
||||
So(sentMsg.Body, ShouldContainSubstring, "http://localhost:3000/dashboard/db/alerting")
|
||||
So(sentMsg.Body, ShouldContainSubstring, "Critical")
|
||||
So(sentMsg.Body, ShouldContainSubstring, "Warn")
|
||||
So(sentMsg.Body, ShouldContainSubstring, "mobile")
|
||||
So(sentMsg.Body, ShouldContainSubstring, "desktop")
|
||||
So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Warn ]")
|
||||
})
|
||||
|
||||
Convey("given ok", func() {
|
||||
cmd := &m.SendEmailCommand{
|
||||
Data: map[string]interface{}{
|
||||
"Name": "Name",
|
||||
"State": "Ok",
|
||||
"Description": "Description",
|
||||
"DashboardLink": "http://localhost:3000/dashboard/db/alerting",
|
||||
"AlertPageUrl": "http://localhost:3000/alerting",
|
||||
},
|
||||
To: []string{"asd@asd.com "},
|
||||
Template: "alert_notification.html",
|
||||
}
|
||||
|
||||
err := sendEmailCommandHandler(cmd)
|
||||
So(err, ShouldBeNil)
|
||||
So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Ok ]")
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user