prometheus-node-exporter-lua: use io.lines(), remove line_split
authorEtienne Champetier <champetier.etienne@gmail.com>
Sat, 9 Dec 2017 03:03:37 +0000 (19:03 -0800)
committerEtienne Champetier <champetier.etienne@gmail.com>
Sat, 9 Dec 2017 05:22:16 +0000 (21:22 -0800)
Signed-off-by: Etienne Champetier <champetier.etienne@gmail.com>
utils/prometheus-node-exporter-lua/files/usr/bin/prometheus-node-exporter-lua
utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/meminfo.lua
utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/nat_traffic.lua
utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/netdev.lua

index 4ca77814d6687a883eefbe17e6e6e7a9038afe13..62e402280f89a40514b625bdddf6104f78745c9e 100755 (executable)
@@ -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 = ""
index a0021c4ad421dd29f24dfd1f703b15cda75a7d19..01f262b47ad6cc809b5efe188118ec0d558124bd 100644 (file)
@@ -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
 
index 4173a0c5a18a97911c2632f522c7e9dba0f71300..0b2da7dc3d49bdfbc8670ec209d19a204d343d13 100644 (file)
@@ -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
index c49d4038aae09f608900cf2b301af2edf0699a3a..9127e12b481e6a5194fcbcd85287a82acba7ae44 100644 (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_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