notifier.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package alerting
  2. import (
  3. "errors"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/log"
  6. m "github.com/grafana/grafana/pkg/models"
  7. )
  8. type RootNotifier struct {
  9. log log.Logger
  10. }
  11. func NewRootNotifier() *RootNotifier {
  12. return &RootNotifier{
  13. log: log.New("alerting.notifier"),
  14. }
  15. }
  16. func (n *RootNotifier) GetType() string {
  17. return "root"
  18. }
  19. func (n *RootNotifier) Notify(context *AlertResultContext) {
  20. n.log.Info("Sending notifications for", "ruleId", context.Rule.Id)
  21. notifiers, err := n.getNotifiers(context.Rule.OrgId, context.Rule.Notifications)
  22. if err != nil {
  23. n.log.Error("Failed to read notifications", "error", err)
  24. return
  25. }
  26. for _, notifier := range notifiers {
  27. n.log.Info("Sending notification", "firing", context.Firing, "type", notifier.GetType())
  28. go notifier.Notify(context)
  29. }
  30. }
  31. func (n *RootNotifier) getNotifiers(orgId int64, notificationIds []int64) ([]Notifier, error) {
  32. query := &m.GetAlertNotificationsQuery{OrgId: orgId, Ids: notificationIds}
  33. if err := bus.Dispatch(query); err != nil {
  34. return nil, err
  35. }
  36. var result []Notifier
  37. for _, notification := range query.Result {
  38. if not, err := n.getNotifierFor(notification); err != nil {
  39. return nil, err
  40. } else {
  41. result = append(result, not)
  42. }
  43. }
  44. return result, nil
  45. }
  46. func (n *RootNotifier) getNotifierFor(model *m.AlertNotification) (Notifier, error) {
  47. factory, found := notifierFactories[model.Type]
  48. if !found {
  49. return nil, errors.New("Unsupported notification type")
  50. }
  51. return factory(model)
  52. // if model.Type == "email" {
  53. // addressesString := model.Settings.Get("addresses").MustString()
  54. //
  55. // if addressesString == "" {
  56. // return nil, fmt.Errorf("Could not find addresses in settings")
  57. // }
  58. //
  59. // NotifierBase: NotifierBase{
  60. // Name: model.Name,
  61. // Type: model.Type,
  62. // },
  63. // Addresses: strings.Split(addressesString, "\n"),
  64. // log: log.New("alerting.notification.email"),
  65. // }, nil
  66. // }
  67. // url := settings.Get("url").MustString()
  68. // if url == "" {
  69. // return nil, fmt.Errorf("Could not find url propertie in settings")
  70. // }
  71. //
  72. // return &WebhookNotifier{
  73. // Url: url,
  74. // User: settings.Get("user").MustString(),
  75. // Password: settings.Get("password").MustString(),
  76. // log: log.New("alerting.notification.webhook"),
  77. // }, nil
  78. }
  79. // type WebhookNotifier struct {
  80. // Url string
  81. // User string
  82. // Password string
  83. // log log.Logger
  84. // }
  85. //
  86. // func (this *WebhookNotifier) Dispatch(context *AlertResultContext) {
  87. // this.log.Info("Sending webhook")
  88. //
  89. // bodyJSON := simplejson.New()
  90. // bodyJSON.Set("name", context.AlertJob.Rule.Name)
  91. // bodyJSON.Set("state", context.State)
  92. // bodyJSON.Set("trigged", context.TriggeredAlerts)
  93. //
  94. // body, _ := bodyJSON.MarshalJSON()
  95. //
  96. // cmd := &m.SendWebhook{
  97. // Url: this.Url,
  98. // User: this.User,
  99. // Password: this.Password,
  100. // Body: string(body),
  101. // }
  102. //
  103. // bus.Dispatch(cmd)
  104. // }
  105. type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
  106. var notifierFactories map[string]NotifierFactory = make(map[string]NotifierFactory)
  107. func RegisterNotifier(typeName string, factory NotifierFactory) {
  108. notifierFactories[typeName] = factory
  109. }