增加获取系统cpu信息

This commit is contained in:
白茶清欢 2021-10-12 18:38:14 +08:00
parent b8c84ebc75
commit dd6446c811
4 changed files with 98 additions and 1 deletions

5
go.mod
View File

@ -53,14 +53,17 @@ require (
github.com/pierrec/lz4 v2.6.0+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/shirou/gopsutil v3.21.9+incompatible // indirect
github.com/tidwall/match v1.0.3 // indirect
github.com/tidwall/pretty v1.1.0 // indirect
github.com/tklauser/go-sysconf v0.3.9 // indirect
github.com/tklauser/numcpus v0.3.0 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect

8
go.sum
View File

@ -139,6 +139,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/shirou/gopsutil v3.21.9+incompatible h1:LTLpUnfX81MkHeCtSrwNKZwuW5Id6kCa7/P43NdcNn4=
github.com/shirou/gopsutil v3.21.9+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@ -154,6 +156,10 @@ github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8=
github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tklauser/go-sysconf v0.3.9 h1:JeUVdAOWhhxVcU6Eqr/ATFHgXk/mmiItdKeJPev3vTo=
github.com/tklauser/go-sysconf v0.3.9/go.mod h1:11DU/5sG7UexIrp/O6g35hrWzu0JxlwQ3LSFUzyeuhs=
github.com/tklauser/numcpus v0.3.0 h1:ILuRUQBtssgnxw0XXIjKUC56fgnOrFoQQ/4+DeU2biQ=
github.com/tklauser/numcpus v0.3.0/go.mod h1:yFGUr7TUHQRAhyqBcEg0Ge34zDBAsIvJJcyE6boqnA8=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
@ -214,6 +220,8 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71 h1:ikCpsnYR+Ew0vu99XlDp55lGgDJdIMx3f4a18jfse/s=
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=

54
system/cpu.go Normal file
View File

@ -0,0 +1,54 @@
// Package system...
//
// Description : system...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-10-12 5:48 下午
package system
import (
"time"
"github.com/shirou/gopsutil/cpu"
)
// GetCPUInfo 获取CPU信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 5:49 下午 2021/10/12
func GetCPUInfo() (*CPUDetail, error) {
var (
cpuInfoList []cpu.InfoStat
cpuTimeList []cpu.TimesStat
percentList []float64
err error
baseInfo cpu.InfoStat
)
if cpuInfoList, err = cpu.Info(); nil != err {
return nil, err
}
if len(cpuInfoList) > 0 {
baseInfo = cpuInfoList[0]
}
if cpuTimeList, err = cpu.Times(true); nil != err {
return nil, err
}
// 使用率
percentList, _ = cpu.Percent(time.Second, true)
logicalCnt, _ := cpu.Counts(true)
physicalCnt, _ := cpu.Counts(false)
return &CPUDetail{
BaseInfo: baseInfo,
Time: cpuTimeList,
Percent: percentList,
CoreCntInfo: CPUCoreCnt{
Logical: logicalCnt,
Physical: physicalCnt,
},
}, nil
}

32
system/define.go Normal file
View File

@ -0,0 +1,32 @@
// Package system...
//
// Description : system...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-10-12 5:48 下午
package system
import "github.com/shirou/gopsutil/cpu"
// CPUDetail CPU详情
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 6:20 下午 2021/10/12
type CPUDetail struct {
BaseInfo cpu.InfoStat `json:"base_info"` // 基础信息
Time []cpu.TimesStat `json:"time"` // cpu时间
Percent []float64 `json:"percent"` // 使用率
CoreCntInfo CPUCoreCnt `json:"cnt_info"` // 核心数信息
}
// CPUCoreCnt cpu核数信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 6:22 下午 2021/10/12
type CPUCoreCnt struct {
Logical int `json:"logical"` // 逻辑核数
Physical int `json:"physical"` // 物理核数
}