From d5193a3be1a4c5e972c90917fd7e339fbbca80a1 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Thu, 30 Mar 2023 16:45:01 +0800 Subject: [PATCH] [agent-go] new cpu status --- agent-go/go.mod | 1 + agent-go/go.sum | 2 ++ agent-go/main.go | 9 +++++ agent-go/status/cpu.go | 47 ++++++++++++++++++++++++++ agent-go/tmp/executor-om-pipeline.json | 2 +- 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 agent-go/status/cpu.go diff --git a/agent-go/go.mod b/agent-go/go.mod index 4ffda9b..a5b4461 100644 --- a/agent-go/go.mod +++ b/agent-go/go.mod @@ -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 diff --git a/agent-go/go.sum b/agent-go/go.sum index 88dfd31..887ee29 100644 --- a/agent-go/go.sum +++ b/agent-go/go.sum @@ -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= diff --git a/agent-go/main.go b/agent-go/main.go index 8ed778d..3127e9b 100644 --- a/agent-go/main.go +++ b/agent-go/main.go @@ -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) + } diff --git a/agent-go/status/cpu.go b/agent-go/status/cpu.go new file mode 100644 index 0000000..b3d46f3 --- /dev/null +++ b/agent-go/status/cpu.go @@ -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) +} diff --git a/agent-go/tmp/executor-om-pipeline.json b/agent-go/tmp/executor-om-pipeline.json index 36044a2..2bf979e 100644 --- a/agent-go/tmp/executor-om-pipeline.json +++ b/agent-go/tmp/executor-om-pipeline.json @@ -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" }