parent
1bb07092b2
commit
50a0ac09ef
|
@ -16,84 +16,84 @@ go get github.com/shirou/gopsutil
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/shirou/gopsutil/cpu"
|
"github.com/shirou/gopsutil/cpu"
|
||||||
"github.com/shirou/gopsutil/disk"
|
"github.com/shirou/gopsutil/disk"
|
||||||
"github.com/shirou/gopsutil/host"
|
"github.com/shirou/gopsutil/host"
|
||||||
"github.com/shirou/gopsutil/mem"
|
"github.com/shirou/gopsutil/mem"
|
||||||
"github.com/shirou/gopsutil/net"
|
"github.com/shirou/gopsutil/net"
|
||||||
"github.com/shirou/gopsutil/process"
|
"github.com/shirou/gopsutil/process"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetHostInfo() *host.InfoStat {
|
func GetHostInfo() *host.InfoStat {
|
||||||
hostInfo, _ := host.Info()
|
hostInfo, _ := host.Info()
|
||||||
return hostInfo
|
return hostInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCoreCount() int {
|
func GetCoreCount() int {
|
||||||
cpuInfo, _ := cpu.Info()
|
cpuInfo, _ := cpu.Info()
|
||||||
return len(cpuInfo)
|
return len(cpuInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetModelName(idx int) string {
|
func GetModelName(idx int) string {
|
||||||
cpuInfo, _ := cpu.Info()
|
cpuInfo, _ := cpu.Info()
|
||||||
return cpuInfo[idx].ModelName
|
return cpuInfo[idx].ModelName
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCpuUsage() float64 {
|
func GetCpuUsage() float64 {
|
||||||
percent, _ := cpu.Percent(time.Second, false)
|
percent, _ := cpu.Percent(time.Second, false)
|
||||||
return percent[0]
|
return percent[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMemUsage() float64 {
|
func GetMemUsage() float64 {
|
||||||
info, _ := mem.VirtualMemory()
|
info, _ := mem.VirtualMemory()
|
||||||
return 100.0 * float64(info.Total-info.Available) / float64(info.Total)
|
return 100.0 * float64(info.Total-info.Available) / float64(info.Total)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDiskUsage() float64 {
|
func GetDiskUsage() float64 {
|
||||||
info, _ := disk.Usage("/")
|
info, _ := disk.Usage("/")
|
||||||
return info.UsedPercent
|
return info.UsedPercent
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetNetStat() []net.ConnectionStat {
|
func GetNetStat() []net.ConnectionStat {
|
||||||
info, _ := net.Connections("all")
|
info, _ := net.Connections("all")
|
||||||
return info
|
return info
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetIOStat() []net.IOCountersStat {
|
func GetIOStat() []net.IOCountersStat {
|
||||||
info, _ := net.IOCounters(false)
|
info, _ := net.IOCounters(false)
|
||||||
return info
|
return info
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPIDs() []int32 {
|
func GetPIDs() []int32 {
|
||||||
info, _ := process.Pids()
|
info, _ := process.Pids()
|
||||||
return info
|
return info
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetProcessInfo() []*process.Process {
|
func GetProcessInfo() []*process.Process {
|
||||||
info, _ := process.Processes()
|
info, _ := process.Processes()
|
||||||
return info
|
return info
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("", GetHostInfo())
|
fmt.Println("", GetHostInfo())
|
||||||
fmt.Println("Model Name: ", GetModelName(0))
|
fmt.Println("Model Name: ", GetModelName(0))
|
||||||
fmt.Println("Core count: ", GetCoreCount())
|
fmt.Println("Core count: ", GetCoreCount())
|
||||||
fmt.Println("Net Stat: ", GetNetStat())
|
fmt.Println("Net Stat: ", GetNetStat())
|
||||||
fmt.Println("IO Stat: ", GetIOStat())
|
fmt.Println("IO Stat: ", GetIOStat())
|
||||||
fmt.Println("PID: ", GetPIDs())
|
fmt.Println("PID: ", GetPIDs())
|
||||||
fmt.Println("Process: ", GetProcessInfo())
|
fmt.Println("Process: ", GetProcessInfo())
|
||||||
for {
|
for {
|
||||||
fmt.Println("CPU: ", GetCpuUsage())
|
fmt.Println("CPU: ", GetCpuUsage())
|
||||||
fmt.Println("Mem: ", GetMemUsage())
|
fmt.Println("Mem: ", GetMemUsage())
|
||||||
fmt.Println("Dsk: ", GetDiskUsage())
|
fmt.Println("Dsk: ", GetDiskUsage())
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## 3. 外部参考资料
|
## 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