2016-07-27 05:09:55 -05:00
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
alerting.RegisterNotifier("email", NewEmailNotifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
type EmailNotifier struct {
|
|
|
|
NotifierBase
|
|
|
|
Addresses []string
|
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEmailNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
|
|
|
|
addressesString := model.Settings.Get("addresses").MustString()
|
|
|
|
|
|
|
|
if addressesString == "" {
|
2016-07-27 09:29:28 -05:00
|
|
|
return nil, alerting.ValidationError{Reason: "Could not find addresses in settings"}
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return &EmailNotifier{
|
|
|
|
NotifierBase: NotifierBase{
|
|
|
|
Name: model.Name,
|
|
|
|
Type: model.Type,
|
|
|
|
},
|
|
|
|
Addresses: strings.Split(addressesString, "\n"),
|
|
|
|
log: log.New("alerting.notifier.email"),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-07-27 09:29:28 -05:00
|
|
|
func (this *EmailNotifier) Notify(context *alerting.EvalContext) {
|
2016-07-27 05:09:55 -05:00
|
|
|
this.log.Info("Sending alert notification to", "addresses", this.Addresses)
|
|
|
|
|
2016-07-30 06:36:21 -05:00
|
|
|
ruleUrl, err := context.GetRuleUrl()
|
2016-07-27 05:09:55 -05:00
|
|
|
if err != nil {
|
|
|
|
this.log.Error("Failed get rule link", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &m.SendEmailCommand{
|
|
|
|
Data: map[string]interface{}{
|
2016-08-01 03:07:00 -05:00
|
|
|
"Title": context.GetNotificationTitle(),
|
2016-07-27 05:09:55 -05:00
|
|
|
"RuleState": context.Rule.State,
|
|
|
|
"RuleName": context.Rule.Name,
|
|
|
|
"Severity": context.Rule.Severity,
|
2016-07-30 06:36:21 -05:00
|
|
|
"RuleUrl": ruleUrl,
|
2016-07-27 05:09:55 -05:00
|
|
|
},
|
|
|
|
To: this.Addresses,
|
|
|
|
Template: "alert_notification.html",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(cmd); err != nil {
|
|
|
|
this.log.Error("Failed to send alert notification email", "error", err)
|
|
|
|
}
|
|
|
|
}
|