Browse Source

prometheus-node-exporter-lua: use io.lines(), remove line_split

Signed-off-by: Etienne Champetier <champetier.etienne@gmail.com>
lilik-openwrt-22.03
Etienne Champetier 7 years ago
parent
commit
ed7d60d871
4 changed files with 17 additions and 35 deletions
  1. +1
    -12
      utils/prometheus-node-exporter-lua/files/usr/bin/prometheus-node-exporter-lua
  2. +3
    -5
      utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/meminfo.lua
  3. +2
    -4
      utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/nat_traffic.lua
  4. +11
    -14
      utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/netdev.lua

+ 1
- 12
utils/prometheus-node-exporter-lua/files/usr/bin/prometheus-node-exporter-lua View File

@ -8,27 +8,16 @@
socket = require("socket") socket = require("socket")
-- Allow us to call unpack under both lua5.1 and lua5.2+
local unpack = unpack or table.unpack
-- Parsing -- Parsing
function space_split(s) function space_split(s)
elements = {}
local elements = {}
for element in s:gmatch("%S+") do for element in s:gmatch("%S+") do
table.insert(elements, element) table.insert(elements, element)
end end
return elements return elements
end end
function line_split(s)
elements = {}
for element in s:gmatch("[^\n]+") do
table.insert(elements, element)
end
return elements
end
function get_contents(filename) function get_contents(filename)
local f = io.open(filename, "rb") local f = io.open(filename, "rb")
local contents = "" local contents = ""


+ 3
- 5
utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/meminfo.lua View File

@ -1,12 +1,10 @@
local function scrape() local function scrape()
local meminfo = line_split(get_contents("/proc/meminfo"):gsub("[):]", ""):gsub("[(]", "_"))
for i, mi in ipairs(meminfo) do
local name, size, unit = unpack(space_split(mi))
for line in io.lines("/proc/meminfo") do
local name, size, unit = string.match(line, "([^:]+):%s+(%d+)%s?(k?B?)")
if unit == 'kB' then if unit == 'kB' then
size = size * 1024 size = size * 1024
end end
metric("node_memory_" .. name, "gauge", nil, size)
metric("node_memory_" .. name:gsub("[):]", ""):gsub("[(]", "_"), "gauge", nil, size)
end end
end end


+ 2
- 4
utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/nat_traffic.lua View File

@ -1,15 +1,13 @@
local function scrape() local function scrape()
-- documetation about nf_conntrack: -- documetation about nf_conntrack:
-- https://www.frozentux.net/iptables-tutorial/chunkyhtml/x1309.html -- https://www.frozentux.net/iptables-tutorial/chunkyhtml/x1309.html
local natstat = line_split(get_contents("/proc/net/nf_conntrack"))
nat_metric = metric("node_nat_traffic", "gauge" ) nat_metric = metric("node_nat_traffic", "gauge" )
for i, e in ipairs(natstat) do
for e in io.lines("/proc/net/nf_conntrack") do
-- output(string.format("%s\n",e )) -- output(string.format("%s\n",e ))
local fields = space_split(e) local fields = space_split(e)
local src, dest, bytes; local src, dest, bytes;
bytes = 0; bytes = 0;
for ii, field in ipairs(fields) do
for _, field in ipairs(fields) do
if src == nil and string.match(field, '^src') then if src == nil and string.match(field, '^src') then
src = string.match(field,"src=([^ ]+)"); src = string.match(field,"src=([^ ]+)");
elseif dest == nil and string.match(field, '^dst') then elseif dest == nil and string.match(field, '^dst') then


+ 11
- 14
utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/netdev.lua View File

@ -1,26 +1,23 @@
local function scrape()
local netdevstat = line_split(get_contents("/proc/net/dev"))
local netdevsubstat = {"receive_bytes", "receive_packets", "receive_errs",
local netdevsubstat = {"receive_bytes", "receive_packets", "receive_errs",
"receive_drop", "receive_fifo", "receive_frame", "receive_compressed", "receive_drop", "receive_fifo", "receive_frame", "receive_compressed",
"receive_multicast", "transmit_bytes", "transmit_packets", "receive_multicast", "transmit_bytes", "transmit_packets",
"transmit_errs", "transmit_drop", "transmit_fifo", "transmit_colls", "transmit_errs", "transmit_drop", "transmit_fifo", "transmit_colls",
"transmit_carrier", "transmit_compressed"} "transmit_carrier", "transmit_compressed"}
for i, line in ipairs(netdevstat) do
netdevstat[i] = string.match(netdevstat[i], "%S.*")
end
local pattern = "([^%s:]+):%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)"
local function scrape()
local nds_table = {} local nds_table = {}
local devs = {}
for i, nds in ipairs(netdevstat) do
local dev, stat_s = string.match(netdevstat[i], "([^:]+): (.*)")
if dev then
nds_table[dev] = space_split(stat_s)
table.insert(devs, dev)
for line in io.lines("/proc/net/dev") do
local t = {string.match(line, pattern)}
if #t == 17 then
nds_table[t[1]] = t
end end
end end
for i, ndss in ipairs(netdevsubstat) do for i, ndss in ipairs(netdevsubstat) do
netdev_metric = metric("node_network_" .. ndss, "gauge") netdev_metric = metric("node_network_" .. ndss, "gauge")
for ii, d in ipairs(devs) do
netdev_metric({device=d}, nds_table[d][i])
for dev, nds_dev in pairs(nds_table) do
netdev_metric({device=dev}, nds_dev[i+1])
end end
end end
end end


Loading…
Cancel
Save