增加告警间隔配置

This commit is contained in:
2025-11-08 00:13:15 +08:00
parent f46c8aed0c
commit 3a59a2755c
4 changed files with 59 additions and 17 deletions

View File

@@ -113,3 +113,14 @@ notify:
# 定时采集配置 # 定时采集配置
collection: collection:
interval: 1 # 采集间隔 (分钟) interval: 1 # 采集间隔 (分钟)
# 告警通知配置
alarm_notification:
notification_intervals: # 告警通知间隔(分钟)
debug: 1
info: 1
warn: 1
error: 1
dpanic: 1
panic: 1
fatal: 1

View File

@@ -90,4 +90,15 @@ lora_mesh:
# 定时采集配置 # 定时采集配置
collection: collection:
interval: 1 # 采集间隔 (分钟) interval: 1 # 采集间隔 (分钟)
# 告警通知配置
alarm_notification:
notification_intervals: # 告警通知间隔 (分钟)
debug: 1
info: 1
warn: 1
error: 1
dpanic: 1
panic: 1
fatal: 1

View File

@@ -14,15 +14,17 @@ import (
// AlarmNotificationTaskParams 定义了 AlarmNotificationTask 的参数结构 // AlarmNotificationTaskParams 定义了 AlarmNotificationTask 的参数结构
// 如果用户没有指定某个等级的配置, 则默认为该等级消息只发送一次 // 如果用户没有指定某个等级的配置, 则默认为该等级消息只发送一次
type AlarmNotificationTaskParams struct { type AlarmNotificationTaskParams struct {
// NotificationIntervals 告警通知的发送间隔时间,键为告警等级,值为时间间隔 // NotificationIntervals 告警通知的发送间隔时间,键为告警等级,值为时间间隔(分钟)
NotificationIntervals map[models.SeverityLevel]time.Duration `json:"notification_intervals"` NotificationIntervals map[models.SeverityLevel]uint `json:"notification_intervals"`
} }
// AlarmNotificationTask 告警通知发送任务 // AlarmNotificationTask 告警通知发送任务
type AlarmNotificationTask struct { type AlarmNotificationTask struct {
ctx context.Context ctx context.Context
taskLog *models.TaskExecutionLog taskLog *models.TaskExecutionLog
params *AlarmNotificationTaskParams
// notificationIntervals 告警通知的发送间隔时间,键为告警等级,值为 time.Duration
notificationIntervals map[models.SeverityLevel]time.Duration
onceParse sync.Once // 保证解析参数只执行一次 onceParse sync.Once // 保证解析参数只执行一次
@@ -48,7 +50,7 @@ func (t *AlarmNotificationTask) Execute(ctx context.Context) error {
return err return err
} }
// TODO: 实现告警通知发送逻辑,可以使用 t.params.NotificationIntervals 来获取不同等级的发送间隔 // TODO: 实现告警通知发送逻辑,可以使用 t.notificationIntervals 来获取不同等级的发送间隔
logger.Infof("告警通知发送任务执行完成, 任务ID: %d", t.taskLog.TaskID) logger.Infof("告警通知发送任务执行完成, 任务ID: %d", t.taskLog.TaskID)
return nil return nil
@@ -87,12 +89,14 @@ func (t *AlarmNotificationTask) parseParameters(ctx context.Context) error {
return return
} }
// 如果 NotificationIntervals 为 nil则初始化它 // 初始化 notificationIntervals
if params.NotificationIntervals == nil { t.notificationIntervals = make(map[models.SeverityLevel]time.Duration)
params.NotificationIntervals = make(map[models.SeverityLevel]time.Duration)
// 将 uint 类型的秒数转换为 time.Duration
for level, seconds := range params.NotificationIntervals {
t.notificationIntervals[level] = time.Duration(seconds) * time.Minute
} }
t.params = &params
}) })
return err return err
} }

View File

@@ -47,6 +47,9 @@ type Config struct {
// Collection 定时采集配置 // Collection 定时采集配置
Collection CollectionConfig `yaml:"collection"` Collection CollectionConfig `yaml:"collection"`
// AlarmNotification 告警通知配置
AlarmNotification AlarmNotificationConfig `yaml:"alarm_notification"`
} }
// AppConfig 代表应用基础配置 // AppConfig 代表应用基础配置
@@ -204,14 +207,27 @@ type CollectionConfig struct {
Interval int `yaml:"interval"` Interval int `yaml:"interval"`
} }
// NewConfig 创建并返回一个新的配置实例 type NotificationIntervalsConfig struct {
func NewConfig() *Config { // DebugIntervalMinutes Debug级别告警的通知间隔分钟
// 默认值可以在这里设置,但我们优先使用配置文件中的值 DebugIntervalMinutes uint `yaml:"debug"`
return &Config{ // InfoIntervalMinutes Info级别告警的通知间隔分钟
Collection: CollectionConfig{ InfoIntervalMinutes uint `yaml:"info"`
Interval: 1, // 默认为1分钟 // WarnIntervalMinutes Warn级别告警的通知间隔分钟
}, WarnIntervalMinutes uint `yaml:"warn"`
} // ErrorIntervalMinutes Error级别告警的通知间隔分钟
ErrorIntervalMinutes uint `yaml:"error"`
// DPanicIntervalMinutes DPanic级别告警的通知间隔分钟
DPanicIntervalMinutes uint `yaml:"dpanic"`
// PanicIntervalMinutes Panic级别告警的通知间隔分钟
PanicIntervalMinutes uint `yaml:"panic"`
// FatalIntervalMinutes Fatal级别告警的通知间隔分钟
FatalIntervalMinutes uint `yaml:"fatal"`
}
// AlarmNotificationConfig 告警通知配置
type AlarmNotificationConfig struct {
// NotificationIntervals 告警通知的发送间隔时间,键为告警等级,值为时间间隔(秒)
NotificationIntervals NotificationIntervalsConfig `yaml:"notification_intervals"`
} }
// Load 从指定路径加载配置文件 // Load 从指定路径加载配置文件