notifier.go 3.2 KB

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