增加获取系统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

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"` // 物理核数
}