diff --git a/pkg/apis/monitor/alertrecord.go b/pkg/apis/monitor/alertrecord.go index e8b08319b2..33826c0a75 100644 --- a/pkg/apis/monitor/alertrecord.go +++ b/pkg/apis/monitor/alertrecord.go @@ -57,4 +57,6 @@ type AlertRecordRule struct { Period string `json:"period"` AlertDuration int64 `json:"alert_duration"` ConditionType string `json:"condition_type"` + // 静默期 + SilentPeriod string `json:"silent_period"` } diff --git a/pkg/monitor/alerting/notifier.go b/pkg/monitor/alerting/notifier.go index 13689f284f..7f32e57b1a 100644 --- a/pkg/monitor/alerting/notifier.go +++ b/pkg/monitor/alerting/notifier.go @@ -251,5 +251,8 @@ func newAlertRecordRule(evalCtx *EvalContext) monitor.AlertRecordRule { alertRule.AlertDuration = int64(evalCtx.Rule.For)/evalCtx.Rule.Frequency + 1 + if evalCtx.Rule.SilentPeriod != 0 { + alertRule.SilentPeriod = fmt.Sprintf("%dm", evalCtx.Rule.SilentPeriod/60) + } return alertRule } diff --git a/pkg/monitor/alerting/rule.go b/pkg/monitor/alerting/rule.go index 54b92ef57b..cf017ffa5c 100644 --- a/pkg/monitor/alerting/rule.go +++ b/pkg/monitor/alerting/rule.go @@ -64,6 +64,8 @@ type Rule struct { StateChanges int CustomizeConfig jsonutils.JSONObject + // 静默期 + SilentPeriod int64 } var ( @@ -140,6 +142,10 @@ func NewRuleFromDBAlert(ruleDef *models.SAlert) (*Rule, error) { return nil, err } for _, n := range notis { + noti, _ := n.GetNotification() + if noti.Frequency != 0 { + model.SilentPeriod = noti.Frequency + } nIds = append(nIds, n.NotificationId) } model.Notifications = nIds diff --git a/pkg/monitor/models/notification.go b/pkg/monitor/models/notification.go index 7966a4e862..db9cc813b5 100644 --- a/pkg/monitor/models/notification.go +++ b/pkg/monitor/models/notification.go @@ -229,7 +229,7 @@ func (n *SNotification) ShouldSendNotification() bool { if n.Frequency == 0 { return true } - if int64(time.Now().Sub(n.LastSendNotification)/time.Second)+int64(time.Second*60) >= n.Frequency { + if int64(time.Now().Sub(n.LastSendNotification)/time.Second)+int64(60) >= n.Frequency { return true } return false