From ed7d60d8710c63eeb4c6d094eb6038c3dc7d86c8 Mon Sep 17 00:00:00 2001 From: Etienne Champetier Date: Fri, 8 Dec 2017 19:03:37 -0800 Subject: [PATCH] prometheus-node-exporter-lua: use io.lines(), remove line_split Signed-off-by: Etienne Champetier --- .../usr/bin/prometheus-node-exporter-lua | 13 +--------- .../lib/lua/prometheus-collectors/meminfo.lua | 8 +++--- .../lua/prometheus-collectors/nat_traffic.lua | 6 ++--- .../lib/lua/prometheus-collectors/netdev.lua | 25 ++++++++----------- 4 files changed, 17 insertions(+), 35 deletions(-) diff --git a/utils/prometheus-node-exporter-lua/files/usr/bin/prometheus-node-exporter-lua b/utils/prometheus-node-exporter-lua/files/usr/bin/prometheus-node-exporter-lua index 4ca77814d..62e402280 100755 --- a/utils/prometheus-node-exporter-lua/files/usr/bin/prometheus-node-exporter-lua +++ b/utils/prometheus-node-exporter-lua/files/usr/bin/prometheus-node-exporter-lua @@ -8,27 +8,16 @@ socket = require("socket") --- Allow us to call unpack under both lua5.1 and lua5.2+ -local unpack = unpack or table.unpack - -- Parsing function space_split(s) - elements = {} + local elements = {} for element in s:gmatch("%S+") do table.insert(elements, element) end return elements 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) local f = io.open(filename, "rb") local contents = "" diff --git a/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/meminfo.lua b/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/meminfo.lua index a0021c4ad..01f262b47 100644 --- a/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/meminfo.lua +++ b/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/meminfo.lua @@ -1,12 +1,10 @@ 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 size = size * 1024 end - metric("node_memory_" .. name, "gauge", nil, size) + metric("node_memory_" .. name:gsub("[):]", ""):gsub("[(]", "_"), "gauge", nil, size) end end 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 4173a0c5a..0b2da7dc3 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,15 +1,13 @@ local function scrape() -- documetation about nf_conntrack: -- 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" ) - for i, e in ipairs(natstat) do + for e in io.lines("/proc/net/nf_conntrack") do -- output(string.format("%s\n",e )) local fields = space_split(e) local src, dest, bytes; bytes = 0; - for ii, field in ipairs(fields) do + for _, field in ipairs(fields) do if src == nil and string.match(field, '^src') then src = string.match(field,"src=([^ ]+)"); elseif dest == nil and string.match(field, '^dst') then diff --git a/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/netdev.lua b/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/netdev.lua index c49d4038a..9127e12b4 100644 --- a/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/netdev.lua +++ b/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/netdev.lua @@ -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_multicast", "transmit_bytes", "transmit_packets", "transmit_errs", "transmit_drop", "transmit_fifo", "transmit_colls", "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 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 for i, ndss in ipairs(netdevsubstat) do 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