需求:
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