email.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. ruleLink, err := getRuleLink(context.Rule)
  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. "RuleState": context.Rule.State,
  41. "RuleName": context.Rule.Name,
  42. "Severity": context.Rule.Severity,
  43. "RuleLink": ruleLink,
  44. },
  45. To: this.Addresses,
  46. Template: "alert_notification.html",
  47. }
  48. if err := bus.Dispatch(cmd); err != nil {
  49. this.log.Error("Failed to send alert notification email", "error", err)
  50. }
  51. }