1
0

check_cpu_usage.go 734 B

12345678910111213141516171819202122232425262728293031323334
  1. package job
  2. import (
  3. "time"
  4. "github.com/mhsanaei/3x-ui/v3/internal/eventbus"
  5. "github.com/shirou/gopsutil/v4/cpu"
  6. )
  7. // CheckCpuJob monitors CPU usage and publishes events when threshold is exceeded.
  8. type CheckCpuJob struct{}
  9. // NewCheckCpuJob creates a new CPU monitoring job instance.
  10. func NewCheckCpuJob() *CheckCpuJob {
  11. return new(CheckCpuJob)
  12. }
  13. // Run checks CPU usage and publishes a cpu.high event with raw metric data.
  14. func (j *CheckCpuJob) Run() {
  15. percent, err := cpu.Percent(1*time.Minute, false)
  16. if err != nil || len(percent) == 0 {
  17. return
  18. }
  19. if EventBus != nil {
  20. EventBus.Publish(eventbus.Event{
  21. Type: eventbus.EventCPUHigh,
  22. Data: &eventbus.SystemMetricData{
  23. Percent: percent[0],
  24. },
  25. })
  26. }
  27. }