notifier.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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, err := imguploader.NewImageUploader()
  52. if err != nil {
  53. return err
  54. }
  55. renderOpts := &renderer.RenderOpts{
  56. Width: "800",
  57. Height: "400",
  58. Timeout: "30",
  59. OrgId: context.Rule.OrgId,
  60. }
  61. if slug, err := context.GetDashboardSlug(); err != nil {
  62. return err
  63. } else {
  64. renderOpts.Path = fmt.Sprintf("dashboard-solo/db/%s?&panelId=%d", slug, context.Rule.PanelId)
  65. }
  66. if imagePath, err := renderer.RenderToPng(renderOpts); err != nil {
  67. return err
  68. } else {
  69. context.ImageOnDiskPath = imagePath
  70. }
  71. context.ImagePublicUrl, err = uploader.Upload(context.ImageOnDiskPath)
  72. if err != nil {
  73. return err
  74. }
  75. n.log.Info("uploaded", "url", context.ImagePublicUrl)
  76. return nil
  77. }
  78. func (n *RootNotifier) getNotifiers(orgId int64, notificationIds []int64, context *EvalContext) ([]Notifier, error) {
  79. query := &m.GetAlertNotificationsToSendQuery{OrgId: orgId, Ids: notificationIds}
  80. if err := bus.Dispatch(query); err != nil {
  81. return nil, err
  82. }
  83. var result []Notifier
  84. for _, notification := range query.Result {
  85. if not, err := n.createNotifierFor(notification); err != nil {
  86. return nil, err
  87. } else {
  88. if shouldUseNotification(not, context) {
  89. result = append(result, not)
  90. }
  91. }
  92. }
  93. return result, nil
  94. }
  95. func (n *RootNotifier) createNotifierFor(model *m.AlertNotification) (Notifier, error) {
  96. factory, found := notifierFactories[model.Type]
  97. if !found {
  98. return nil, errors.New("Unsupported notification type")
  99. }
  100. return factory(model)
  101. }
  102. func shouldUseNotification(notifier Notifier, context *EvalContext) bool {
  103. if !context.Firing {
  104. return true
  105. }
  106. if context.Error != nil {
  107. return true
  108. }
  109. return notifier.PassesFilter(context.Rule)
  110. }
  111. type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
  112. var notifierFactories map[string]NotifierFactory = make(map[string]NotifierFactory)
  113. func RegisterNotifier(typeName string, factory NotifierFactory) {
  114. notifierFactories[typeName] = factory
  115. }