notifier.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package alerting
  2. import (
  3. "github.com/grafana/grafana/pkg/bus"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. "github.com/grafana/grafana/pkg/log"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/services/alerting/alertstates"
  8. )
  9. type NotifierImpl struct {
  10. log log.Logger
  11. }
  12. func NewNotifier() *NotifierImpl {
  13. return &NotifierImpl{
  14. log: log.New("alerting.notifier"),
  15. }
  16. }
  17. func (n *NotifierImpl) Notify(alertResult *AlertResult) {
  18. notifiers := n.getNotifiers(alertResult.AlertJob.Rule.OrgId, []int64{1, 2})
  19. for _, notifier := range notifiers {
  20. warn := alertResult.State == alertstates.Warn && notifier.SendWarning
  21. crit := alertResult.State == alertstates.Critical && notifier.SendCritical
  22. if warn || crit {
  23. n.log.Info("Sending notification", "state", alertResult.State, "type", notifier.Type)
  24. go notifier.Notifierr.Dispatch(alertResult)
  25. }
  26. }
  27. }
  28. type Notification struct {
  29. Name string
  30. Type string
  31. SendWarning bool
  32. SendCritical bool
  33. Notifierr NotificationDispatcher
  34. }
  35. type EmailNotifier struct {
  36. To string
  37. log log.Logger
  38. }
  39. func (this *EmailNotifier) Dispatch(alertResult *AlertResult) {
  40. this.log.Info("Sending email")
  41. cmd := &m.SendEmailCommand{
  42. Data: map[string]interface{}{
  43. "Description": alertResult.Description,
  44. "TriggeredAlerts": alertResult.TriggeredAlerts,
  45. },
  46. To: []string{this.To},
  47. Info: "Alert result",
  48. Massive: false,
  49. Template: "",
  50. }
  51. bus.Dispatch(cmd)
  52. }
  53. type WebhookNotifier struct {
  54. Url string
  55. User string
  56. Password string
  57. log log.Logger
  58. }
  59. func (this *WebhookNotifier) Dispatch(alertResult *AlertResult) {
  60. this.log.Info("Sending webhook")
  61. cmd := &m.SendWebhook{
  62. Url: this.Url,
  63. User: this.User,
  64. Password: this.Password,
  65. Body: alertResult.Description,
  66. }
  67. bus.Dispatch(cmd)
  68. }
  69. type NotificationDispatcher interface {
  70. Dispatch(alertResult *AlertResult)
  71. }
  72. func (n *NotifierImpl) getNotifiers(orgId int64, notificationGroups []int64) []*Notification {
  73. query := &m.GetAlertNotificationQuery{
  74. OrgID: orgId,
  75. Ids: notificationGroups,
  76. }
  77. err := bus.Dispatch(query)
  78. if err != nil {
  79. n.log.Error("Failed to read notifications", "error", err)
  80. }
  81. var result []*Notification
  82. for _, notification := range query.Result {
  83. not, err := NewNotificationFromDBModel(notification)
  84. if err == nil {
  85. result = append(result, not)
  86. }
  87. }
  88. return result
  89. }
  90. func NewNotificationFromDBModel(model *m.AlertNotification) (*Notification, error) {
  91. return &Notification{
  92. Name: model.Name,
  93. Type: model.Type,
  94. Notifierr: createNotifier(model.Type, model.Settings),
  95. SendCritical: !model.Settings.Get("ignoreCrit").MustBool(),
  96. SendWarning: !model.Settings.Get("ignoreWarn").MustBool(),
  97. }, nil
  98. }
  99. var createNotifier = func(notificationType string, settings *simplejson.Json) NotificationDispatcher {
  100. if notificationType == "email" {
  101. return &EmailNotifier{
  102. To: settings.Get("to").MustString(),
  103. log: log.New("alerting.notification.email"),
  104. }
  105. }
  106. return &WebhookNotifier{
  107. Url: settings.Get("url").MustString(),
  108. User: settings.Get("user").MustString(),
  109. Password: settings.Get("password").MustString(),
  110. log: log.New("alerting.notification.webhook"),
  111. }
  112. }