在查看 open-falcon 项目源码时,常常会看到其引用了一个类库 https://github.com/toolkits ,而仔细查看该类库的做者为秦晓辉(UlricQin)--- 原Open-Falcon主程,现滴滴云运维负责人 。因此有了这层关系就不难理解open-Falcon引用toolkits里不少代码的缘由了吧。toolkits里根据模块类型分了多个子项目,其根据类型又分为LINUX底层性能监控的、邮件发送的、网络的等,这里以nux项为例,说下以下引用。linux
以下引用其中的部分实现的函数进行处理,代码以下:git
// 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 这里只取其中一个指标的获取方式的代码,以下:github
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 }