notifier.go 3.3 KB

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