2016-07-27 05:09:55 -05:00
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2016-08-11 14:12:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
2016-07-27 05:09:55 -05:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
2016-08-12 09:44:53 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2016-07-27 05:09:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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{
|
2016-09-06 06:19:05 -05:00
|
|
|
NotifierBase: NewNotifierBase(model.Name, model.Type, model.Settings),
|
|
|
|
Addresses: strings.Split(addressesString, "\n"),
|
|
|
|
log: log.New("alerting.notifier.email"),
|
2016-07-27 05:09:55 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
func (this *EmailNotifier) Notify(evalContext *alerting.EvalContext) error {
|
2016-07-27 05:09:55 -05:00
|
|
|
this.log.Info("Sending alert notification to", "addresses", this.Addresses)
|
2016-08-11 14:12:39 -05:00
|
|
|
metrics.M_Alerting_Notification_Sent_Email.Inc(1)
|
2016-07-27 05:09:55 -05:00
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
ruleUrl, err := evalContext.GetRuleUrl()
|
2016-07-27 05:09:55 -05:00
|
|
|
if err != nil {
|
|
|
|
this.log.Error("Failed get rule link", "error", err)
|
2016-10-03 02:38:03 -05:00
|
|
|
return err
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
cmd := &m.SendEmailCommandSync{
|
|
|
|
SendEmailCommand: m.SendEmailCommand{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"Title": evalContext.GetNotificationTitle(),
|
|
|
|
"State": evalContext.Rule.State,
|
|
|
|
"Name": evalContext.Rule.Name,
|
|
|
|
"StateModel": evalContext.GetStateModel(),
|
|
|
|
"Message": evalContext.Rule.Message,
|
|
|
|
"RuleUrl": ruleUrl,
|
|
|
|
"ImageLink": evalContext.ImagePublicUrl,
|
|
|
|
"AlertPageUrl": setting.AppUrl + "alerting",
|
|
|
|
"EvalMatches": evalContext.EvalMatches,
|
|
|
|
},
|
|
|
|
To: this.Addresses,
|
|
|
|
Template: "alert_notification.html",
|
2016-07-27 05:09:55 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
err = bus.DispatchCtx(evalContext, cmd)
|
|
|
|
|
|
|
|
if err != nil {
|
2016-07-27 05:09:55 -05:00
|
|
|
this.log.Error("Failed to send alert notification email", "error", err)
|
|
|
|
}
|
2016-10-03 02:38:03 -05:00
|
|
|
return nil
|
|
|
|
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|