I finally finished the first version of the patch (attached) -- Fidelis Assis fideli...
[rrdtool.git] / bindings / lua / test.lua
diff --git a/bindings/lua/test.lua b/bindings/lua/test.lua
new file mode 100644 (file)
index 0000000..541330c
--- /dev/null
@@ -0,0 +1,98 @@
+-- Test script adapted from the one in the Ruby binding.
+
+local rrd = require "rrd"
+
+local name = "test.rrd"
+local start = 300 * math.floor(os.time() / 300)
+
+io.write('\n-- Creating ', name, '\n')
+rrd.create(
+    name,
+    "--start", start-1,
+    "--step", "300",
+    "DS:a:GAUGE:600:U:U",
+    "DS:b:GAUGE:600:U:U",
+    "RRA:AVERAGE:0.5:1:300")
+
+local num_points = 0
+for t=start, start+300*300, 300 do
+  local s = string.format('%d:%d:%f', t,
+                          math.random(100), math.sin(t/800)*50+50)
+  rrd.update(name, s)
+  num_points = num_points + 1
+end
+
+io.write('rrd file created with ', num_points, ' points, from ', start,
+         ' to ', start+300*300, '\n')
+
+io.write('\n-- Testing rrd.info\n')
+local info = rrd.info(name)
+for k,v in pairs(info) do
+  io.write(k, '=', v, '\n')
+end
+io.write('\n')
+
+io.write('-- Testing rrd.fetch\n') 
+io.write("fetching data from ", name, ' - interval: ', start, ' to ',
+         start+300*300, '\n') 
+local fstart, fend, fstep, fnames, fdata =
+  rrd.fetch(name, "--start", start, "--end", start+300*300+10, "AVERAGE")
+io.write('got ', #fdata[1], ' data sources with ', #fdata,
+         ' data points each.\n')
+
+-- uncomment below to print fetched data
+---[[
+io.write('\n-- Printing fetched data\n') 
+io.write('            ')
+for i, n in ipairs(fnames) do
+  io.write(n, '            ')
+end
+io.write('\n')
+for i, v in ipairs(fdata) do
+  local time = fstart + (i-1)*fstep
+  io.write(string.format('%s (%d): ', os.date("%c", time), time))
+  for _, w in ipairs(v) do
+    io.write(string.format('%e ', w))
+  end
+  io.write('\n')
+end
+io.write('\n')
+--]]
+
+io.write('\n-- Testing rrd.graphv - creates test.png and returns values\n') 
+local t = rrd.graphv(
+   "test.png",
+   "--title", "Enjoy Lua RRDTool module!",
+   "--start", start+3600,
+   "--end", "start + 1000 min",
+   "--interlace",
+   "--imgformat", "PNG",
+   "--width=450",
+   "DEF:a=" .. name .. ":a:AVERAGE",
+   "DEF:b=" .. name .. ":b:AVERAGE",
+   "CDEF:line=TIME,2400,%,300,LT,a,UNKN,IF",
+   "AREA:b#00b6e4:beta",
+   "AREA:line#0022e9:alpha",
+   "LINE3:line#ff0000",
+   "VDEF:va=a,AVERAGE",
+   "VDEF:vb=b,AVERAGE",
+   "PRINT:va:%5.2lf",
+   "PRINT:vb:%5.2lf")
+
+io.write('The graph "test.png" was created.\n')
+
+-- uncomment below to print graphv returned data
+--[[
+io.write('\n-- Printing returned values\n') 
+io.write('print[0]: ', t['print[0]'], '\n')
+io.write('print[1]: ', t['print[1]'], '\n')
+for k, v in pairs(t) do
+  if not string.match(k, '^print%[%d+%]') then
+    io.write(k, ': ', v, '\n')
+  end
+end
+io.write('\n')
+--]]
+
+io.write('Use your preferred viewer to display the graph.\n\n')
+