[ Cmii ] [ Octopus ] - reformat agent-go - 1

This commit is contained in:
zeaslity
2024-03-29 11:39:14 +08:00
parent aa4412f042
commit 1be48aaac2
52 changed files with 683 additions and 557 deletions

77
agent-go/a_status/CPU.go Normal file
View File

@@ -0,0 +1,77 @@
package a_status
import (
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/load"
"time"
)
type CPUMetric struct {
NumCores int
CPUPercent float64
CPULoads *load.AvgStat
}
type CPUInfo struct {
CPUMetric
CPUInfo []cpu.InfoStat
}
func GetCPUMetric() (*CPUMetric, error) {
numCores, err := cpu.Counts(true)
if err != nil {
return nil, err
}
cpuPercent, err := cpu.Percent(time.Second, false)
if err != nil {
return nil, err
}
cpuLoads, err := load.Avg()
if err != nil {
return nil, err
}
return &CPUMetric{
NumCores: numCores,
CPUPercent: cpuPercent[0],
CPULoads: cpuLoads,
}, nil
}
func GetCPUInfo() (*CPUInfo, error) {
numCores, err := cpu.Counts(true)
if err != nil {
return nil, err
}
cpuPercent, err := cpu.Percent(time.Second, false)
if err != nil {
return nil, err
}
cpuLoads, err := load.Avg()
if err != nil {
return nil, err
}
infoStats, err := cpu.Info()
if err != nil {
return nil, err
}
return &CPUInfo{
CPUMetric: CPUMetric{
NumCores: numCores,
CPUPercent: cpuPercent[0],
CPULoads: cpuLoads,
},
CPUInfo: infoStats,
}, nil
}