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

Signed-off-by: Etienne Champetier <champetier.etienne@gmail.com>
This commit is contained in:
Etienne Champetier 2017-12-08 19:03:37 -08:00
parent 84023027a2
commit ed7d60d871
4 changed files with 17 additions and 35 deletions

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 = ""

View file

@ -1,12 +1,10 @@
local function scrape() local function scrape()
local meminfo = line_split(get_contents("/proc/meminfo"):gsub("[):]", ""):gsub("[(]", "_")) for line in io.lines("/proc/meminfo") do
local name, size, unit = string.match(line, "([^:]+):%s+(%d+)%s?(k?B?)")
for i, mi in ipairs(meminfo) do
local name, size, unit = unpack(space_split(mi))
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

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

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 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+)"
netdevstat[i] = string.match(netdevstat[i], "%S.*")
end local function scrape()
local nds_table = {} local nds_table = {}
local devs = {} for line in io.lines("/proc/net/dev") do
for i, nds in ipairs(netdevstat) do local t = {string.match(line, pattern)}
local dev, stat_s = string.match(netdevstat[i], "([^:]+): (.*)") if #t == 17 then
if dev then nds_table[t[1]] = t
nds_table[dev] = space_split(stat_s)
table.insert(devs, dev)
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 for dev, nds_dev in pairs(nds_table) do
netdev_metric({device=d}, nds_table[d][i]) netdev_metric({device=dev}, nds_dev[i+1])
end end
end end
end end