2021-10-12 18:38:14 +08:00
|
|
|
// Package system...
|
|
|
|
//
|
|
|
|
// Description : system...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2021-10-12 5:48 下午
|
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2021-10-12 18:49:27 +08:00
|
|
|
"github.com/shirou/gopsutil/load"
|
|
|
|
|
2021-10-12 18:38:14 +08:00
|
|
|
"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)
|
|
|
|
|
2021-10-12 18:49:27 +08:00
|
|
|
avg, _ := load.Avg()
|
|
|
|
|
2021-10-12 18:38:14 +08:00
|
|
|
return &CPUDetail{
|
|
|
|
BaseInfo: baseInfo,
|
|
|
|
Time: cpuTimeList,
|
|
|
|
Percent: percentList,
|
|
|
|
CoreCntInfo: CPUCoreCnt{
|
|
|
|
Logical: logicalCnt,
|
|
|
|
Physical: physicalCnt,
|
|
|
|
},
|
2021-10-12 18:49:27 +08:00
|
|
|
Load: avg,
|
2021-10-12 18:38:14 +08:00
|
|
|
}, nil
|
|
|
|
}
|