2016-07-27 05:09:55 -05:00
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
2016-10-20 08:06:59 -05:00
|
|
|
"os"
|
2016-07-27 05:09:55 -05:00
|
|
|
"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() {
|
2017-01-06 05:04:25 -06:00
|
|
|
alerting.RegisterNotifier(&alerting.NotifierPlugin{
|
|
|
|
Type: "email",
|
|
|
|
Name: "Email",
|
|
|
|
Description: "Sends notifications using Grafana server configured STMP settings",
|
|
|
|
Factory: NewEmailNotifier,
|
|
|
|
OptionsTemplate: `
|
|
|
|
<h3 class="page-heading">Email addresses</h3>
|
|
|
|
<div class="gf-form">
|
|
|
|
<textarea rows="7" class="gf-form-input width-25" required ng-model="ctrl.model.settings.addresses"></textarea>
|
|
|
|
</div>
|
|
|
|
<div class="gf-form">
|
|
|
|
<span>You can enter multiple email addresses using a ";" separator</span>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
})
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-10-22 03:24:31 -05:00
|
|
|
// split addresses with a few different ways
|
|
|
|
addresses := strings.FieldsFunc(addressesString, func(r rune) bool {
|
|
|
|
switch r {
|
|
|
|
case ',', ';', '\n':
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2016-07-27 05:09:55 -05:00
|
|
|
return &EmailNotifier{
|
2016-10-11 03:13:19 -05:00
|
|
|
NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
|
2016-10-22 03:24:31 -05:00
|
|
|
Addresses: addresses,
|
2016-09-06 06:19:05 -05:00
|
|
|
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{
|
2016-12-15 07:26:35 -06:00
|
|
|
Subject: evalContext.GetNotificationTitle(),
|
2016-10-03 02:38:03 -05:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"Title": evalContext.GetNotificationTitle(),
|
|
|
|
"State": evalContext.Rule.State,
|
|
|
|
"Name": evalContext.Rule.Name,
|
|
|
|
"StateModel": evalContext.GetStateModel(),
|
|
|
|
"Message": evalContext.Rule.Message,
|
|
|
|
"RuleUrl": ruleUrl,
|
2016-10-20 08:06:59 -05:00
|
|
|
"ImageLink": "",
|
|
|
|
"EmbededImage": "",
|
2016-10-03 02:38:03 -05:00
|
|
|
"AlertPageUrl": setting.AppUrl + "alerting",
|
|
|
|
"EvalMatches": evalContext.EvalMatches,
|
|
|
|
},
|
2016-10-20 08:06:59 -05:00
|
|
|
To: this.Addresses,
|
|
|
|
Template: "alert_notification.html",
|
|
|
|
EmbededFiles: []string{},
|
2016-07-27 05:09:55 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-10-20 08:06:59 -05:00
|
|
|
if evalContext.ImagePublicUrl != "" {
|
|
|
|
cmd.Data["ImageLink"] = evalContext.ImagePublicUrl
|
|
|
|
} else {
|
|
|
|
file, err := os.Stat(evalContext.ImageOnDiskPath)
|
|
|
|
if err == nil {
|
|
|
|
cmd.EmbededFiles = []string{evalContext.ImageOnDiskPath}
|
|
|
|
cmd.Data["EmbededImage"] = file.Name()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 10:03:21 -05:00
|
|
|
err = bus.DispatchCtx(evalContext.Ctx, cmd)
|
2016-10-03 02:38:03 -05:00
|
|
|
|
|
|
|
if err != nil {
|
2016-07-27 05:09:55 -05:00
|
|
|
this.log.Error("Failed to send alert notification email", "error", err)
|
2016-12-12 03:20:50 -06:00
|
|
|
return err
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
2016-10-03 02:38:03 -05:00
|
|
|
return nil
|
|
|
|
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|