美文网首页
11、GO语言使用gopsutil包进行机器信息采集

11、GO语言使用gopsutil包进行机器信息采集

作者: 灵魂深灵 | 来源:发表于2019-08-09 14:13 被阅读0次
package main

import (
    "fmt"
    "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"
    "time"
)

func main() {
    //获取本机信息
    h,_ := host.Info()
    fmt.Println("本机信息:",h)

    //获取CPU信息
    c,_ := cpu.Info()
    fmt.Println("cpu信息:",c)
    /*用户CPU时间/系统CPU时间/空闲时间。。。等等
    用户CPU时间:就是用户的进程获得了CPU资源以后,在用户态执行的时间。
    系统CPU时间:用户进程获得了CPU资源以后,在内核态的执行时间。
    */
    c1,_ := cpu.Times(false)
    fmt.Println("cpu1:",c1)

    //CPU使用率,每秒刷新一次
    for{
     c2, _ := cpu.Percent(time.Duration(time.Second), false)
     fmt.Println(c2)
    }

    //获取物理内存和交换区内存信息
    m1, _ := mem.VirtualMemory()
    fmt.Println("m1:",m1)
    m2, _ := mem.SwapMemory()
    fmt.Println("m2:",m2)

    //可以通过psutil获取磁盘分区、磁盘使用率和磁盘IO信息
    d1, _ := disk.Partitions(true)  //所有分区
    fmt.Println("d1:",d1)
    d2, _ := disk.Usage("E:")  //指定某路径的硬盘使用情况
    fmt.Println("d2:",d2)
    d3, _ := disk.IOCounters()  //所有硬盘的io信息
    fmt.Println("d3:",d3)

    //获取当前网络连接信息
    n1, _ := net.Connections("all")  //可填入tcp、udp、tcp4、udp4等等
    fmt.Println("n1:",n1)

    //获取网络读写字节/包的个数
    n2, _ := net.IOCounters(false)
    fmt.Println("n2:",n2)

    //获取到所有进程的详细信息
    p1, _ := process.Pids()  //获取当前所有进程的pid
    fmt.Println("p1:",p1)
    p2,_ := process.GetWin32Proc(1120)  //对应pid的进程信息
    fmt.Println("p2:",p2)
    //fmt.Println(p2[0].ParentProcessID)  //获取父进程的pid
}

相关文章

  • 11、GO语言使用gopsutil包进行机器信息采集

    参考:https://blog.csdn.net/sinat_26682309/article/details/9...

  • shirou/gopsutil/disk

    shirou/gopsutil/disk子包用于获取磁盘信息,包括IO统计、分区、使用率等。 分区信息 disk....

  • 模板的使用

    模版 Go模板使用 在Go语言中,我们使用template包来进行模版处理,使用类似Parse,ParseFile...

  • Gox语言中下载文件-GX50.1

    Gox语言中可以使用基本的Go语言标准包(net/http包)进行文件下载,也可以使用内置的github.com/...

  • go语言执行grep -v grep爬坑

    使用go语言的golang.org/x/crypto/ssh包,执行远程机器上的进程检查命令ps -ef | g...

  • 理解Go语言包(package)

    1. Go语言包的概念 Go语言使用包来组织源代码的,并实现命名空间的管理,任何一个Go语言程序必须属于一个包,即...

  • context 使用简介

    简介 在使用 Go 语言进行网络编程时,经常需要使用 context 来传递跟每个 Request 相关的信息,因...

  • go json 实践中遇到的坑

    在使用 go 语言开发过程中,经常需要使用到 json 包来进行 json 和 struct 的互相转换,在使用过...

  • TODO:Go语言同名Go字体发布

    TODO:Go语言同名Go字体发布 2016-11-16 Go语言官方博客发布了一款同名字体–Go字体。此字体族包...

  • Golang io reader writer

    推荐阅读Go语言小贴士1 - io包Go语言小贴士2 - 协议解析Go语言小贴士3 - bufio包 一、《GO语...

网友评论

      本文标题:11、GO语言使用gopsutil包进行机器信息采集

      本文链接:https://www.haomeiwen.com/subject/bnfodctx.html