From bdfc1a84e07534f0308a51d5bfff788c5914014b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=83=E9=9A=A8=E7=B7=A3=E5=8B=95?= Date: Sat, 27 Sep 2025 23:28:26 +0800 Subject: [PATCH] Some minor fixes --- web/job/check_cpu_usage.go | 86 +++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 33 deletions(-) diff --git a/web/job/check_cpu_usage.go b/web/job/check_cpu_usage.go index cd9fcc9a..4d312ff3 100644 --- a/web/job/check_cpu_usage.go +++ b/web/job/check_cpu_usage.go @@ -1,34 +1,54 @@ -package job - -import ( - "strconv" - "time" - - "x-ui/web/service" - - "github.com/shirou/gopsutil/v4/cpu" -) - -type CheckCpuJob struct { - tgbotService service.Tgbot - settingService service.SettingService -} - -func NewCheckCpuJob() *CheckCpuJob { - return new(CheckCpuJob) -} - -// Here run is a interface method of Job interface -func (j *CheckCpuJob) Run() { - threshold, _ := j.settingService.GetTgCpu() - - // get latest status of server - percent, err := cpu.Percent(1*time.Minute, false) - if err == nil && percent[0] > float64(threshold) { - msg := j.tgbotService.I18nBot("tgbot.messages.cpuThreshold", - "Percent=="+strconv.FormatFloat(percent[0], 'f', 2, 64), - "Threshold=="+strconv.Itoa(threshold)) - - j.tgbotService.SendMsgToTgbotAdmins(msg) - } +package job + +import ( + "strconv" + "time" + + "x-ui/web/service" + + "github.com/shirou/gopsutil/v4/cpu" +) + +// 连续超阈值告警实现 +type CheckCpuJob struct { + tgbotService service.Tgbot + settingService service.SettingService + overThresholdCount int // 连续超阈值计数器 + lastNotifyTime time.Time // 最近一次告警时间 +} + +func NewCheckCpuJob() *CheckCpuJob { + return &CheckCpuJob{} +} + +// run 是 Job 接口方法 +func (j *CheckCpuJob) Run() { + threshold, _ := j.settingService.GetTgCpu() + notifyInterval := 10 * time.Minute // 两次告警最小间隔,可做成配置项 + + percent, err := cpu.Percent(10*time.Second, false) // 10秒采样 + if err != nil || len(percent) == 0 { + return + } + + now := time.Now() + if percent[0] > float64(threshold) { + j.overThresholdCount++ + } else { + j.overThresholdCount = 0 + } + + // 连续3次超阈值,且距离上次告警超过告警间隔 + if j.overThresholdCount >= 3 && now.Sub(j.lastNotifyTime) > notifyInterval { + msg := j.tgbotService.I18nBot( + "tgbot.messages.cpuThreshold", + "Percent=="+strconv.FormatFloat(percent[0], 'f', 2, 64), + "Threshold=="+strconv.Itoa(threshold), + "SampleInterval==10s", + "NotifyPolicy==连续3次超阈值", + ) + j.tgbotService.SendMsgToTgbotAdmins(msg) + j.lastNotifyTime = now + j.overThresholdCount = 0 + } }