the rrdlua.pod is in the doc directory
[rrdtool.git] / bindings / lua / test.lua
1 -- Test script adapted from the one in the Ruby binding.
2
3 local rrd = require "rrd"
4
5 local name = "test.rrd"
6 local start = 300 * math.floor(os.time() / 300)
7
8 io.write('\n-- Creating ', name, '\n')
9 rrd.create(
10     name,
11     "--start", start-1,
12     "--step", "300",
13     "DS:a:GAUGE:600:U:U",
14     "DS:b:GAUGE:600:U:U",
15     "RRA:AVERAGE:0.5:1:300")
16
17 local num_points = 0
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)
21   rrd.update(name, s)
22   num_points = num_points + 1
23 end
24
25 io.write('rrd file created with ', num_points, ' points, from ', start,
26          ' to ', start+300*300, '\n')
27
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')
32 end
33 io.write('\n')
34
35 io.write('-- Testing rrd.fetch\n') 
36 io.write("fetching data from ", name, ' - interval: ', start, ' to ',
37          start+300*300, '\n') 
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')
42
43 -- uncomment below to print fetched data
44 ---[[
45 io.write('\n-- Printing fetched data\n') 
46 io.write('            ')
47 for i, n in ipairs(fnames) do
48   io.write(n, '            ')
49 end
50 io.write('\n')
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))
56   end
57   io.write('\n')
58 end
59 io.write('\n')
60 --]]
61
62 io.write('\n-- Testing rrd.graphv - creates test.png and returns values\n') 
63 local t = rrd.graphv(
64    "test.png",
65    "--title", "Enjoy Lua RRDTool module!",
66    "--start", start+3600,
67    "--end", "start + 1000 min",
68    "--interlace",
69    "--imgformat", "PNG",
70    "--width=450",
71    "DEF:a=" .. name .. ":a:AVERAGE",
72    "DEF:b=" .. name .. ":b:AVERAGE",
73    "CDEF:line=TIME,2400,%,300,LT,a,UNKN,IF",
74    "AREA:b#00b6e4:beta",
75    "AREA:line#0022e9:alpha",
76    "LINE3:line#ff0000",
77    "VDEF:va=a,AVERAGE",
78    "VDEF:vb=b,AVERAGE",
79    "PRINT:va:%5.2lf",
80    "PRINT:vb:%5.2lf")
81
82 io.write('The graph "test.png" was created.\n')
83
84 -- uncomment below to print graphv returned data
85 --[[
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')
92   end
93 end
94 io.write('\n')
95 --]]
96
97 io.write('Use your preferred viewer to display the graph.\n\n')
98