From 730c13b83768a1d7bcdfb95c0f1c39f3d8f57634 Mon Sep 17 00:00:00 2001 From: zhaoxiangchun <1422928955@qq.com> Date: Fri, 5 Feb 2021 19:56:38 +0800 Subject: [PATCH] feat(monitor): alert record add silentPeriod info MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.报警记录中增加静默期等信息 --- pkg/apis/monitor/alertrecord.go | 2 ++ pkg/monitor/alerting/notifier.go | 3 +++ pkg/monitor/alerting/rule.go | 6 ++++++ pkg/monitor/models/notification.go | 2 +- 4 files changed, 12 insertions(+), 1 deletion(-) 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