base.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package notifiers
  2. import (
  3. "context"
  4. "time"
  5. "github.com/grafana/grafana/pkg/log"
  6. "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/services/alerting"
  8. )
  9. const (
  10. triggMetrString = "Triggered metrics:\n\n"
  11. )
  12. type NotifierBase struct {
  13. Name string
  14. Type string
  15. Id int64
  16. IsDeault bool
  17. UploadImage bool
  18. SendReminder bool
  19. DisableResolveMessage bool
  20. Frequency time.Duration
  21. log log.Logger
  22. }
  23. func NewNotifierBase(model *models.AlertNotification) NotifierBase {
  24. uploadImage := true
  25. value, exist := model.Settings.CheckGet("uploadImage")
  26. if exist {
  27. uploadImage = value.MustBool()
  28. }
  29. return NotifierBase{
  30. Id: model.Id,
  31. Name: model.Name,
  32. IsDeault: model.IsDefault,
  33. Type: model.Type,
  34. UploadImage: uploadImage,
  35. SendReminder: model.SendReminder,
  36. DisableResolveMessage: model.DisableResolveMessage,
  37. Frequency: model.Frequency,
  38. log: log.New("alerting.notifier." + model.Name),
  39. }
  40. }
  41. // ShouldNotify checks this evaluation should send an alert notification
  42. func (n *NotifierBase) ShouldNotify(ctx context.Context, context *alerting.EvalContext, notiferState *models.AlertNotificationState) bool {
  43. // Only notify on state change.
  44. if context.PrevAlertState == context.Rule.State && !n.SendReminder {
  45. return false
  46. }
  47. if context.PrevAlertState == context.Rule.State && n.SendReminder {
  48. // Do not notify if interval has not elapsed
  49. lastNotify := time.Unix(notiferState.UpdatedAt, 0)
  50. if notiferState.UpdatedAt != 0 && lastNotify.Add(n.Frequency).After(time.Now()) {
  51. return false
  52. }
  53. // Do not notify if alert state is OK or pending even on repeated notify
  54. if context.Rule.State == models.AlertStateOK || context.Rule.State == models.AlertStatePending {
  55. return false
  56. }
  57. }
  58. // Do not notify when we become OK for the first time.
  59. if context.PrevAlertState == models.AlertStateUnknown && context.Rule.State == models.AlertStateOK {
  60. return false
  61. }
  62. // Do not notify when we become OK for the first time.
  63. if context.PrevAlertState == models.AlertStateUnknown && context.Rule.State == models.AlertStatePending {
  64. return false
  65. }
  66. // Do not notify when we become OK from pending
  67. if context.PrevAlertState == models.AlertStatePending && context.Rule.State == models.AlertStateOK {
  68. return false
  69. }
  70. // Do not notify when we OK -> Pending
  71. if context.PrevAlertState == models.AlertStateOK && context.Rule.State == models.AlertStatePending {
  72. return false
  73. }
  74. // Do not notifu if state pending and it have been updated last minute
  75. if notiferState.State == models.AlertNotificationStatePending {
  76. lastUpdated := time.Unix(notiferState.UpdatedAt, 0)
  77. if lastUpdated.Add(1 * time.Minute).After(time.Now()) {
  78. return false
  79. }
  80. }
  81. // Do not notify when state is OK if DisableResolveMessage is set to true
  82. if context.Rule.State == models.AlertStateOK && n.DisableResolveMessage {
  83. return false
  84. }
  85. return true
  86. }
  87. func (n *NotifierBase) GetType() string {
  88. return n.Type
  89. }
  90. func (n *NotifierBase) NeedsImage() bool {
  91. return n.UploadImage
  92. }
  93. func (n *NotifierBase) GetNotifierId() int64 {
  94. return n.Id
  95. }
  96. func (n *NotifierBase) GetIsDefault() bool {
  97. return n.IsDeault
  98. }
  99. func (n *NotifierBase) GetSendReminder() bool {
  100. return n.SendReminder
  101. }
  102. func (n *NotifierBase) GetDisableResolveMessage() bool {
  103. return n.DisableResolveMessage
  104. }
  105. func (n *NotifierBase) GetFrequency() time.Duration {
  106. return n.Frequency
  107. }