notifier.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package alerting
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/components/imguploader"
  7. "github.com/grafana/grafana/pkg/components/renderer"
  8. "github.com/grafana/grafana/pkg/log"
  9. m "github.com/grafana/grafana/pkg/models"
  10. )
  11. type RootNotifier struct {
  12. log log.Logger
  13. }
  14. func NewRootNotifier() *RootNotifier {
  15. return &RootNotifier{
  16. log: log.New("alerting.notifier"),
  17. }
  18. }
  19. func (n *RootNotifier) GetType() string {
  20. return "root"
  21. }
  22. func (n *RootNotifier) NeedsImage() bool {
  23. return false
  24. }
  25. func (n *RootNotifier) PassesFilter(rule *Rule) bool {
  26. return false
  27. }
  28. func (n *RootNotifier) Notify(context *EvalContext) {
  29. n.log.Info("Sending notifications for", "ruleId", context.Rule.Id)
  30. notifiers, err := n.getNotifiers(context.Rule.OrgId, context.Rule.Notifications, context)
  31. if err != nil {
  32. n.log.Error("Failed to read notifications", "error", err)
  33. return
  34. }
  35. if len(notifiers) == 0 {
  36. return
  37. }
  38. err = n.uploadImage(context)
  39. if err != nil {
  40. n.log.Error("Failed to upload alert panel image", "error", err)
  41. }
  42. n.sendNotifications(notifiers, context)
  43. }
  44. func (n *RootNotifier) sendNotifications(notifiers []Notifier, context *EvalContext) {
  45. for _, notifier := range notifiers {
  46. n.log.Info("Sending notification", "firing", context.Firing, "type", notifier.GetType())
  47. go notifier.Notify(context)
  48. }
  49. }
  50. func (n *RootNotifier) uploadImage(context *EvalContext) (err error) {
  51. uploader, _ := imguploader.NewImageUploader()
  52. renderOpts := &renderer.RenderOpts{
  53. Width: "800",
  54. Height: "400",
  55. Timeout: "30",
  56. OrgId: context.Rule.OrgId,
  57. }
  58. if slug, err := context.GetDashboardSlug(); err != nil {
  59. return err
  60. } else {
  61. renderOpts.Path = fmt.Sprintf("dashboard-solo/db/%s?&panelId=%d", slug, context.Rule.PanelId)
  62. }
  63. if imagePath, err := renderer.RenderToPng(renderOpts); err != nil {
  64. return err
  65. } else {
  66. context.ImageOnDiskPath = imagePath
  67. }
  68. context.ImagePublicUrl, err = uploader.Upload(context.ImageOnDiskPath)
  69. if err != nil {
  70. return err
  71. }
  72. n.log.Info("uploaded", "url", context.ImagePublicUrl)
  73. return nil
  74. }
  75. func (n *RootNotifier) getNotifiers(orgId int64, notificationIds []int64, context *EvalContext) ([]Notifier, error) {
  76. query := &m.GetAlertNotificationsToSendQuery{OrgId: orgId, Ids: notificationIds}
  77. if err := bus.Dispatch(query); err != nil {
  78. return nil, err
  79. }
  80. var result []Notifier
  81. for _, notification := range query.Result {
  82. if not, err := n.createNotifierFor(notification); err != nil {
  83. return nil, err
  84. } else {
  85. if shouldUseNotification(not, context) {
  86. result = append(result, not)
  87. }
  88. }
  89. }
  90. return result, nil
  91. }
  92. func (n *RootNotifier) createNotifierFor(model *m.AlertNotification) (Notifier, error) {
  93. factory, found := notifierFactories[model.Type]
  94. if !found {
  95. return nil, errors.New("Unsupported notification type")
  96. }
  97. return factory(model)
  98. }
  99. func shouldUseNotification(notifier Notifier, context *EvalContext) bool {
  100. if !context.Firing {
  101. return true
  102. }
  103. if context.Error != nil {
  104. return true
  105. }
  106. return notifier.PassesFilter(context.Rule)
  107. }
  108. type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
  109. var notifierFactories map[string]NotifierFactory = make(map[string]NotifierFactory)
  110. func RegisterNotifier(typeName string, factory NotifierFactory) {
  111. notifierFactories[typeName] = factory
  112. }