check_cpu_usage.go 742 B

12345678910111213141516171819202122232425262728293031323334
  1. package job
  2. import (
  3. "strconv"
  4. "time"
  5. "x-ui/web/service"
  6. "github.com/shirou/gopsutil/v4/cpu"
  7. )
  8. type CheckCpuJob struct {
  9. tgbotService service.Tgbot
  10. settingService service.SettingService
  11. }
  12. func NewCheckCpuJob() *CheckCpuJob {
  13. return new(CheckCpuJob)
  14. }
  15. // Here run is a interface method of Job interface
  16. func (j *CheckCpuJob) Run() {
  17. threshold, _ := j.settingService.GetTgCpu()
  18. // get latest status of server
  19. percent, err := cpu.Percent(1*time.Second, false)
  20. if err == nil && percent[0] > float64(threshold) {
  21. msg := j.tgbotService.I18nBot("tgbot.messages.cpuThreshold",
  22. "Percent=="+strconv.FormatFloat(percent[0], 'f', 2, 64),
  23. "Threshold=="+strconv.Itoa(threshold))
  24. j.tgbotService.SendMsgToTgbotAdmins(msg)
  25. }
  26. }