package agent import ( "runtime" "sync" "syscall" "github.com/x-grk/longpolling-remote-access/internal/proto" ) // statMu serializes samples: the CPU figure is a delta against the last one. var statMu sync.Mutex // sysStat samples the machine's load. It costs nothing while nobody asks: the // panel requests it only for an open status page. func sysStat() (proto.SysStatResult, error) { statMu.Lock() defer statMu.Unlock() res := proto.SysStatResult{NCPU: runtime.NumCPU()} cpu, err := cpuPercent() if err != nil { return res, err } res.CPU = cpu if res.MemTotal, res.MemUsed, err = memUsage(); err != nil { return res, err } var fs syscall.Statfs_t if err := syscall.Statfs("/", &fs); err != nil { return res, err } res.DiskTotal = uint64(fs.Blocks) * uint64(fs.Bsize) res.DiskFree = uint64(fs.Bavail) * uint64(fs.Bsize) return res, nil } func clampPercent(v float64) float64 { if v < 0 { return 0 } if v > 100 { return 100 } return v }