Files
trihuy-russian/web/job/check_cpu_usage.go
T

37 lines
989 B
Go
Raw Normal View History

2023-03-17 19:37:49 +03:30
package job
import (
2023-05-21 03:30:26 +04:30
"strconv"
2023-03-17 19:37:49 +03:30
"time"
2024-03-11 01:01:24 +03:30
2025-09-19 10:05:43 +02:00
"github.com/mhsanaei/3x-ui/v2/web/service"
2023-03-17 19:37:49 +03:30
2024-06-17 21:48:49 +02:00
"github.com/shirou/gopsutil/v4/cpu"
2023-03-17 19:37:49 +03:30
)
2025-09-20 09:35:50 +02:00
// CheckCpuJob monitors CPU usage and sends Telegram notifications when usage exceeds the configured threshold.
2023-03-17 19:37:49 +03:30
type CheckCpuJob struct {
tgbotService service.Tgbot
settingService service.SettingService
}
2025-09-20 09:35:50 +02:00
// NewCheckCpuJob creates a new CPU monitoring job instance.
2023-03-17 19:37:49 +03:30
func NewCheckCpuJob() *CheckCpuJob {
return new(CheckCpuJob)
}
2025-09-20 09:35:50 +02:00
// Run checks CPU usage over the last minute and sends a Telegram alert if it exceeds the threshold.
2023-03-17 19:37:49 +03:30
func (j *CheckCpuJob) Run() {
threshold, _ := j.settingService.GetTgCpu()
// get latest status of server
percent, err := cpu.Percent(1*time.Minute, false)
2023-03-17 19:37:49 +03:30
if err == nil && percent[0] > float64(threshold) {
2023-05-21 03:30:26 +04:30
msg := j.tgbotService.I18nBot("tgbot.messages.cpuThreshold",
"Percent=="+strconv.FormatFloat(percent[0], 'f', 2, 64),
"Threshold=="+strconv.Itoa(threshold))
2023-03-17 19:37:49 +03:30
j.tgbotService.SendMsgToTgbotAdmins(msg)
}
}