前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GO语言使用gopsutil包进行机器信息采集

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

作者头像
李海彬
发布2018-03-26 14:01:01
4.5K0
发布2018-03-26 14:01:01
举报
文章被收录于专栏:Golang语言社区Golang语言社区

GO语言本身拥有极强的性能,非常适合做一些后端的数据采集管理以及运维系统。

其中会面临对当前系统信息的采集,我在这里使用的是GO的工具包 gopsutil

贴出一套测试代码,抛砖引玉:

代码语言:javascript
复制
import (    "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")

func collet() {
    v, _ := mem.VirtualMemory()
    c, _ := cpu.Info()
    cc, _ := cpu.Percent(time.Second, false)
    d, _ := disk.Usage("/")
    n, _ := host.Info()
    nv, _ := net.IOCounters(true)
    boottime, _ := host.BootTime()
    btime := time.Unix(int64(boottime), 0).Format("2006-01-02 15:04:05")

    fmt.Printf("        Mem       : %v MB  Free: %v MB Used:%v Usage:%f%%\n", v.Total/1024/1024, v.Available/1024/1024, v.Used/1024/1024, v.UsedPercent)    if len(c) > 1 {        for _, sub_cpu := range c {
            modelname := sub_cpu.ModelName
            cores := sub_cpu.Cores
            fmt.Printf("        CPU       : %v   %v cores \n", modelname, cores)
        }
    } else {
        sub_cpu := c[0]
        modelname := sub_cpu.ModelName
        cores := sub_cpu.Cores
        fmt.Printf("        CPU       : %v   %v cores \n", modelname, cores)

    }
    fmt.Printf("        Network: %v bytes / %v bytes\n", nv[0].BytesRecv, nv[0].BytesSent)
    fmt.Printf("        SystemBoot:%v\n", btime)
    fmt.Printf("        CPU Used    : used %f%% \n", cc[0])
    fmt.Printf("        HD        : %v GB  Free: %v GB Usage:%f%%\n", d.Total/1024/1024/1024, d.Free/1024/1024/1024, d.UsedPercent)
    fmt.Printf("        OS        : %v(%v)   %v  \n", n.Platform, n.PlatformFamily, n.PlatformVersion)
    fmt.Printf("        Hostname  : %v  \n", n.Hostname)
}

代码中还包含CPU使用率采集,可用内存采集以及网络数据包收发采集,目前网络数据采集尚不稳定。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-03-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档