email.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package notifiers
  2. import (
  3. "strings"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/log"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/services/alerting"
  8. )
  9. func init() {
  10. alerting.RegisterNotifier("email", NewEmailNotifier)
  11. }
  12. type EmailNotifier struct {
  13. NotifierBase
  14. Addresses []string
  15. log log.Logger
  16. }
  17. func NewEmailNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  18. addressesString := model.Settings.Get("addresses").MustString()
  19. if addressesString == "" {
  20. return nil, alerting.ValidationError{Reason: "Could not find addresses in settings"}
  21. }
  22. return &EmailNotifier{
  23. NotifierBase: NotifierBase{
  24. Name: model.Name,
  25. Type: model.Type,
  26. },
  27. Addresses: strings.Split(addressesString, "\n"),
  28. log: log.New("alerting.notifier.email"),
  29. }, nil
  30. }
  31. func (this *EmailNotifier) Notify(context *alerting.EvalContext) {
  32. this.log.Info("Sending alert notification to", "addresses", this.Addresses)
  33. ruleUrl, err := context.GetRuleUrl()
  34. if err != nil {
  35. this.log.Error("Failed get rule link", "error", err)
  36. return
  37. }
  38. cmd := &m.SendEmailCommand{
  39. Data: map[string]interface{}{
  40. "Title": context.GetNotificationTitle(),
  41. "RuleState": context.Rule.State,
  42. "RuleName": context.Rule.Name,
  43. "Severity": context.Rule.Severity,
  44. "RuleUrl": ruleUrl,
  45. },
  46. To: this.Addresses,
  47. Template: "alert_notification.html",
  48. }
  49. if err := bus.Dispatch(cmd); err != nil {
  50. this.log.Error("Failed to send alert notification email", "error", err)
  51. }
  52. }