Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use toolkits package in golang

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use the toolkits package in golang, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

When looking at the source code of the open-falcon project, you will often see that it refers to a class library https://github.com/toolkits, and the author of this class library is Qin Xiaohui (UlricQin)-the original Open-Falcon main program, now in charge of Didi Cloud operation and maintenance. So with this relationship, it's not hard to understand why open-Falcon references a lot of code in toolkits. Toolkits is divided into several sub-projects according to the type of module, and according to the type, it is divided into LINUX underlying performance monitoring, e-mail, network, and so on. Here, taking the nux item as an example, the following references are given.

Refer to some of the implemented functions as follows, and the code is as follows:

/ / code from www.361way.compackage mainimport ("fmt"github.com/toolkits/nux") func main () {nux.LoadAvg _: = 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"))} the execution result is as follows:

Introduction to the use of golang toolkits package introduction to the use of golang toolkits package

The code is written clearly and succinctly and can be used as a reference. However, the results of the processing of some metrics may still be different from those we need. For example, the CPU utilization we usually need to check does not take the CPU time occupied by each indicator, but directly looks at the percentage of CPU occupied by idel, us, and so on, just like the results found by top. Because of this, the open-falcon project has been re-encapsulated on this basis, and its address is: https://github.com/open-falcon/falcon-plus/blob/master/modules/agent/funcs/cpustat.go. Here is the code for how to obtain only one of the metrics, as follows:

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] .CP u.Idle) * invQuotient} Thank you for reading this article carefully. I hope the article "how to use toolkits package in golang" shared by the editor will be helpful to you. At the same time, I hope you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report