golang toolkits包的使用方法介紹

在查看 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"))
}
執行結果以下:

golang toolkits包的使用方法介紹golang toolkits包的使用方法介紹

其代碼寫的比較清晰簡潔,能夠做爲參考使用下。不過其對部分指標處理的結果可能和咱們所需的結果仍是有一些出入,好比,咱們平時須要查看的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
}
相關文章
相關標籤/搜索