parent
1bb07092b2
commit
50a0ac09ef
|
@ -16,84 +16,84 @@ go get github.com/shirou/gopsutil
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
"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
|
||||
hostInfo, _ := host.Info()
|
||||
return hostInfo
|
||||
}
|
||||
|
||||
func GetCoreCount() int {
|
||||
cpuInfo, _ := cpu.Info()
|
||||
return len(cpuInfo)
|
||||
cpuInfo, _ := cpu.Info()
|
||||
return len(cpuInfo)
|
||||
}
|
||||
|
||||
func GetModelName(idx int) string {
|
||||
cpuInfo, _ := cpu.Info()
|
||||
return cpuInfo[idx].ModelName
|
||||
cpuInfo, _ := cpu.Info()
|
||||
return cpuInfo[idx].ModelName
|
||||
}
|
||||
|
||||
func GetCpuUsage() float64 {
|
||||
percent, _ := cpu.Percent(time.Second, false)
|
||||
return percent[0]
|
||||
percent, _ := cpu.Percent(time.Second, false)
|
||||
return percent[0]
|
||||
}
|
||||
|
||||
func GetMemUsage() float64 {
|
||||
info, _ := mem.VirtualMemory()
|
||||
return 100.0 * float64(info.Total-info.Available) / float64(info.Total)
|
||||
info, _ := mem.VirtualMemory()
|
||||
return 100.0 * float64(info.Total-info.Available) / float64(info.Total)
|
||||
}
|
||||
|
||||
func GetDiskUsage() float64 {
|
||||
info, _ := disk.Usage("/")
|
||||
return info.UsedPercent
|
||||
info, _ := disk.Usage("/")
|
||||
return info.UsedPercent
|
||||
}
|
||||
|
||||
func GetNetStat() []net.ConnectionStat {
|
||||
info, _ := net.Connections("all")
|
||||
return info
|
||||
info, _ := net.Connections("all")
|
||||
return info
|
||||
}
|
||||
|
||||
func GetIOStat() []net.IOCountersStat {
|
||||
info, _ := net.IOCounters(false)
|
||||
return info
|
||||
info, _ := net.IOCounters(false)
|
||||
return info
|
||||
}
|
||||
|
||||
func GetPIDs() []int32 {
|
||||
info, _ := process.Pids()
|
||||
return info
|
||||
info, _ := process.Pids()
|
||||
return info
|
||||
}
|
||||
|
||||
func GetProcessInfo() []*process.Process {
|
||||
info, _ := process.Processes()
|
||||
return info
|
||||
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: ", GetCpuUsage())
|
||||
fmt.Println("Mem: ", GetMemUsage())
|
||||
fmt.Println("Dsk: ", GetDiskUsage())
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
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: ", GetCpuUsage())
|
||||
fmt.Println("Mem: ", GetMemUsage())
|
||||
fmt.Println("Dsk: ", GetDiskUsage())
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 3. 外部参考资料
|
||||
|
||||
* [golang 获取cpu 内存 硬盘 使用率 信息 进程信息](https://blog.csdn.net/whatday/article/details/109620192)
|
||||
* [golang 获取cpu 内存 硬盘 使用率 信息 进程信息](https://blog.csdn.net/whatday/article/details/109620192)
|
||||
|
|
Loading…
Reference in New Issue