From 611b3724427d24b7a709704e5af17afd0111f24c Mon Sep 17 00:00:00 2001 From: Antoine C Date: Wed, 5 Jun 2024 19:34:33 +0100 Subject: [PATCH] prometheus-node-exporter-lua: remove duplicated nat samples Merge duplicate src/dest samples by suming their value (bytes count) Fixes #24166 Signed-off-by: Antoine C [bump version number] Signed-off-by: Etienne Champetier (cherry picked from commit cd8f67298c588a6e80c5991b6921222eea10a06f) --- utils/prometheus-node-exporter-lua/Makefile | 2 +- .../lua/prometheus-collectors/nat_traffic.lua | 26 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/utils/prometheus-node-exporter-lua/Makefile b/utils/prometheus-node-exporter-lua/Makefile index f40ae7a0d7..74d4d09cd9 100644 --- a/utils/prometheus-node-exporter-lua/Makefile +++ b/utils/prometheus-node-exporter-lua/Makefile @@ -4,7 +4,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=prometheus-node-exporter-lua -PKG_VERSION:=2024.06.03 +PKG_VERSION:=2024.06.15 PKG_RELEASE:=1 PKG_MAINTAINER:=Etienne CHAMPETIER diff --git a/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/nat_traffic.lua b/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/nat_traffic.lua index 0b2da7dc3d..24e4ca1f93 100644 --- a/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/nat_traffic.lua +++ b/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/nat_traffic.lua @@ -1,7 +1,20 @@ local function scrape() -- documetation about nf_conntrack: -- https://www.frozentux.net/iptables-tutorial/chunkyhtml/x1309.html - nat_metric = metric("node_nat_traffic", "gauge" ) + + -- two dimesional table to sum bytes for the pair (src/dest) + local nat = {} + -- default constructor to init unknow pairs + setmetatable(nat, { + __index = function (t, addr) + t[addr] = {} + setmetatable(t[addr], { + __index = function () return 0 end + }) + return t[addr] + end + }) + for e in io.lines("/proc/net/nf_conntrack") do -- output(string.format("%s\n",e )) local fields = space_split(e) @@ -22,9 +35,16 @@ local function scrape() -- local src, dest, bytes = string.match(natstat[i], "src=([^ ]+) dst=([^ ]+) .- bytes=([^ ]+)"); -- local src, dest, bytes = string.match(natstat[i], "src=([^ ]+) dst=([^ ]+) sport=[^ ]+ dport=[^ ]+ packets=[^ ]+ bytes=([^ ]+)") - local labels = { src = src, dest = dest } -- output(string.format("src=|%s| dest=|%s| bytes=|%s|", src, dest, bytes )) - nat_metric(labels, bytes ) + nat[src][dest] = nat[src][dest] + bytes + end + + nat_metric = metric("node_nat_traffic", "gauge" ) + for src, values in next, nat do + for dest, bytes in next, values do + local labels = { src = src, dest = dest } + nat_metric(labels, bytes ) + end end end -- 2.30.2