当前位置: 首页 > news >正文

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行即可

 70         NewPrometheusGauge(3306)71         NewPrometheusGauge(6379)

代码

package mainimport ("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 = 0for _, 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/promhttpgo 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

http://www.lryc.cn/news/116585.html

相关文章:

  • MoveTowards详解
  • Redis学习笔记Day01-Redis入门
  • C++ Lambda表达式的完整介绍
  • 【等保测评】云计算Linux服务器(一)
  • [vue-element-admin]下载与安装
  • OPENCV C++(九)鼠标响应+dft+idft
  • python编程求出介于这两个数 之间的所有质数并打印输出。显示格式为“*数是质数
  • 基于Selenium模块实现无界面模式 执行JS脚本
  • 【LangChain学习】基于PDF文档构建问答知识库(二)创建项目
  • 【Kubernetes】Kubernetes之kubectl详解
  • 【torch.nn.PixelShuffle】和 【torch.nn.UnpixelShuffle】
  • Rocky9 KVM网桥的配置
  • 爬虫013_函数的定义_调用_参数_返回值_局部变量_全局变量---python工作笔记032
  • 将.doc文档的默认打开方式从WPS修改为word office打开方式的具体方法(以win 10 操作系统为例)
  • 如何搭建个人的GPT网页服务
  • [QCM6125][Android13] 默认关闭SELinux权限
  • 【jvm】jvm发展历程
  • Dubbo3.0 Demo
  • 源码分析——ConcurrentHashMap源码+底层数据结构分析
  • R语言中的函数25:paste,paste0
  • (八)穿越多媒体奇境:探索Streamlit的图像、音频与视频魔法
  • CAD练习——绘制房子平面图
  • spring 面试题
  • Springboot项目集成Durid数据源和P6Spy以及dbType not support问题
  • 安卓如何卸载应用
  • 【云原生|Kubernetes】14-DaemonSet资源控制器详解
  • 基于 Guava Retry 在Spring封装一个重试功能
  • 适用HarmonyOS 3.1版本及以上的应用及服务开发工具 DevEco Studio 3.1.1 Release 安装
  • [信号与系统系列] 正弦振幅调制之差拍信号
  • vb+SQL航空公司管理系统设计与实现