notifier.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.Notify(alertResult)
  27. }
  28. }
  29. }
  30. type Notification struct {
  31. Name string
  32. Type string
  33. SendWarning bool
  34. SendCritical bool
  35. Notifierr Notifierr
  36. }
  37. type EmailNotifier struct {
  38. To string
  39. From string
  40. log log.Logger
  41. }
  42. func (this *EmailNotifier) Notify(alertResult *AlertResult) {
  43. //bus.dispath to notification package in grafana
  44. this.log.Info("Sending email")
  45. }
  46. type WebhookNotifier struct {
  47. Url string
  48. AuthUser string
  49. AuthPassword string
  50. log log.Logger
  51. }
  52. func (this *WebhookNotifier) Notify(alertResult *AlertResult) {
  53. //bus.dispath to notification package in grafana
  54. this.log.Info("Sending webhook")
  55. }
  56. type Notifierr interface {
  57. Notify(alertResult *AlertResult)
  58. }
  59. func (n *NotifierImpl) getNotifiers(orgId int64, notificationGroups []int64) []*Notification {
  60. query := &m.GetAlertNotificationQuery{
  61. OrgID: orgId,
  62. Ids: notificationGroups,
  63. }
  64. err := bus.Dispatch(query)
  65. if err != nil {
  66. n.log.Error("Failed to read notifications", "error", err)
  67. }
  68. var result []*Notification
  69. n.log.Warn("query result", "length", len(query.Result))
  70. for _, notification := range query.Result {
  71. not, err := NewNotificationFromDBModel(notification)
  72. if err == nil {
  73. result = append(result, not)
  74. }
  75. }
  76. return result
  77. }
  78. func NewNotificationFromDBModel(model *m.AlertNotification) (*Notification, error) {
  79. return &Notification{
  80. Name: model.Name,
  81. Type: model.Type,
  82. Notifierr: createNotifier(model.Type, model.Settings),
  83. SendCritical: !model.Settings.Get("ignoreCrit").MustBool(),
  84. SendWarning: !model.Settings.Get("ignoreWarn").MustBool(),
  85. }, nil
  86. }
  87. var createNotifier = func(notificationType string, settings *simplejson.Json) Notifierr {
  88. if notificationType == "email" {
  89. return &EmailNotifier{
  90. To: settings.Get("to").MustString(),
  91. From: settings.Get("from").MustString(),
  92. log: log.New("alerting.notification.email"),
  93. }
  94. }
  95. return &WebhookNotifier{
  96. Url: settings.Get("url").MustString(),
  97. AuthUser: settings.Get("user").MustString(),
  98. AuthPassword: settings.Get("password").MustString(),
  99. log: log.New("alerting.notification.webhook"),
  100. }
  101. }