Some minor fixes
This commit is contained in:
parent
cb2f3905e8
commit
bdfc1a84e0
@ -1,34 +1,54 @@
|
|||||||
package job
|
package job
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"x-ui/web/service"
|
"x-ui/web/service"
|
||||||
|
|
||||||
"github.com/shirou/gopsutil/v4/cpu"
|
"github.com/shirou/gopsutil/v4/cpu"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CheckCpuJob struct {
|
// 连续超阈值告警实现
|
||||||
tgbotService service.Tgbot
|
type CheckCpuJob struct {
|
||||||
settingService service.SettingService
|
tgbotService service.Tgbot
|
||||||
}
|
settingService service.SettingService
|
||||||
|
overThresholdCount int // 连续超阈值计数器
|
||||||
func NewCheckCpuJob() *CheckCpuJob {
|
lastNotifyTime time.Time // 最近一次告警时间
|
||||||
return new(CheckCpuJob)
|
}
|
||||||
}
|
|
||||||
|
func NewCheckCpuJob() *CheckCpuJob {
|
||||||
// Here run is a interface method of Job interface
|
return &CheckCpuJob{}
|
||||||
func (j *CheckCpuJob) Run() {
|
}
|
||||||
threshold, _ := j.settingService.GetTgCpu()
|
|
||||||
|
// run 是 Job 接口方法
|
||||||
// get latest status of server
|
func (j *CheckCpuJob) Run() {
|
||||||
percent, err := cpu.Percent(1*time.Minute, false)
|
threshold, _ := j.settingService.GetTgCpu()
|
||||||
if err == nil && percent[0] > float64(threshold) {
|
notifyInterval := 10 * time.Minute // 两次告警最小间隔,可做成配置项
|
||||||
msg := j.tgbotService.I18nBot("tgbot.messages.cpuThreshold",
|
|
||||||
"Percent=="+strconv.FormatFloat(percent[0], 'f', 2, 64),
|
percent, err := cpu.Percent(10*time.Second, false) // 10秒采样
|
||||||
"Threshold=="+strconv.Itoa(threshold))
|
if err != nil || len(percent) == 0 {
|
||||||
|
return
|
||||||
j.tgbotService.SendMsgToTgbotAdmins(msg)
|
}
|
||||||
}
|
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user