用go编写prometheus自研exporter——CPU及内存的指标
2021/4/8 7:12:44
本文主要是介绍用go编写prometheus自研exporter——CPU及内存的指标,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
需求:利用go语言获取linux主机的cpu利用率及内存使用量和剩余量,并将其制作成exporter
编写go代码及注解如下:
package main import ( "bufio" "io/ioutil" "net/http" "os" "fmt" "strconv" "strings" "time" ) func getCPUSample() (idle, total uint64) { //读取/proc/stat内容 contents, err := ioutil.ReadFile("/proc/stat") if err != nil { return } //读取内容转化为字符串 lines := strings.Split(string(contents), "\n") for _, line := range(lines) { //将字符串以空白字符(\t, \n, \v, \f, \r, ’ ‘, U+0085 (NEL), U+00A0 (NBSP) 。)进行分割多个子串 fields := strings.Fields(line) //如果第一列字符为“cpu” if fields[0] == "cpu" { //统计子串数量 numFields := len(fields) for i := 1; i < numFields; i++ { val, err := strconv.ParseUint(fields[i], 10, 64) if err != nil { fmt.Println("Error: ", i, fields[i], err) } //将是CPU字符的所有子串相加 total += val // tally up all the numbers to get total ticks if i == 4 { // idle is the 5th field in the cpu line //第五列的数据赋值给idle idle = val } } return } } return } func ReadLine(lineNumber int) string{ //读取/proc/meminfo内容 file, _ := os.Open("/proc/meminfo") //按行读取所有内容 fileScanner := bufio.NewScanner(file) lineCount := 1 for fileScanner.Scan(){ if lineCount == lineNumber{ return fileScanner.Text() } lineCount++ } defer file.Close() return "" } func handler(w http.ResponseWriter, r *http.Request) { //定义一个整数型切片 var s []int //读取/proc/meminfo的第二行内容 MemFree := ReadLine(2) //将读取的内容转化为字符串 MemFree_lines := strings.Split(string(MemFree), "\n") //将字符串以空白字符(\t, \n, \v, \f, \r, ’ ‘, U+0085 (NEL), U+00A0 (NBSP) 。)进行分割多个子串 for _, MemFree_line := range (MemFree_lines) { fields := strings.Fields(MemFree_line) //将第二列的内容转化为整数,并将这个整数追加到s切片中 if MemFree_line, err := strconv.Atoi(fields[1]); err == nil { //fmt.Printf("%T, %v", MemFree_line, MemFree_line) s = append(s, MemFree_line) } } Buffers := ReadLine(4) Buffers_lines := strings.Split(string(Buffers), "\n") for _, Buffers_line := range (Buffers_lines) { fields := strings.Fields(Buffers_line) if Buffers_line, err := strconv.Atoi(fields[1]); err == nil { //fmt.Printf("%T, %v", Buffers_line, Buffers_line) s = append(s, Buffers_line) } } Cached := ReadLine(4) Cached_lines := strings.Split(string(Cached), "\n") for _, Cached_line := range (Cached_lines) { fields := strings.Fields(Cached_line) if Cached_line, err := strconv.Atoi(fields[1]); err == nil { //fmt.Printf("%T, %v", Cached_line, Cached_line) s = append(s, Cached_line) } } MemTotal := ReadLine(1) MemTotal_lines := strings.Split(string(MemTotal), "\n") for _, MemTotal_line := range (MemTotal_lines) { fields := strings.Fields(MemTotal_line) if MemTotal_line, err := strconv.Atoi(fields[1]); err == nil { //fmt.Printf("%T, %v", MemTotal_line, MemTotal_line) s = append(s, MemTotal_line) } } idle0, total0 := getCPUSample() time.Sleep(3 * time.Second) idle1, total1 := getCPUSample() idleTicks := float64(idle1 - idle0) totalTicks := float64(total1 - total0) //计算cpu利用率 cpuUsage := 100 * (totalTicks - idleTicks) / totalTicks //计算内存的使用率及剩余量 memoryused := (s[0] + s[1] + s[2]) memoryfreeused:=(s[3]-memoryused) //页面显示内容 fmt.Fprintf(w,"#HELP node_memory_guest_seconds\n node_memory{key=\"used\"}\t%v\n node_memory{key=\"free\"}\t%v\n#node_CPU_guest_seconds\n node_cpu{key=\"usage\"}\t%v\n",memoryused,memoryfreeused,cpuUsage) } func main(){ //调用handler函数 http.HandleFunc("/", handler) //端口为8006 http.ListenAndServe(":8006", nil) }
执行并打开页面(每次加载页面会自动刷新数据值):
[root@localhost ~]# go run exporter_cpu_memory.go [root@localhost ~]# curl 127.0.0.1:8006 #HELP node_memory_guest_seconds node_memory{key="used"} 117864 node_memory{key="free"} 878060 #node_CPU_guest_seconds node_cpu{key="usage"} 0
这篇关于用go编写prometheus自研exporter——CPU及内存的指标的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23MongoDB身份认证机制揭秘!
- 2024-11-20MongoDB教程:从入门到实践详解
- 2024-11-17执行 Google Ads API 查询后返回的是空数组什么原因?-icode9专业技术文章分享
- 2024-11-17google广告数据不同经理账户下的凭证可以获取对方的api数据吗?-icode9专业技术文章分享
- 2024-11-15SendGrid 的 Go 客户端库怎么实现同时向多个邮箱发送邮件?-icode9专业技术文章分享
- 2024-11-15SendGrid 的 Go 客户端库怎么设置header 和 标签tag 呢?-icode9专业技术文章分享
- 2024-11-12Cargo deny安装指路
- 2024-11-02MongoDB项目实战:从入门到初级应用
- 2024-11-01随时随地一键转录,Google Cloud 新模型 Chirp 2 让语音识别更上一层楼
- 2024-10-25Google Cloud动手实验详解:如何在Cloud Run上开发无服务器应用