[golang]-自定义exporter获取pid的read_bytes

导语:通过golang自定义exporter 来获取pid的io情况

通过获取/proc/*/io这个文件的信息

代码

package main

import (
	"bufio"
	"flag"
	//"fmt"
	"io"
	"os"
	"regexp"
	"strconv"
	"strings"

	//      "fmt"
	"log"
	"net/http"
	//"time"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

// 自定义端口
var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests")

var (
	jobsInQueue = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Name: "pid_io_info",
		Help: "Current number of jobs in the queue",
	}, []string{"item", "pid"})
)

type PidStat struct {
	Pidnum        string  `json:"pidnum"`
	Pidrchar      float64 `json:"pidrchar"`
	Pidreadbytes  float64 `json:"pidreadbytes"`
	Pidwritebytes float64 `json:"pidwritebytes"`
}

func init() {
	prometheus.MustRegister(jobsInQueue)
}

func main() {
	flag.Parse()
	//jobsInQueue.WithLabelValues("testjob", "testtype").Set(999)
	GetPidInfo()

	http.Handle("/metrics", promhttp.Handler())
	log.Fatal(http.ListenAndServe(*addr, nil))
}

// 判断所给路径是否为文件夹
func IsPidDir(path string) bool {
	s, err := os.Stat(path)
	if err != nil {
		return false
	}
	return s.IsDir()
}

func GetConfigFromFile(path string) map[string]string {
	config := make(map[string]string)

	f, err := os.Open(path)
	defer f.Close()
	if err != nil {
		panic(err)
	}

	r := bufio.NewReader(f)
	for {
		b, _, err := r.ReadLine()
		if err != nil {
			if err == io.EOF {
				break
			}
			panic(err)
		}
		s := strings.TrimSpace(string(b))
		index := strings.Index(s, ":")
		if index < 0 {
			continue
		}
		key := strings.TrimSpace(s[:index])
		if len(key) == 0 {
			continue
		}
		value := strings.TrimSpace(s[index+1:])
		if len(value) == 0 {
			continue
		}
		config[key] = value
	}
	return config
}

func GetPidInfo() {
	path := "/proc"

	f, err := os.Open(path)
	if err != nil {
		log.Fatal(err)
	}
	files, err := f.Readdir(-1)
	f.Close()
	if err != nil {
		log.Fatal(err)
	}

	for _, file := range files {
		if regexp.MustCompile(`^[0-9]*$`).MatchString(file.Name()) && IsPidDir(path+"/"+file.Name()) {
			//fmt.Println("linux pid dir", file.Name())
			config := GetConfigFromFile(path + "/" + file.Name() + "/io")
			rchar := config["rchar"]
			read_bytes := config["read_bytes"]
			write_bytes := config["write_bytes"]
			//cache_hits := strconv.Atoi(read_bytes) / strconv.Atoi(rchar)
			p := PidStat{}
			p.Pidrchar, _ = strconv.ParseFloat(rchar, 64)
			p.Pidreadbytes, _ = strconv.ParseFloat(read_bytes, 64)
			p.Pidwritebytes, _ = strconv.ParseFloat(write_bytes, 64)
			p.Pidnum = file.Name()

			//fmt.Println("pid: ", file.Name(), "rchar=", rchar, " read_bytes=", read_bytes, " write_bytes=", write_bytes)
			jobsInQueue.WithLabelValues("write_bytes", p.Pidnum).Set(p.Pidwritebytes)

			//a ,_:= strconv.Atoi(read_bytes)
			//b ,_:= strconv.Atoi(rchar)
			//if rchar == "0" || read_bytes == "0" {
			//	fmt.Println("pid: ", file.Name(), "no rchar or read_bytes, cache hits=", "0")
			//}else  {
			//	cache_hit, _ := strconv.ParseFloat(fmt.Sprintf("%.5f", float64(a)/float64(b)), 64) // 保留5位小数
			//	fmt.Println("pid: ", file.Name(), "cache_hit", cache_hit)
			//}

		}
		//fmt.Println(file.Name())
	}
}

rchar比read_bytes小。有资料说是read_bytes有预读,问了大佬如果文件都是只读一次的话,那么理论上rchar应该比read_bytes小,因为从磁盘里读的量一定要>=应用读取的量。

获取/proc/*/io 里的数据 参考读取配置文件

https://blog.csdn.net/inthat/article/details/119772913

除法结果取小数不要取整 不取小数算出来很多是0

https://blog.csdn.net/HYZX_9987/article/details/108104634

编写exporter

https://blog.csdn.net/u012140251/article/details/120201215

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爷来辣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值