Files
ProjectOctopus/agent-go/status/CPU.go
2023-06-15 17:03:22 +08:00

51 lines
816 B
Go

package status
import (
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/load"
)
type CPUStatus struct {
NumCores int
CPUInfo []cpu.InfoStat
CPUPercent float64
CPULoads *load.AvgStat
SystemLoads *load.AvgStat
}
func GetCPUStatus() (*CPUStatus, error) {
numCores, err := cpu.Counts(true)
if err != nil {
return nil, err
}
cpuInfo, err := cpu.Info()
if err != nil {
return nil, err
}
cpuPercent, err := cpu.Percent(0, false)
if err != nil {
return nil, err
}
cpuLoads, err := load.Avg()
if err != nil {
return nil, err
}
systemLoads, err := load.Avg()
if err != nil {
return nil, err
}
return &CPUStatus{
NumCores: numCores,
CPUInfo: cpuInfo,
CPUPercent: cpuPercent[0],
CPULoads: cpuLoads,
SystemLoads: systemLoads,
}, nil
}