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
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2019-05-14 01:15:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2019-08-26 10:19:03 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2019-05-14 01:15:05 -05:00
|
|
|
|
2016-07-27 05:09:55 -05:00
|
|
|
"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",
|
2017-03-06 14:08:45 -06:00
|
|
|
Description: "Sends notifications using Grafana server configured SMTP settings",
|
2017-01-06 05:04:25 -06:00
|
|
|
Factory: NewEmailNotifier,
|
|
|
|
OptionsTemplate: `
|
2020-01-10 09:06:33 -06:00
|
|
|
<h3 class="page-heading">Email settings</h3>
|
|
|
|
<div class="gf-form">
|
|
|
|
<gf-form-switch
|
|
|
|
class="gf-form"
|
|
|
|
label="Single email"
|
|
|
|
label-class="width-8"
|
|
|
|
checked="ctrl.model.settings.singleEmail"
|
|
|
|
tooltip="Send a single email to all recipients">
|
|
|
|
</gf-form-switch>
|
|
|
|
</div>
|
|
|
|
<div class="gf-form">
|
|
|
|
<label class="gf-form-label width-8">
|
|
|
|
Addresses
|
|
|
|
</label>
|
|
|
|
<textarea rows="7" class="gf-form-input width-27" required ng-model="ctrl.model.settings.addresses"></textarea>
|
|
|
|
</div>
|
|
|
|
<div class="gf-form offset-width-8">
|
|
|
|
<span>You can enter multiple email addresses using a ";" separator</span>
|
|
|
|
</div>
|
2017-01-06 05:04:25 -06:00
|
|
|
`,
|
|
|
|
})
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// EmailNotifier is responsible for sending
|
|
|
|
// alert notifications over email.
|
2016-07-27 05:09:55 -05:00
|
|
|
type EmailNotifier struct {
|
|
|
|
NotifierBase
|
2020-01-10 09:06:33 -06:00
|
|
|
Addresses []string
|
|
|
|
SingleEmail bool
|
|
|
|
log log.Logger
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// NewEmailNotifier is the constructor function
|
|
|
|
// for the EmailNotifier.
|
2019-05-14 01:15:05 -05:00
|
|
|
func NewEmailNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
|
2016-07-27 05:09:55 -05:00
|
|
|
addressesString := model.Settings.Get("addresses").MustString()
|
2020-01-10 09:06:33 -06:00
|
|
|
singleEmail := model.Settings.Get("singleEmail").MustBool(false)
|
2016-07-27 05:09:55 -05:00
|
|
|
|
|
|
|
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
|
2019-08-26 10:19:03 -05:00
|
|
|
addresses := util.SplitEmails(addressesString)
|
2016-10-22 03:24:31 -05:00
|
|
|
|
2016-07-27 05:09:55 -05:00
|
|
|
return &EmailNotifier{
|
2018-06-05 03:27:29 -05:00
|
|
|
NotifierBase: NewNotifierBase(model),
|
2016-10-22 03:24:31 -05:00
|
|
|
Addresses: addresses,
|
2020-01-10 09:06:33 -06:00
|
|
|
SingleEmail: singleEmail,
|
2016-09-06 06:19:05 -05:00
|
|
|
log: log.New("alerting.notifier.email"),
|
2016-07-27 05:09:55 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// Notify sends the alert notification.
|
|
|
|
func (en *EmailNotifier) Notify(evalContext *alerting.EvalContext) error {
|
2020-01-10 09:06:33 -06:00
|
|
|
en.log.Info("Sending alert notification to", "addresses", en.Addresses, "singleEmail", en.SingleEmail)
|
2016-07-27 05:09:55 -05:00
|
|
|
|
2019-06-03 03:25:58 -05:00
|
|
|
ruleURL, err := evalContext.GetRuleURL()
|
2016-07-27 05:09:55 -05:00
|
|
|
if err != nil {
|
2019-05-20 08:23:06 -05:00
|
|
|
en.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
|
|
|
}
|
|
|
|
|
2017-05-17 02:51:51 -05:00
|
|
|
error := ""
|
|
|
|
if evalContext.Error != nil {
|
|
|
|
error = evalContext.Error.Error()
|
|
|
|
}
|
|
|
|
|
2019-05-14 01:15:05 -05:00
|
|
|
cmd := &models.SendEmailCommandSync{
|
|
|
|
SendEmailCommand: models.SendEmailCommand{
|
2016-12-15 07:26:35 -06:00
|
|
|
Subject: evalContext.GetNotificationTitle(),
|
2016-10-03 02:38:03 -05:00
|
|
|
Data: map[string]interface{}{
|
2019-03-27 03:42:20 -05:00
|
|
|
"Title": evalContext.GetNotificationTitle(),
|
|
|
|
"State": evalContext.Rule.State,
|
|
|
|
"Name": evalContext.Rule.Name,
|
|
|
|
"StateModel": evalContext.GetStateModel(),
|
|
|
|
"Message": evalContext.Rule.Message,
|
|
|
|
"Error": error,
|
2019-05-20 08:23:06 -05:00
|
|
|
"RuleUrl": ruleURL,
|
2019-03-27 03:42:20 -05:00
|
|
|
"ImageLink": "",
|
|
|
|
"EmbeddedImage": "",
|
|
|
|
"AlertPageUrl": setting.AppUrl + "alerting",
|
|
|
|
"EvalMatches": evalContext.EvalMatches,
|
2016-10-03 02:38:03 -05:00
|
|
|
},
|
2020-06-01 10:11:25 -05:00
|
|
|
To: en.Addresses,
|
|
|
|
SingleEmail: en.SingleEmail,
|
|
|
|
Template: "alert_notification.html",
|
|
|
|
EmbeddedFiles: []string{},
|
2016-07-27 05:09:55 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-03-30 17:46:01 -05:00
|
|
|
if en.NeedsImage() {
|
|
|
|
if evalContext.ImagePublicURL != "" {
|
|
|
|
cmd.Data["ImageLink"] = evalContext.ImagePublicURL
|
|
|
|
} else {
|
|
|
|
file, err := os.Stat(evalContext.ImageOnDiskPath)
|
|
|
|
if err == nil {
|
2020-06-01 10:11:25 -05:00
|
|
|
cmd.EmbeddedFiles = []string{evalContext.ImageOnDiskPath}
|
2020-03-30 17:46:01 -05:00
|
|
|
cmd.Data["EmbeddedImage"] = file.Name()
|
|
|
|
}
|
2016-10-20 08:06:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-05-20 08:23:06 -05:00
|
|
|
en.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
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
return nil
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|