From 5bcaaba508899b424b0ad5327847fbaa2f936c79 Mon Sep 17 00:00:00 2001 From: "chen.yang" Date: Thu, 19 Aug 2021 14:56:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E8=AF=B4=E6=98=8E=E5=92=8C?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chen.yang --- ...ng_获取硬件参数及_CPU_内存_硬盘的使用率.md | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/Software/Development/Language/Go/Package/Gopsutil/Golang_获取硬件参数及_CPU_内存_硬盘的使用率.md b/Software/Development/Language/Go/Package/Gopsutil/Golang_获取硬件参数及_CPU_内存_硬盘的使用率.md index 98c65bf..4f630af 100644 --- a/Software/Development/Language/Go/Package/Gopsutil/Golang_获取硬件参数及_CPU_内存_硬盘的使用率.md +++ b/Software/Development/Language/Go/Package/Gopsutil/Golang_获取硬件参数及_CPU_内存_硬盘的使用率.md @@ -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()) @@ -57,4 +92,8 @@ func main() { time.Sleep(1 * time.Second) } } -``` \ No newline at end of file +``` + +## 3. 外部参考资料 + +* [golang 获取cpu 内存 硬盘 使用率 信息 进程信息](https://blog.csdn.net/whatday/article/details/109620192) \ No newline at end of file