2016-07-27 05:09:55 -05:00
|
|
|
package notifiers
|
|
|
|
|
2016-09-06 06:19:05 -05:00
|
|
|
import (
|
2018-06-29 08:15:31 -05:00
|
|
|
"context"
|
2018-05-19 15:21:00 -05:00
|
|
|
"time"
|
|
|
|
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2018-06-05 03:27:29 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-09-13 08:09:55 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
2016-09-06 06:19:05 -05:00
|
|
|
)
|
|
|
|
|
2018-09-22 03:50:00 -05:00
|
|
|
const (
|
|
|
|
triggMetrString = "Triggered metrics:\n\n"
|
|
|
|
)
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// NotifierBase is the base implementation of a notifier.
|
2016-07-27 05:09:55 -05:00
|
|
|
type NotifierBase struct {
|
2018-10-17 03:41:18 -05:00
|
|
|
Name string
|
|
|
|
Type string
|
2019-05-20 08:23:06 -05:00
|
|
|
UID string
|
2020-06-02 07:42:34 -05:00
|
|
|
IsDefault bool
|
2018-10-17 03:41:18 -05:00
|
|
|
UploadImage bool
|
|
|
|
SendReminder bool
|
|
|
|
DisableResolveMessage bool
|
|
|
|
Frequency time.Duration
|
2018-06-05 05:07:02 -05:00
|
|
|
|
|
|
|
log log.Logger
|
2016-09-06 06:19:05 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// NewNotifierBase returns a new `NotifierBase`.
|
2018-06-05 03:27:29 -05:00
|
|
|
func NewNotifierBase(model *models.AlertNotification) NotifierBase {
|
2018-03-27 02:19:14 -05:00
|
|
|
uploadImage := true
|
2021-04-22 09:00:21 -05:00
|
|
|
if value, exists := model.Settings.CheckGet("uploadImage"); exists {
|
2018-03-27 02:19:14 -05:00
|
|
|
uploadImage = value.MustBool()
|
|
|
|
}
|
2017-02-13 05:43:12 -06:00
|
|
|
|
2016-10-11 03:13:19 -05:00
|
|
|
return NotifierBase{
|
2019-05-20 08:23:06 -05:00
|
|
|
UID: model.Uid,
|
2018-10-17 03:41:18 -05:00
|
|
|
Name: model.Name,
|
2020-06-02 07:42:34 -05:00
|
|
|
IsDefault: model.IsDefault,
|
2018-10-17 03:41:18 -05:00
|
|
|
Type: model.Type,
|
|
|
|
UploadImage: uploadImage,
|
|
|
|
SendReminder: model.SendReminder,
|
|
|
|
DisableResolveMessage: model.DisableResolveMessage,
|
|
|
|
Frequency: model.Frequency,
|
|
|
|
log: log.New("alerting.notifier." + model.Name),
|
2016-10-11 03:13:19 -05:00
|
|
|
}
|
2016-09-06 06:19:05 -05:00
|
|
|
}
|
|
|
|
|
2018-10-02 07:28:48 -05:00
|
|
|
// ShouldNotify checks this evaluation should send an alert notification
|
2020-06-01 10:11:25 -05:00
|
|
|
func (n *NotifierBase) ShouldNotify(ctx context.Context, context *alerting.EvalContext, notifierState *models.AlertNotificationState) bool {
|
2019-05-07 03:34:00 -05:00
|
|
|
prevState := context.PrevAlertState
|
|
|
|
newState := context.Rule.State
|
|
|
|
|
2018-03-15 04:22:51 -05:00
|
|
|
// Only notify on state change.
|
2019-05-07 03:34:00 -05:00
|
|
|
if prevState == newState && !n.SendReminder {
|
2018-05-12 20:11:58 -05:00
|
|
|
return false
|
|
|
|
}
|
2018-06-05 03:27:29 -05:00
|
|
|
|
2019-05-07 03:34:00 -05:00
|
|
|
if prevState == newState && n.SendReminder {
|
2018-09-28 11:34:20 -05:00
|
|
|
// Do not notify if interval has not elapsed
|
2020-06-01 10:11:25 -05:00
|
|
|
lastNotify := time.Unix(notifierState.UpdatedAt, 0)
|
|
|
|
if notifierState.UpdatedAt != 0 && lastNotify.Add(n.Frequency).After(time.Now()) {
|
2018-09-28 11:34:20 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not notify if alert state is OK or pending even on repeated notify
|
2019-05-07 03:34:00 -05:00
|
|
|
if newState == models.AlertStateOK || newState == models.AlertStatePending {
|
2018-09-28 11:34:20 -05:00
|
|
|
return false
|
|
|
|
}
|
2017-11-15 10:53:02 -06:00
|
|
|
}
|
2018-06-05 03:27:29 -05:00
|
|
|
|
2019-05-07 03:34:00 -05:00
|
|
|
okOrPending := newState == models.AlertStatePending || newState == models.AlertStateOK
|
|
|
|
|
2019-09-05 10:54:27 -05:00
|
|
|
// Do not notify when new state is ok/pending when previous is unknown
|
|
|
|
if prevState == models.AlertStateUnknown && okOrPending {
|
2018-11-05 03:23:43 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-07 03:34:00 -05:00
|
|
|
// Do not notify when we become Pending for the first
|
|
|
|
if prevState == models.AlertStateNoData && newState == models.AlertStatePending {
|
2018-11-14 16:19:35 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-11-05 03:23:43 -06:00
|
|
|
// Do not notify when we become OK from pending
|
2019-05-07 03:34:00 -05:00
|
|
|
if prevState == models.AlertStatePending && newState == models.AlertStateOK {
|
2018-05-20 18:01:10 -05:00
|
|
|
return false
|
|
|
|
}
|
2018-06-05 03:27:29 -05:00
|
|
|
|
2018-09-28 11:34:20 -05:00
|
|
|
// Do not notify when we OK -> Pending
|
2019-05-07 03:34:00 -05:00
|
|
|
if prevState == models.AlertStateOK && newState == models.AlertStatePending {
|
2017-11-15 10:53:02 -06:00
|
|
|
return false
|
|
|
|
}
|
2018-06-05 05:07:02 -05:00
|
|
|
|
2019-05-07 03:34:00 -05:00
|
|
|
// Do not notify if state pending and it have been updated last minute
|
2020-06-01 10:11:25 -05:00
|
|
|
if notifierState.State == models.AlertNotificationStatePending {
|
|
|
|
lastUpdated := time.Unix(notifierState.UpdatedAt, 0)
|
2018-09-30 14:52:50 -05:00
|
|
|
if lastUpdated.Add(1 * time.Minute).After(time.Now()) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 03:41:18 -05:00
|
|
|
// Do not notify when state is OK if DisableResolveMessage is set to true
|
2019-05-07 03:34:00 -05:00
|
|
|
if newState == models.AlertStateOK && n.DisableResolveMessage {
|
2018-10-15 16:16:14 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-09-13 08:09:55 -05:00
|
|
|
return true
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// GetType returns the notifier type.
|
2016-07-27 05:09:55 -05:00
|
|
|
func (n *NotifierBase) GetType() string {
|
|
|
|
return n.Type
|
|
|
|
}
|
2016-07-29 07:55:02 -05:00
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// NeedsImage returns true if an image is expected in the notification.
|
2016-07-29 07:55:02 -05:00
|
|
|
func (n *NotifierBase) NeedsImage() bool {
|
2017-02-13 05:43:12 -06:00
|
|
|
return n.UploadImage
|
2016-07-29 07:55:02 -05:00
|
|
|
}
|
2016-10-11 03:13:19 -05:00
|
|
|
|
2019-06-03 03:25:58 -05:00
|
|
|
// GetNotifierUID returns the notifier `uid`.
|
|
|
|
func (n *NotifierBase) GetNotifierUID() string {
|
2019-05-20 08:23:06 -05:00
|
|
|
return n.UID
|
2016-10-11 03:13:19 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// GetIsDefault returns true if the notifiers should
|
|
|
|
// be used for all alerts.
|
2016-10-11 03:13:19 -05:00
|
|
|
func (n *NotifierBase) GetIsDefault() bool {
|
2020-06-02 07:42:34 -05:00
|
|
|
return n.IsDefault
|
2016-10-11 03:13:19 -05:00
|
|
|
}
|
2018-05-19 15:21:00 -05:00
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// GetSendReminder returns true if reminders should be sent.
|
2018-06-05 03:27:29 -05:00
|
|
|
func (n *NotifierBase) GetSendReminder() bool {
|
|
|
|
return n.SendReminder
|
2018-05-19 15:21:00 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// GetDisableResolveMessage returns true if ok alert notifications
|
|
|
|
// should be skipped.
|
2018-10-17 03:41:18 -05:00
|
|
|
func (n *NotifierBase) GetDisableResolveMessage() bool {
|
|
|
|
return n.DisableResolveMessage
|
2018-10-15 16:16:14 -05:00
|
|
|
}
|
|
|
|
|
2020-04-29 14:37:21 -05:00
|
|
|
// GetFrequency returns the frequency for how often
|
2019-05-20 08:23:06 -05:00
|
|
|
// alerts should be evaluated.
|
2018-05-19 15:21:00 -05:00
|
|
|
func (n *NotifierBase) GetFrequency() time.Duration {
|
|
|
|
return n.Frequency
|
|
|
|
}
|