diff --git a/web/job/check_cpu_usage.go b/web/job/check_cpu_usage.go new file mode 100644 index 00000000..cfc86b60 --- /dev/null +++ b/web/job/check_cpu_usage.go @@ -0,0 +1,30 @@ +package job + +import ( + "fmt" + "time" + "x-ui/web/service" + + "github.com/shirou/gopsutil/v3/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.Second, false) + if err == nil && percent[0] > float64(threshold) { + msg := fmt.Sprintf("🔴 CPU usage %.2f%% is more than threshold %d%%", percent[0], threshold) + j.tgbotService.SendMsgToTgbotAdmins(msg) + } +} diff --git a/web/job/stats_notify_job.go b/web/job/stats_notify_job.go index 365f41ae..ae5eba70 100644 --- a/web/job/stats_notify_job.go +++ b/web/job/stats_notify_job.go @@ -12,9 +12,8 @@ const ( ) type StatsNotifyJob struct { - xrayService service.XrayService - inboundService service.InboundService - tgbotService service.Tgbot + xrayService service.XrayService + tgbotService service.Tgbot } func NewStatsNotifyJob() *StatsNotifyJob { diff --git a/web/translation/translate.fa_IR.toml b/web/translation/translate.fa_IR.toml index 18ebc1a8..9c0e092c 100644 --- a/web/translation/translate.fa_IR.toml +++ b/web/translation/translate.fa_IR.toml @@ -74,7 +74,7 @@ "xraySwitchClickDesk" = "لطفا با دقت انتخاب کنید ، در صورت انتخاب اشتباه امکان قطعی سیستم وجود دارد ." "operationHours" = "ساعت فعال" "operationHoursDesc" = "ساعت فعال بعد از شروع سیستم" -"systemLoad" = "سرعت لود سیستم" +"systemLoad" = "بار روی سیستم" "connectionCount" = "تعداد کانکشن ها" "connectionCountDesc" = "تعداد کانکشن ها برای کل شبکه" "upSpeed" = "سرعت آپلود در حال حاضر سیستم" diff --git a/web/web.go b/web/web.go index 7a68d1ee..4880ff19 100644 --- a/web/web.go +++ b/web/web.go @@ -325,6 +325,13 @@ func (s *Server) startTask() { logger.Warning("Add NewStatsNotifyJob error", err) return } + + // Check CPU load and alarm to TgBot if threshold passes + cpuThreshold, err := s.settingService.GetTgCpu() + if (err == nil) && (cpuThreshold > 0) { + s.cron.AddJob("@every 10s", job.NewCheckCpuJob()) + } + } else { s.cron.Remove(entry) }