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