feat: 触发报警后的通知支持仅发送一次
This commit is contained in:
		
							parent
							
								
									222b87ec78
								
							
						
					
					
						commit
						32d15e116e
					
				@ -600,6 +600,7 @@ type alertRuleForm struct {
 | 
				
			|||||||
	Name            string
 | 
						Name            string
 | 
				
			||||||
	RulesRaw        string
 | 
						RulesRaw        string
 | 
				
			||||||
	NotificationTag string
 | 
						NotificationTag string
 | 
				
			||||||
 | 
						TriggerMode     int
 | 
				
			||||||
	Enable          string
 | 
						Enable          string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -642,6 +643,7 @@ func (ma *memberAPI) addOrEditAlertRule(c *gin.Context) {
 | 
				
			|||||||
		r.RulesRaw = arf.RulesRaw
 | 
							r.RulesRaw = arf.RulesRaw
 | 
				
			||||||
		r.NotificationTag = arf.NotificationTag
 | 
							r.NotificationTag = arf.NotificationTag
 | 
				
			||||||
		enable := arf.Enable == "on"
 | 
							enable := arf.Enable == "on"
 | 
				
			||||||
 | 
							r.TriggerMode = arf.TriggerMode
 | 
				
			||||||
		r.Enable = &enable
 | 
							r.Enable = &enable
 | 
				
			||||||
		r.ID = arf.ID
 | 
							r.ID = arf.ID
 | 
				
			||||||
		//保证NotificationTag不为空
 | 
							//保证NotificationTag不为空
 | 
				
			||||||
 | 
				
			|||||||
@ -7,6 +7,11 @@ import (
 | 
				
			|||||||
	"gorm.io/gorm"
 | 
						"gorm.io/gorm"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						ModeAlwaysTrigger  = 0
 | 
				
			||||||
 | 
						ModeOnetimeTrigger = 1
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type CycleTransferStats struct {
 | 
					type CycleTransferStats struct {
 | 
				
			||||||
	Name       string
 | 
						Name       string
 | 
				
			||||||
	From       time.Time
 | 
						From       time.Time
 | 
				
			||||||
@ -23,6 +28,7 @@ type AlertRule struct {
 | 
				
			|||||||
	Name            string
 | 
						Name            string
 | 
				
			||||||
	RulesRaw        string
 | 
						RulesRaw        string
 | 
				
			||||||
	Enable          *bool
 | 
						Enable          *bool
 | 
				
			||||||
 | 
						TriggerMode     int    `gorm:"default:0"` // 触发模式: 0-始终触发(默认) 1-单次触发
 | 
				
			||||||
	NotificationTag string // 该报警规则所在的通知组
 | 
						NotificationTag string // 该报警规则所在的通知组
 | 
				
			||||||
	Rules           []Rule `gorm:"-" json:"-"`
 | 
						Rules           []Rule `gorm:"-" json:"-"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -153,13 +153,19 @@ func checkStatus() {
 | 
				
			|||||||
			// 保存当前服务器状态信息
 | 
								// 保存当前服务器状态信息
 | 
				
			||||||
			curServer := model.Server{}
 | 
								curServer := model.Server{}
 | 
				
			||||||
			copier.Copy(&curServer, server)
 | 
								copier.Copy(&curServer, server)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 本次未通过检查
 | 
				
			||||||
			if !passed {
 | 
								if !passed {
 | 
				
			||||||
 | 
									// 始终触发模式或上次检查不为失败时触发报警(跳过单次触发+上次失败的情况)
 | 
				
			||||||
 | 
									if alert.TriggerMode == model.ModeAlwaysTrigger || alertsPrevState[alert.ID][server.ID] != _RuleCheckFail {
 | 
				
			||||||
					alertsPrevState[alert.ID][server.ID] = _RuleCheckFail
 | 
										alertsPrevState[alert.ID][server.ID] = _RuleCheckFail
 | 
				
			||||||
					message := fmt.Sprintf("[%s] %s(%s) %s", Localizer.MustLocalize(&i18n.LocalizeConfig{
 | 
										message := fmt.Sprintf("[%s] %s(%s) %s", Localizer.MustLocalize(&i18n.LocalizeConfig{
 | 
				
			||||||
						MessageID: "Incident",
 | 
											MessageID: "Incident",
 | 
				
			||||||
					}), server.Name, IPDesensitize(server.Host.IP), alert.Name)
 | 
										}), server.Name, IPDesensitize(server.Host.IP), alert.Name)
 | 
				
			||||||
					go SendNotification(alert.NotificationTag, message, true, &curServer)
 | 
										go SendNotification(alert.NotificationTag, message, true, &curServer)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
 | 
									// 本次通过检查但上一次的状态为失败,则发送恢复通知
 | 
				
			||||||
				if alertsPrevState[alert.ID][server.ID] == _RuleCheckFail {
 | 
									if alertsPrevState[alert.ID][server.ID] == _RuleCheckFail {
 | 
				
			||||||
					message := fmt.Sprintf("[%s] %s(%s) %s", Localizer.MustLocalize(&i18n.LocalizeConfig{
 | 
										message := fmt.Sprintf("[%s] %s(%s) %s", Localizer.MustLocalize(&i18n.LocalizeConfig{
 | 
				
			||||||
						MessageID: "Resolved",
 | 
											MessageID: "Resolved",
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user