1 -- Test script adapted from the one in the Ruby binding.
3 local rrd = require "rrd"
5 local name = "test.rrd"
6 local start = 300 * math.floor(os.time() / 300)
8 io.write('\n-- Creating ', name, '\n')
15 "RRA:AVERAGE:0.5:1:300")
18 for t=start, start+300*300, 300 do
19 local s = string.format('%d:%d:%f', t,
20 math.random(100), math.sin(t/800)*50+50)
22 num_points = num_points + 1
25 io.write('rrd file created with ', num_points, ' points, from ', start,
26 ' to ', start+300*300, '\n')
28 io.write('\n-- Testing rrd.info\n')
29 local info = rrd.info(name)
30 for k,v in pairs(info) do
31 io.write(k, '=', v, '\n')
35 io.write('-- Testing rrd.fetch\n')
36 io.write("fetching data from ", name, ' - interval: ', start, ' to ',
38 local fstart, fend, fstep, fnames, fdata =
39 rrd.fetch(name, "--start", start, "--end", start+300*300+10, "AVERAGE")
40 io.write('got ', #fdata[1], ' data sources with ', #fdata,
41 ' data points each.\n')
43 -- uncomment below to print fetched data
45 io.write('\n-- Printing fetched data\n')
47 for i, n in ipairs(fnames) do
51 for i, v in ipairs(fdata) do
52 local time = fstart + (i-1)*fstep
53 io.write(string.format('%s (%d): ', os.date("%c", time), time))
54 for _, w in ipairs(v) do
55 io.write(string.format('%e ', w))
62 io.write('\n-- Testing rrd.graphv - creates test.png and returns values\n')
65 "--title", "Enjoy Lua RRDTool module!",
66 "--start", start+3600,
67 "--end", "start + 1000 min",
71 "DEF:a=" .. name .. ":a:AVERAGE",
72 "DEF:b=" .. name .. ":b:AVERAGE",
73 "CDEF:line=TIME,2400,%,300,LT,a,UNKN,IF",
75 "AREA:line#0022e9:alpha",
82 io.write('The graph "test.png" was created.\n')
84 -- uncomment below to print graphv returned data
86 io.write('\n-- Printing returned values\n')
87 io.write('print[0]: ', t['print[0]'], '\n')
88 io.write('print[1]: ', t['print[1]'], '\n')
89 for k, v in pairs(t) do
90 if not string.match(k, '^print%[%d+%]') then
91 io.write(k, ': ', v, '\n')
97 io.write('Use your preferred viewer to display the graph.\n\n')