golang 自定义exporter - 服务连接数 portConnCount_exporter 导出器

本文介绍了一个使用Go语言编写的Prometheusexporter,通过netstat命令获取6379和3306服务的连接数,并以Gauge的形式实时更新,适用于Linux和Windows系统,同时展示了如何在Grafana中进行查询和可视化。
摘要由CSDN通过智能技术生成

需求:

1、计算当前6379 、3306 服务的连接数
2、可prometheus 语法查询

下面代码可直接使用:
注:
1、windows 与linux的区分 第38行代码
localAddr := fields[1] //windows为fields[1] , linux为fields[3]
2、如需求 增加/修改/删除 端口,可参考第70 71行即可

 70         NewPrometheusGauge(3306)
 71         NewPrometheusGauge(6379)

代码

package main

import (
        "fmt"
        "log"
        "net/http"
        "os/exec"
        "strings"
        "time"

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

func PortConnCounts(port int) float64 {
        // 执行netstat命令
        out, err := exec.Command("netstat", "-an").Output()
        if err != nil {
                fmt.Println("执行netstat命令失败:", err)
                return -1
        }

        // 解析netstat命令输出
        result := string(out)
        lines := strings.Split(result, "\n")
        // fmt.Printf("lines: %v\n", lines)

        var count float64 = 0
        for _, line := range lines {
                // 忽略空行和表头
                if line == "" || strings.Contains(line, "Active Internet connections") || strings.Contains(line, "Proto") {
                        continue
                }

                fields := strings.Fields(line)
                if len(fields) >= 4 {
                        // 获取本地地址和端口
                        localAddr := fields[3] //windows为fields[1]  linux为fields[3]
                        addrParts := strings.Split(localAddr, ":")
                        if len(addrParts) >= 2 {
                                localPort := addrParts[len(addrParts)-1]
                                if localPort == fmt.Sprint(port) {
                                        count++
                                }
                        }
                }
        }
        log.Printf("  port: %v count: %v\n", port, count)
        return count
}

func NewPrometheusGauge(port int) {
        t1 := prometheus.NewGauge(prometheus.GaugeOpts{
                Name: fmt.Sprint("portconnscount", port),
                Help: fmt.Sprint("portconnscount", port, "每10秒执行一次,端口链接数,误差1个左右, 误差在于:::ipv6的显示"),
        })
        // 注册指标
        prometheus.MustRegister(t1)

        // 每秒钟增加指标值
        go func() {
                for {
                        t1.Set(PortConnCounts(port))
                        time.Sleep(time.Second * 10)
                }
        }()
}

func main() {
        NewPrometheusGauge(3306)
        NewPrometheusGauge(6379)

        // 创建一个 Gauge 指标

        // 处理 "/metrics" 路径,暴露指标
        http.Handle("/metrics", promhttp.Handler())
        log.Fatal(http.ListenAndServe(":9101", nil))
}

生成 二进制文件

//get 一下包
go get github.com/prometheus/client_golang/prometheus
go get github.com/prometheus/client_golang/prometheus/promhttp

go build -o portConnCount_exporter main.go

执行

nohup ./portConnCount_exporter &

[root@www netstat.go]# tail -f nohup.out 
2023/08/09 15:56:39   port: 6379 count: 2272
2023/08/09 15:56:39   port: 3306 count: 100
2023/08/09 15:56:49   port: 3306 count: 100
2023/08/09 15:56:49   port: 6379 count: 2272
2023/08/09 15:56:59   port: 6379 count: 2272
2023/08/09 15:56:59   port: 3306 count: 100
2023/08/09 15:57:09   port: 6379 count: 2272
2023/08/09 15:57:09   port: 3306 count: 100
2023/08/09 15:57:20   port: 6379 count: 2272

网页访问
http://ip:9101/metrics
在这里插入图片描述

prometheus语法查询
在这里插入图片描述

Grafana
在这里插入图片描述

在这里插入图片描述

------------end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值