Nlbwmon Prometheus Lua File, looking for help

Hi, I'm trying to get nlbwmon data into grafana.
I need help creating a lua prometheus file for nlbwmon. Here is what I have so far:

I'm saving the nlbwmon output to a file, then I want prometheus to scrape it but I'm having no luck.

nlbw -c csv -g ip,mac -o ip | tr -d '"' | tail -n +2 > /tmp/nlbwmon.out

output looks like this:

11:44:54:00:00:00       10.0.0.1        5039    0       0       15227038        232789
22:22:33:00:00:00       10.0.0.2        151194  298574558       1794766 187757841       1951818

Here is my lua file.

local nlbwstat = {
    "mac",
    "ip",
    "conns",
    "rx_bytes",
    "rx_pkts",
    "tx_bytes",
    "tx_pkts"
}

local pattern = "(%w+:%w+:%w+:%w+:%w+:%w+)%s+(%d+.%d+.%d+.%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)"

local function scrape()
  local nlbw_table = {}
  for line in io.lines("/tmp/nlbwmon.out") do
    local t = {string.match(line, pattern)}
    if #t == 7 then
      nlbw_table[t[1]] = t
    end
  end
  for i, nlbw in ipairs(nlbwstat) do
    nlbw_metric = metric("node_network_" .. nlbw, "counter")
    for mac, nlbw_table_entry in pairs(nlbw_table) do
      nlbw_metric:set(nlbw_table_entry[i], {mac_address=mac})
    end
  end
end

-- Add some error handling to the io.lines call
local function read_file()
  local file = io.open("/tmp/nlbwmon.out", "r")
  if not file then
    return nil, "Error opening file"
  end
  local contents = file:read("*all")
  file:close()
  return contents
end

return { scrape = scrape, read_file = read_file }

Prometheus output from http://10.0.0.2:9100/metrics

# TYPE node_network_mac counter
node_scrape_collector_duration_seconds{collector="nlbw_stat"} 0.00056004524230957
node_scrape_collector_success{collector="nlbw_stat"} 0

ChatGPT was able to review and help. I created this and it kind of does what I need.

local nlbwstat = {
    "mac",
    "ip",
    "conns",
    "rx_bytes",
    "rx_pkts",
    "tx_bytes",
    "tx_pkts"
}

local pattern = "(%w+:%w+:%w+:%w+:%w+:%w+)%s+(%d+.%d+.%d+.%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)"

local function scrape()
  local nlbw_table = {}
  for line in io.lines("/tmp/nlbwmon.out") do
    local t = {string.match(line, pattern)}
    if #t == 7 then
      nlbw_table[t[1]] = t
    end
  end
  
  -- create a new metric
  nlbw_metric = metric("nlbwmon_stats", "counter")
  
  -- iterate through the nlbw_table and add the values to the metric
  for mac, nlbw in pairs(nlbw_table) do
    nlbw_metric({
      mac = nlbw[1],
      ip = nlbw[2],
      conns = nlbw[3],
      rx_bytes = nlbw[4],
      rx_pkts = nlbw[5],
      tx_bytes = nlbw[6],
      tx_pkts = nlbw[7]
    }, nlbw[7])
  end
end

return { scrape = scrape }

# TYPE nlbwmon_stats counter
nlbwmon_stats{mac="dd:cc:bb:aa:22:11",tx_bytes="1350",rx_bytes="0",conns="3",rx_pkts="0",tx_pkts="18",ip="10.0.0.224"} 18
nlbwmon_stats{mac="kk:77:dd:55:aa:55",tx_bytes="8569424",rx_bytes="0",conns="1793",rx_pkts="0",tx_pkts="19770",ip="10.0.0.245"} 19770
1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.