notifier.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. "github.com/grafana/grafana/pkg/setting"
  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) Notify(context *EvalContext) {
  26. n.log.Info("Sending notifications for", "ruleId", context.Rule.Id)
  27. notifiers, err := n.getNotifiers(context.Rule.OrgId, context.Rule.Notifications)
  28. if err != nil {
  29. n.log.Error("Failed to read notifications", "error", err)
  30. return
  31. }
  32. if len(notifiers) == 0 {
  33. return
  34. }
  35. err = n.uploadImage(context)
  36. if err != nil {
  37. n.log.Error("Failed to upload alert panel image", "error", err)
  38. }
  39. for _, notifier := range notifiers {
  40. n.log.Info("Sending notification", "firing", context.Firing, "type", notifier.GetType())
  41. go notifier.Notify(context)
  42. }
  43. }
  44. func (n *RootNotifier) uploadImage(context *EvalContext) error {
  45. uploader := imguploader.NewS3Uploader(
  46. setting.S3TempImageStoreBucketUrl,
  47. setting.S3TempImageStoreAccessKey,
  48. setting.S3TempImageStoreSecretKey)
  49. imageUrl, err := context.GetImageUrl()
  50. if err != nil {
  51. return err
  52. }
  53. renderOpts := &renderer.RenderOpts{
  54. Url: imageUrl,
  55. Width: "800",
  56. Height: "400",
  57. SessionId: "123",
  58. Timeout: "10",
  59. }
  60. if imagePath, err := renderer.RenderToPng(renderOpts); err != nil {
  61. return err
  62. } else {
  63. context.ImageOnDiskPath = imagePath
  64. }
  65. context.ImagePublicUrl, err = uploader.Upload(context.ImageOnDiskPath)
  66. if err != nil {
  67. return err
  68. }
  69. n.log.Info("uploaded", "url", context.ImagePublicUrl)
  70. return nil
  71. }
  72. func (n *RootNotifier) getNotifiers(orgId int64, notificationIds []int64) ([]Notifier, error) {
  73. if len(notificationIds) == 0 {
  74. return []Notifier{}, nil
  75. }
  76. query := &m.GetAlertNotificationsQuery{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.getNotifierFor(notification); err != nil {
  83. return nil, err
  84. } else {
  85. result = append(result, not)
  86. }
  87. }
  88. return result, nil
  89. }
  90. func (n *RootNotifier) getNotifierFor(model *m.AlertNotification) (Notifier, error) {
  91. factory, found := notifierFactories[model.Type]
  92. if !found {
  93. return nil, errors.New("Unsupported notification type")
  94. }
  95. return factory(model)
  96. }
  97. type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
  98. var notifierFactories map[string]NotifierFactory = make(map[string]NotifierFactory)
  99. func RegisterNotifier(typeName string, factory NotifierFactory) {
  100. notifierFactories[typeName] = factory
  101. }