补充说明和示例代码.
Signed-off-by: chen.yang <chen.yang@yuzhen-iot.com>
This commit is contained in:
parent
127ef4381e
commit
5bcaaba508
|
@ -1,5 +1,7 @@
|
|||
# Golang 获取硬件参数及 CPU/内存/硬盘 的使用率
|
||||
|
||||
gopsutil 是一组功能强大的包,能够获取内存总数、内存使用率、CPU 信息、CPU 使用率、IO 信息、网络状况、获取进程信息(包括进程状态、运行时间、资源消耗情况、父进程、子进程、线程、用户和组信息等)、进程控制(Create、SendSignal、Suspend、Resume、Terminate、Kill)等。
|
||||
|
||||
## 1. 安装软件包
|
||||
|
||||
```bash
|
||||
|
@ -19,9 +21,17 @@ import (
|
|||
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
"github.com/shirou/gopsutil/disk"
|
||||
"github.com/shirou/gopsutil/host"
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
"github.com/shirou/gopsutil/net"
|
||||
"github.com/shirou/gopsutil/process"
|
||||
)
|
||||
|
||||
func GetHostInfo() *host.InfoStat {
|
||||
hostInfo, _ := host.Info()
|
||||
return hostInfo
|
||||
}
|
||||
|
||||
func GetCoreCount() int {
|
||||
cpuInfo, _ := cpu.Info()
|
||||
return len(cpuInfo)
|
||||
|
@ -47,9 +57,34 @@ func GetDiskPercent() float64 {
|
|||
return diskInfo.UsedPercent
|
||||
}
|
||||
|
||||
func GetNetStat() []net.ConnectionStat {
|
||||
info, _ := net.Connections("all")
|
||||
return info
|
||||
}
|
||||
|
||||
func GetIOStat() []net.IOCountersStat {
|
||||
info, _ := net.IOCounters(false)
|
||||
return info
|
||||
}
|
||||
|
||||
func GetPIDs() []int32 {
|
||||
info, _ := process.Pids()
|
||||
return info
|
||||
}
|
||||
|
||||
func GetProcessInfo() []*process.Process {
|
||||
info, _ := process.Processes()
|
||||
return info
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("", GetHostInfo())
|
||||
fmt.Println("Model Name: ", GetModelName(0))
|
||||
fmt.Println("Core count: ", GetCoreCount())
|
||||
fmt.Println("Net Stat: ", GetNetStat())
|
||||
fmt.Println("IO Stat: ", GetIOStat())
|
||||
fmt.Println("PID: ", GetPIDs())
|
||||
fmt.Println("Process: ", GetProcessInfo())
|
||||
for {
|
||||
fmt.Println("CPU: ", GetCpuPercent())
|
||||
fmt.Println("Mem: ", GetMemPercent())
|
||||
|
@ -58,3 +93,7 @@ func main() {
|
|||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 3. 外部参考资料
|
||||
|
||||
* [golang 获取cpu 内存 硬盘 使用率 信息 进程信息](https://blog.csdn.net/whatday/article/details/109620192)
|
Loading…
Reference in New Issue