[agent-go] new cpu status
This commit is contained in:
@@ -3,6 +3,7 @@ module agent-go
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8
|
||||
github.com/nacos-group/nacos-sdk-go/v2 v2.2.0
|
||||
github.com/panjf2000/ants/v2 v2.7.2
|
||||
github.com/spf13/viper v1.15.0
|
||||
|
||||
@@ -55,6 +55,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
|
||||
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
|
||||
github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8 h1:SjZ2GvvOononHOpK84APFuMvxqsk3tEIaKH/z4Rpu3g=
|
||||
github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8/go.mod h1:uEyr4WpAH4hio6LFriaPkL938XnrvLpNPmQHBdrmbIE=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"agent-go/g"
|
||||
logger2 "agent-go/logger"
|
||||
"agent-go/register"
|
||||
"agent-go/status"
|
||||
"flag"
|
||||
"fmt"
|
||||
)
|
||||
@@ -26,4 +27,12 @@ func main() {
|
||||
// 执行初始化之策工作
|
||||
register.AgentServerInfoCache = register.INIT()
|
||||
|
||||
// 测试调用CPU状态
|
||||
getCpuMap, err := status.GetCpuMap()
|
||||
if err != nil {
|
||||
log.ErrorF("error is %v", err)
|
||||
}
|
||||
|
||||
log.InfoF("cpu status is %v", getCpuMap)
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
"uuid": "2023-03-27 14:38:49",
|
||||
"init_time": "2023-03-27T14:38:49.8162801+08:00",
|
||||
"type": "EXECUTOR",
|
||||
"content": "{\n \"needResultReplay\": true,\n \"durationTask,default:false\": false,\n \"type\": \"pipeline\",\n \"singleLineCommand\": null,\n \"multiLineCommand\": null,\n \"pipeLineCommand\": [[\"echo\",\"yes china\"],[\"awk\",\"'{print$2}'\"]],\n \"resultKey\": \"output\"\n}\n",
|
||||
"content": "{\n \"needResultReplay\": true,\n \"durationTask,default:false\": false,\n \"type\": \"pipeline\",\n \"singleLineCommand\": null,\n \"multiLineCommand\": null,\n \"pipeLineCommand\": [[\"ls\",\"-la\"],[\"grep\",\"-c\", \"dev\"]],\n \"resultKey\": \"output\"\n}\n",
|
||||
"result": "",
|
||||
"ac_time": "0001-01-01T00:00:00Z"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user