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

需求:

1、计算当前6379 、3306 服务的连接数

2、可prometheus 语法查询

下面代码可直接使用:

注:

1、windows 与linux的区分 第38行代码
localAddr := fields[1] //windows为fields[1] , linux为fields[3]

2、如需求 增加/修改/删除 端口,可参考第70 71行即可

cpp 复制代码
 70         NewPrometheusGauge(3306)
 71         NewPrometheusGauge(6379)

代码

cpp 复制代码
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))
}

生成 二进制文件

cpp 复制代码
//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

执行

cpp 复制代码
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

相关推荐
茉莉玫瑰花茶3 小时前
GO [ 标准库 ]
开发语言·后端·golang
lbb 小魔仙6 小时前
Redis运行状态总靠手动查?我用redis_exporter+Prometheus把指标、告警和远程抓取全接起来了
redis·bootstrap·prometheus
程序员-Benothing8 小时前
Linux系统监控工具全家桶:sar、dstat、glances、Prometheus
linux·运维·prometheus
wzq11_66612 小时前
Prometheus监控——云原生与非云原生监控
云原生·prometheus
东风微鸣1 天前
把 Tailscale 接进 Prometheus 监控
java·数据库·prometheus
Kendra9191 天前
运维监控平台(Prometheus + Grafana)部署与维护
运维·grafana·prometheus
紫洋葱hh1 天前
Go 入门基础:类型、变量与运算符
开发语言·后端·ai·golang
codeejun1 天前
每日一Go·MySQL-7、MySQL 核心原理全景图谱
开发语言·mysql·golang
Dovis(誓平步青云)1 天前
一年1956亿行:智能电表场景下的时序数据存储实践
运维·前端·自动化·音视频·prometheus
acd120091 天前
Go 里 err 被不经意覆盖的几个现场
开发语言·后端·golang