[agent-go] new cpu status
This commit is contained in:
47
agent-go/status/cpu.go
Normal file
47
agent-go/status/cpu.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package status
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
linuxproc "github.com/c9s/goprocinfo/linux"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetCpuMap() (map[string]uint64, error) {
|
||||
statA, err := linuxproc.ReadStat("/proc/stat")
|
||||
statErrMsg := "failed to stat CPU data, received error: %s"
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(statErrMsg, err.Error())
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
statB, err := linuxproc.ReadStat("/proc/stat")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(statErrMsg, err.Error())
|
||||
}
|
||||
|
||||
resultMap := make(map[string]uint64)
|
||||
resultMap["all_active_percent"] = cpuStatToPercent(statA.CPUStatAll, statB.CPUStatAll)
|
||||
for idx, statB := range statB.CPUStats {
|
||||
statA := statA.CPUStats[idx]
|
||||
resultMap[statB.Id+"_active_percent"] = cpuStatToPercent(statA, statB)
|
||||
}
|
||||
|
||||
return resultMap, nil
|
||||
}
|
||||
|
||||
func cpuStatToPercent(statA, statB linuxproc.CPUStat) uint64 {
|
||||
aIdle := statA.Idle + statA.IOWait
|
||||
bIdle := statB.Idle + statB.IOWait
|
||||
|
||||
aNonIdle := statA.User + statA.Nice + statA.System + statA.IRQ + statA.SoftIRQ + statA.Steal
|
||||
bNonIdle := statB.User + statB.Nice + statB.System + statB.IRQ + statB.SoftIRQ + statB.Steal
|
||||
|
||||
aTotal := aIdle + aNonIdle
|
||||
bTotal := bIdle + bNonIdle
|
||||
|
||||
totalDiff := bTotal - aTotal
|
||||
idleDiff := bIdle - aIdle
|
||||
|
||||
return uint64((float64(totalDiff-idleDiff) / float64(totalDiff)) * 100)
|
||||
}
|
||||
Reference in New Issue
Block a user