notifier.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. for _, notifier := range notifiers {
  39. n.log.Info("Sending notification", "firing", context.Firing, "type", notifier.GetType())
  40. go notifier.Notify(context)
  41. }
  42. }
  43. func (n *RootNotifier) uploadImage(context *EvalContext) error {
  44. uploader, _ := imguploader.NewImageUploader()
  45. imageUrl, err := context.GetImageUrl()
  46. if err != nil {
  47. return err
  48. }
  49. renderOpts := &renderer.RenderOpts{
  50. Url: imageUrl,
  51. Width: "800",
  52. Height: "400",
  53. SessionId: "123",
  54. Timeout: "10",
  55. }
  56. if imagePath, err := renderer.RenderToPng(renderOpts); err != nil {
  57. return err
  58. } else {
  59. context.ImageOnDiskPath = imagePath
  60. }
  61. context.ImagePublicUrl, err = uploader.Upload(context.ImageOnDiskPath)
  62. if err != nil {
  63. return err
  64. }
  65. n.log.Info("uploaded", "url", context.ImagePublicUrl)
  66. return nil
  67. }
  68. func (n *RootNotifier) getNotifiers(orgId int64, notificationIds []int64) ([]Notifier, error) {
  69. if len(notificationIds) == 0 {
  70. return []Notifier{}, nil
  71. }
  72. query := &m.GetAlertNotificationsQuery{OrgId: orgId, Ids: notificationIds}
  73. if err := bus.Dispatch(query); err != nil {
  74. return nil, err
  75. }
  76. var result []Notifier
  77. for _, notification := range query.Result {
  78. if not, err := n.getNotifierFor(notification); err != nil {
  79. return nil, err
  80. } else {
  81. result = append(result, not)
  82. }
  83. }
  84. return result, nil
  85. }
  86. func (n *RootNotifier) getNotifierFor(model *m.AlertNotification) (Notifier, error) {
  87. factory, found := notifierFactories[model.Type]
  88. if !found {
  89. return nil, errors.New("Unsupported notification type")
  90. }
  91. return factory(model)
  92. }
  93. type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
  94. var notifierFactories map[string]NotifierFactory = make(map[string]NotifierFactory)
  95. func RegisterNotifier(typeName string, factory NotifierFactory) {
  96. notifierFactories[typeName] = factory
  97. }