在查看 open-falcon 项目源码时,经常会看到其引用了一个类库 https://github.com/toolkits ,而仔细查看该类库的作者为秦晓辉(UlricQin)--- 原Open-Falcon主程,现滴滴云运维负责人 。所以有了这层关系就不难理解open-Falcon引用toolkits里很多代码的原因了吧。toolkits里根据模块类型分了多个子项目,其根据类型又分为LINUX底层性能监控的、邮件发送的、网络的等,这里以nux项为例,说下如下引用。

如下引用其中的部分实现的函数进行处理,代码如下:

// code from www.361way.com
package main
import (
"fmt"
"github.com/toolkits/nux"
)
func main() {
l,_ := nux.LoadAvg()
fmt.Println(nux.LoadAvg())
m,_ := nux.MemInfo()
fmt.Println(l)
fmt.Println(l.Avg1min)
fmt.Println(m)
fmt.Println(nux.NumCpu())
//fmt.Println(nux.CurrentProcStat())
fmt.Println(nux.ListMountPoint())
fmt.Println(nux.BuildDeviceUsage("/dev/mapper/centos-root","/","xfs"))
}
执行结果如下:

其代码写的比较清晰简洁,可以作为参考使用下。不过其对部分指标处理的结果可能和我们所需的结果还是有一些出入,比如,我们平时需要查看的CPU使用率,并不会取各各指标占用的CPU时间,而是直接像top查看到的结果一样,只看idel、us等占用的CPU百分比是多少。正因为如些,所以open-falcon项目在此基础上又进行了二次封装,其地址为:https://github.com/open-falcon/falcon-plus/blob/master/modules/agent/funcs/cpustat.go 这里只取其中一个指标的获取方式的代码,如下:

func CpuIdle() float64 {
psLock.RLock()
defer psLock.RUnlock()
dt := deltaTotal()
if dt == 0 {
return 0.0
}
invQuotient := 100.00 / float64(dt)
return float64(procStatHistory[0].Cpu.Idle-procStatHistory[1].Cpu.Idle) * invQuotient
}

原文来自:http://www.361way.com/golang-toolkits/5904.html

本文地址:https://www.linuxprobe.com/golang-toolkits-userd.html编辑:问题终结者,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

Linux系统大全:https://www.linuxdown.com/

红帽认证RHCE考试心得:https://www.rhce.net/