feat: 触发报警后的通知支持仅发送一次

This commit is contained in:
Akkia 2022-09-13 04:01:08 +08:00
parent 222b87ec78
commit 32d15e116e
No known key found for this signature in database
GPG Key ID: DABE9A4AB2DD7EF3
3 changed files with 19 additions and 5 deletions

View File

@ -600,6 +600,7 @@ type alertRuleForm struct {
Name string
RulesRaw string
NotificationTag string
TriggerMode int
Enable string
}
@ -642,6 +643,7 @@ func (ma *memberAPI) addOrEditAlertRule(c *gin.Context) {
r.RulesRaw = arf.RulesRaw
r.NotificationTag = arf.NotificationTag
enable := arf.Enable == "on"
r.TriggerMode = arf.TriggerMode
r.Enable = &enable
r.ID = arf.ID
//保证NotificationTag不为空

View File

@ -7,6 +7,11 @@ import (
"gorm.io/gorm"
)
const (
ModeAlwaysTrigger = 0
ModeOnetimeTrigger = 1
)
type CycleTransferStats struct {
Name string
From time.Time
@ -23,6 +28,7 @@ type AlertRule struct {
Name string
RulesRaw string
Enable *bool
TriggerMode int `gorm:"default:0"` // 触发模式: 0-始终触发(默认) 1-单次触发
NotificationTag string // 该报警规则所在的通知组
Rules []Rule `gorm:"-" json:"-"`
}

View File

@ -153,13 +153,19 @@ func checkStatus() {
// 保存当前服务器状态信息
curServer := model.Server{}
copier.Copy(&curServer, server)
// 本次未通过检查
if !passed {
// 始终触发模式或上次检查不为失败时触发报警(跳过单次触发+上次失败的情况)
if alert.TriggerMode == model.ModeAlwaysTrigger || alertsPrevState[alert.ID][server.ID] != _RuleCheckFail {
alertsPrevState[alert.ID][server.ID] = _RuleCheckFail
message := fmt.Sprintf("[%s] %s(%s) %s", Localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "Incident",
}), server.Name, IPDesensitize(server.Host.IP), alert.Name)
go SendNotification(alert.NotificationTag, message, true, &curServer)
}
} else {
// 本次通过检查但上一次的状态为失败,则发送恢复通知
if alertsPrevState[alert.ID][server.ID] == _RuleCheckFail {
message := fmt.Sprintf("[%s] %s(%s) %s", Localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "Resolved",