618临近了,介绍一个go版本的系统采集工具包 gopsutil ; python 也有同样的工具包psutil,及时上手一个小脚本跑一把体验下;功能还是很丰富的。go语言本身拥有极强的性能,也非常适合做一些后端的数据采集管理以及运维系统。废话不多说,直接来个示例走一下。

官网地址:

github 上的地址是

示例:

package main

import (

“fmt”

“time”

“github.com/shirou/gopsutil/cpu”

“github.com/shirou/gopsutil/disk”

“github.com/shirou/gopsutil/h OS t”

“github.com/shirou/gopsutil/mem”

“github.com/shirou/gopsutil/net”

)

func main() {

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. Core s

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(“SSD : %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)

}

结果:

Mem : 7888 MB Free: 3591 MB Used:4296 Usage:54.000000%

CPU : Intel(R) Core(TM) i7-5500U CPU @ 2.80GHz 8 cores

Network: 26110662 bytes / 3584665 bytes

SystemBoot:2018-06-13 21:06:29

CPU Used : used 2.307692%

SSD : 119 GB Free: 45 GB Usage:61.544903%

OS : Microsoft Windows 10 企业版(Standalone Workstation) 6.1.7601 Build 7601

Hostname : XXOO

更多内容请关注每日编程,每天进步一点。