博客
关于我
Go语言获取系统性能数据gopsutil库
阅读量:447 次
发布时间:2019-03-06

本文共 2419 字,大约阅读时间需要 8 分钟。

目录

go 从入门到放弃完整教程目录(更有python、go、pytorch、tensorflow、爬虫、人工智能教学等着你):

更新、更全的《Go从入门到放弃》的更新网站,更有python、go、人工智能教学等着你:
psutil是一个跨平台进程和系统监控的Python库,而gopsutil是其Go语言版本的实现。本文介绍了它的基本使用。

Go语言部署简单、性能好的特点非常适合做一些诸如采集系统信息和监控的服务,本文介绍的gopsutil库是知名Python库:psutil的一个Go语言版本的实现。

一、安装

go get github.com/shirou/gopsutil

二、使用

2.1 CPU

采集CPU相关信息。

import "github.com/shirou/gopsutil/cpu"// cpu infofunc getCpuInfo() {	cpuInfos, err := cpu.Info()	if err != nil {		fmt.Printf("get cpu info failed, err:%v", err)	}	for _, ci := range cpuInfos {		fmt.Println(ci)	}	// CPU使用率	for {		percent, _ := cpu.Percent(time.Second, false)		fmt.Printf("cpu percent:%v\n", percent)	}}

获取CPU负载信息:

import "github.com/shirou/gopsutil/load"func getCpuLoad() {	info, _ := load.Avg()	fmt.Printf("%v\n", info)}

2.2 Memory

import "github.com/shirou/gopsutil/mem"// mem infofunc getMemInfo() {	memInfo, _ := mem.VirtualMemory()	fmt.Printf("mem info:%v\n", memInfo)}

2.3 Host

import "github.com/shirou/gopsutil/host"// host infofunc getHostInfo() {	hInfo, _ := host.Info()	fmt.Printf("host info:%v uptime:%v boottime:%v\n", hInfo, hInfo.Uptime, hInfo.BootTime)}

2.4 Disk

import "github.com/shirou/gopsutil/disk"// disk infofunc getDiskInfo() {	parts, err := disk.Partitions(true)	if err != nil {		fmt.Printf("get Partitions failed, err:%v\n", err)		return	}	for _, part := range parts {		fmt.Printf("part:%v\n", part.String())		diskInfo, _ := disk.Usage(part.Mountpoint)		fmt.Printf("disk info:used:%v free:%v\n", diskInfo.UsedPercent, diskInfo.Free)	}	ioStat, _ := disk.IOCounters()	for k, v := range ioStat {		fmt.Printf("%v:%v\n", k, v)	}}

2.5 net IO

import "github.com/shirou/gopsutil/net"func getNetInfo() {	info, _ := net.IOCounters(true)	for index, v := range info {		fmt.Printf("%v:%v send:%v recv:%v\n", index, v, v.BytesSent, v.BytesRecv)	}}

三、net

3.1 获取本机IP的两种方式

func GetLocalIP() (ip string, err error) {	addrs, err := net.InterfaceAddrs()	if err != nil {		return	}	for _, addr := range addrs {		ipAddr, ok := addr.(*net.IPNet)		if !ok {			continue		}		if ipAddr.IP.IsLoopback() {			continue		}		if !ipAddr.IP.IsGlobalUnicast() {			continue		}		return ipAddr.IP.String(), nil	}	return}

或:

// Get preferred outbound ip of this machinefunc GetOutboundIP() string {	conn, err := net.Dial("udp", "8.8.8.8:80")	if err != nil {		log.Fatal(err)	}	defer conn.Close()	localAddr := conn.LocalAddr().(*net.UDPAddr)	fmt.Println(localAddr.String())	return localAddr.IP.String()}

转载地址:http://kxgyz.baihongyu.com/

你可能感兴趣的文章
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>
mysql丢失更新问题
查看>>
MySQL两千万数据优化&迁移
查看>>
MySql中 delimiter 详解
查看>>
MYSQL中 find_in_set() 函数用法详解
查看>>
MySQL中auto_increment有什么作用?(IT枫斗者)
查看>>
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>