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