在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
转载(原文章地址):https://blog.csdn.net/sinat_26682309/article/details/90457192 文档地址:https://godoc.org/github.com/shirou/gopsutil 1. 获取本机信息info, _ := host.Info() fmt.Println(info) //output: {"hostname":"WIN-SP09TQCP1U8","uptime":25308,"bootTime":1558574107,"procs":175,"os":"windows","platform":"Microsoft Windows 10 Pro","platformFamily":"Standalone Workstation","platformVersion":"10.0.17134 Build 17134","kernelVersion":"","virtualizationSystem":"","virtualizationRole":"","hostid":。。。} 2. 获取CPU信息info, _ := cpu.Info() //总体信息 fmt.Println(info) //output: [{"cpu":0,cores":4,"modelName":"Intel(R) Core(TM) i5-2520M CPU @ 2.50GHz","mhz":2501,。。。] c, _ := cpu.Counts(true) //cpu逻辑数量 fmt.Println(c) //4 c, _ = cpu.Counts(false) //cpu物理核心 fmt.Println(c) //如果是2说明是双核超线程, 如果是4则是4核非超线程 用户CPU时间/系统CPU时间/空闲时间。。。等等 info, _ := cpu.Times(false) fmt.Println(info) //output: [{"cpu":"cpu-total","user":1272.0,"system":1572.7,"idle":23092.3,"nice":0.0,"iowait":0.0,"irq":0.0,。。。}] 用户CPU时间:就是用户的进程获得了CPU资源以后,在用户态执行的时间。 系统CPU时间:用户进程获得了CPU资源以后,在内核态的执行时间。 CPU使用率,每秒刷新一次 for{ info, _ := cpu.Percent(time.Duration(time.Second), false) fmt.Println(info) } 3. 获取内存信息获取物理内存和交换区内存信息 info, _ := mem.VirtualMemory() fmt.Println(info) info2, _ := mem.SwapMemory() fmt.Println(info2) //output: {"total":8129818624,"available":4193423360,"used":3936395264,"usedPercent":48,"free":0,"active":0,"inactive":0,...} {"total":8666689536,"used":4716843008,"free":3949846528,"usedPercent":0.5442496801583825,"sin":0,"sout":0,...} 总内存大小是8129818624 = 8 GB,已用3936395264 = 3.9 GB,使用了48%。而交换区大小是8666689536 = 8 GB。 4. 获取磁盘信息可以通过psutil获取磁盘分区、磁盘使用率和磁盘IO信息: info, _ := disk.Partitions(true) //所有分区 fmt.Println(info) info2, _ := disk.Usage("E:") //指定某路径的硬盘使用情况 fmt.Println(info2) info3, _ := disk.IOCounters() //所有硬盘的io信息 fmt.Println(info3) //output: [{"device":"C:","mountpoint":"C:","fstype":"NTFS","opts":"rw.compress"} {"device":"D:","mountpoint":"D:","fstype":"NTFS","opts":"rw.compress"} {"device":"E:","mountpoint":"E:","fstype":"NTFS","opts":"rw.compress"} ] {"path":"E:","fstype":"","total":107380965376,"free":46790828032,"used":60590137344,"usedPercent":56.425398236866755,"inodesTotal":0,"inodesUsed":0,"inodesFree":0,"inodesUsedPercent":0} map[C::{"readCount":0,"mergedReadCount":0,"writeCount":0,"mergedWriteCount":0,"readBytes":0,"writeBytes":4096,"readTime":0,"writeTime":0,"iopsInProgress":0,"ioTime":0,"weightedIO":0,"name":"C:","serialNumber":"","label":""} 。。。] 5. 获取网络信息获取当前网络连接信息 info, _ := net.Connections("all") //可填入tcp、udp、tcp4、udp4等等 fmt.Println(info) //output: [{"fd":0,"family":2,"type":1,"localaddr":{"ip":"0.0.0.0","port":135},"remoteaddr":{"ip":"0.0.0.0","port":0},"status":"LISTEN","uids":null,"pid":668} {"fd":0,"family":2,"type":1,"localaddr":{"ip":"0.0.0.0","port":445},"remoteaddr":{"ip":"0.0.0.0","port":0},"status":"LISTEN","uids":null,"pid":4} {"fd":0,"family":2,"type":1,"localaddr":{"ip":"0.0.0.0","port":1801},"remoteaddr":{"ip":"0.0.0.0","port":0},"status":"LISTEN","uids":null,"pid":3860} 等等。。。] 获取网络读写字节/包的个数 info, _ := net.IOCounters(false) fmt.Println(info) //output:[{"name":"all","bytesSent":6516450,"bytesRecv":36991210,"packetsSent":21767,"packetsRecv":33990,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0}] 6. 获取进程信息获取到所有进程的详细信息: info, _ := process.Pids() //获取当前所有进程的pid fmt.Println(info) //output: [0 4 96 464 636 740 748 816 852 880 976 348 564 668 912 1048 1120 1184 1268 1288。。。] info2,_ := process.GetWin32Proc(1120) //对应pid的进程信息 fmt.Println(info2) //output: [{svchost.exe 0xc00003e570 0xc00003e580 8 2019-05-23 09:15:28.444192 +0800 CST 5600 4 0xc00003e5b0 0 0 0 0 Win32_ComputerSystem WIN-SE89TTCP7U3 0xc00003e620 Win32_Process 0xc00003e650 0xc00005331e 209 6250000 0xc000053378 0xc0000533b8 Win32_OperatingSystem Microsoft Windows 10 专业版。。。}] fmt.Println(info2[0].ParentProcessID) //获取父进程的pid
|
请发表评论