1 module("luci.statistics.rrdtool", package.seeall)
3 require("luci.statistics.datatree")
4 require("luci.statistics.rrdtool.colors")
5 require("luci.statistics.rrdtool.definitions")
11 Graph = luci.util.class()
13 function Graph.__init__( self, timespan, opts )
17 self.colors = luci.statistics.rrdtool.colors.Instance()
18 self.defs = luci.statistics.rrdtool.definitions.Instance()
19 self.tree = luci.statistics.datatree.Instance()
23 opts.rrasingle = opts.rrasingle or true -- XXX: fixme (uci)
24 opts.host = opts.host or "OpenWrt" -- XXX: fixme (uci)
25 opts.timespan = opts.timespan or 900 -- XXX: fixme (uci)
26 opts.width = opts.width or 400 -- XXX: fixme (uci)
28 -- rrdtool default args
31 "-s", "NOW-" .. opts.timespan,
39 self.i18n.loadc("statistics")
42 function Graph.mktitle( self, plugin, plugin_instance, dtype, dtype_instance )
43 local t = self.opts.host .. "/" .. plugin
44 if type(plugin_instance) == "string" and plugin_instance:len() > 0 then
45 t = t .. "-" .. plugin_instance
48 if type(dtype_instance) == "string" and dtype_instance:len() > 0 then
49 t = t .. "-" .. dtype_instance
54 function Graph.mkrrdpath( self, ... )
55 return string.format( "/tmp/%s.rrd", self:mktitle( ... ) )
58 function Graph.mkpngpath( self, ... )
59 return string.format( "/tmp/rrdimg/%s.png", self:mktitle( ... ) )
62 function Graph._forcelol( self, list )
63 if type(list[1]) ~= "table" then
69 function Graph._rrdtool( self, def, rrd )
72 local dir = def[1]:gsub("/[^/]+$","")
73 luci.fs.mkdir( dir, true )
75 -- construct commandline
76 local cmdline = "rrdtool graph"
78 -- copy default arguments to def stack
79 for i, opt in ipairs(self.args) do
80 table.insert( def, 1 + i, opt )
83 -- construct commandline from def stack
84 for i, opt in ipairs(def) do
85 opt = opt .. "" -- force string
88 opt = opt:gsub( "{file}", rrd )
91 if opt:match("[^%w]") then
92 cmdline = cmdline .. " '" .. opt .. "'"
94 cmdline = cmdline .. " " .. opt
99 local rrdtool = io.popen( cmdline )
103 function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
105 -- generated graph defs
108 -- internal state variables
111 local _stack_neg = { }
112 local _stack_pos = { }
113 local _longest_name = 0
114 local _has_totals = false
116 -- some convenient aliases
117 local _ti = table.insert
118 local _sf = string.format
120 -- local helper: append a string.format() formatted string to given table
121 function _tif( list, fmt, ... )
122 table.insert( list, string.format( fmt, ... ) )
125 -- local helper: create definitions for min, max, avg and create *_nnl (not null) variable from avg
126 function __def(source)
128 local inst = source.sname
129 local rrd = source.rrd
132 if not ds or ds:len() == 0 then ds = "value" end
134 _tif( _args, "DEF:%s_avg=%s:%s:AVERAGE", inst, rrd, ds )
136 if not self.opts.rrasingle then
137 _tif( _args, "DEF:%s_min=%s:%s:MIN", inst, rrd, ds )
138 _tif( _args, "DEF:%s_max=%s:%s:MAX", inst, rrd, ds )
141 _tif( _args, "CDEF:%s_nnl=%s_avg,UN,0,%s_avg,IF", inst, inst, inst )
144 -- local helper: create cdefs depending on source options like flip and overlay
145 function __cdef(source)
149 -- find previous source, choose stack depending on flip state
151 prev = _stack_neg[#_stack_neg]
153 prev = _stack_pos[#_stack_pos]
156 -- is first source in stack or overlay source: source_stk = source_nnl
157 if not prev or source.overlay then
158 -- create cdef statement
159 _tif( _args, "CDEF:%s_stk=%s_nnl", source.sname, source.sname )
161 -- is subsequent source without overlay: source_stk = source_nnl + previous_stk
163 -- create cdef statement
164 _tif( _args, "CDEF:%s_stk=%s_nnl,%s_stk,+", source.sname, source.sname, prev )
167 -- create multiply by minus one cdef if flip is enabled
170 -- create cdef statement: source_stk = source_stk * -1
171 _tif( _args, "CDEF:%s_neg=%s_stk,-1,*", source.sname, source.sname )
173 -- push to negative stack if overlay is disabled
174 if not source.overlay then
175 _ti( _stack_neg, source.sname )
178 -- no flipping, push to positive stack if overlay is disabled
179 elseif not source.overlay then
181 -- push to positive stack
182 _ti( _stack_pos, source.sname )
185 -- calculate total amount of data if requested
188 "CDEF:%s_avg_sample=%s_avg,UN,0,%s_avg,IF,sample_len,*",
189 source.sname, source.sname, source.sname
193 "CDEF:%s_avg_sum=PREV,UN,0,PREV,IF,%s_avg_sample,+",
194 source.sname, source.sname, source.sname
199 -- local helper: create cdefs required for calculating total values
200 function __cdef_totals()
202 _tif( _args, "CDEF:mytime=%s_avg,TIME,TIME,IF", _sources[1].sname )
203 _ti( _args, "CDEF:sample_len_raw=mytime,PREV(mytime),-" )
204 _ti( _args, "CDEF:sample_len=sample_len_raw,UN,0,sample_len_raw,IF" )
208 -- local helper: create line and area statements
209 function __line(source)
216 -- find colors: try source, then opts.colors; fall back to random color
217 if type(source.color) == "string" then
218 line_color = source.color
219 area_color = self.colors:from_string( line_color )
220 elseif type(opts.colors[source.name:gsub("[^%w]","_")]) == "string" then
221 line_color = opts.colors[source.name:gsub("[^%w]","_")]
222 area_color = self.colors:from_string( line_color )
224 area_color = self.colors:random()
225 line_color = self.colors:to_string( area_color )
228 -- derive area background color from line color
229 area_color = self.colors:to_string( self.colors:faded( area_color ) )
231 -- choose source_stk or source_neg variable depending on flip state
239 legend = _sf( "%-" .. _longest_name .. "s", source.title )
241 -- create area if not disabled
242 if not source.noarea then
243 _tif( _args, "AREA:%s_%s#%s", source.sname, var, area_color )
246 -- create line1 statement
247 _tif( _args, "LINE1:%s_%s#%s:%s", source.sname, var, line_color, legend )
250 -- local helper: create gprint statements
251 function __gprint(source)
253 local numfmt = opts.number_format or "%6.1lf"
254 local totfmt = opts.totals_format or "%5.1lf%s"
256 -- don't include MIN if rrasingle is enabled
257 if not self.opts.rrasingle then
258 _tif( _args, "GPRINT:%s_min:MIN:%s Min", source.sname, numfmt )
261 -- always include AVERAGE
262 _tif( _args, "GPRINT:%s_avg:AVERAGE:%s Avg", source.sname, numfmt )
264 -- don't include MAX if rrasingle is enabled
265 if not self.opts.rrasingle then
266 _tif( _args, "GPRINT:%s_max:MAX:%s Max", source.sname, numfmt )
269 -- include total count if requested else include LAST
271 _tif( _args, "GPRINT:%s_avg_sum:LAST:(ca. %s Total)\\l", source.sname, totfmt )
273 _tif( _args, "GPRINT:%s_avg:LAST:%s Last\\l", source.sname, numfmt )
279 -- find all data sources
286 data_types = { dtype }
288 data_types = opts.data.types or { }
291 if not ( dtype or opts.data.types ) then
292 if opts.data.instances then
293 for k, v in pairs(opts.data.instances) do
296 elseif opts.data.sources then
297 for k, v in pairs(opts.data.sources) do
304 -- iterate over data types
305 for i, dtype in ipairs(data_types) do
311 if not opts.per_instance then
312 if type(opts.data.instances) == "table" and type(opts.data.instances[dtype]) == "table" then
313 data_instances = opts.data.instances[dtype]
315 data_instances = self.tree:data_instances( plugin, plugin_instance, dtype )
319 if type(data_instances) ~= "table" or #data_instances == 0 then data_instances = { "" } end
322 -- iterate over data instances
323 for i, dinst in ipairs(data_instances) do
325 -- construct combined data type / instance name
328 if dinst:len() > 0 then
329 dname = dname .. "_" .. dinst
334 local data_sources = { "value" }
336 if type(opts.data.sources) == "table" then
337 if type(opts.data.sources[dname]) == "table" then
338 data_sources = opts.data.sources[dname]
339 elseif type(opts.data.sources[dtype]) == "table" then
340 data_sources = opts.data.sources[dtype]
345 -- iterate over data sources
346 for i, dsource in ipairs(data_sources) do
348 local dsname = dtype .. "_" .. dinst:gsub("[^%w]","_") .. "_" .. dsource
349 local altname = dtype .. "__" .. dsource
351 -- find datasource options
354 if type(opts.data.options) == "table" then
355 if type(opts.data.options[dsname]) == "table" then
356 dopts = opts.data.options[dsname]
357 elseif type(opts.data.options[altname]) == "table" then
358 dopts = opts.data.options[altname]
359 elseif type(opts.data.options[dname]) == "table" then
360 dopts = opts.data.options[dname]
361 elseif type(opts.data.options[dtype]) == "table" then
362 dopts = opts.data.options[dtype]
369 title = dsname, -- XXX: fixme i18n (dopts.title || i18n || dname)
370 rrd = dopts.rrd or self:mkrrdpath( plugin, plugin_instance, dtype, dinst ),
371 color = dopts.color or self.colors:to_string( self.colors:random() ),
372 flip = dopts.flip or false,
373 total = dopts.total or false,
374 overlay = dopts.overlay or false,
375 noarea = dopts.noarea or false,
379 index = #_sources + 1,
380 sname = ( #_sources + 1 ) .. dtype
384 -- find longest name ...
385 if _sources[#_sources].title:len() > _longest_name then
386 _longest_name = _sources[#_sources].title:len()
391 if _sources[#_sources].total then
400 -- construct diagrams
403 -- if per_instance is enabled then find all instances from the first datasource in diagram
404 -- if per_instance is disabled then use an empty pseudo instance and use model provided values
405 local instances = { "" }
407 if opts.per_instance then
408 instances = self.tree:data_instances( plugin, plugin_instance, _sources[1].type )
412 -- iterate over instances
413 for i, instance in ipairs(instances) do
415 -- store title and vlabel
418 _ti( _args, opts.title )
420 _ti( _args, opts.vlabel )
422 -- store additional rrd options
424 for i, o in ipairs(opts.rrdopts) do _ti( _args, o ) end
428 -- create DEF statements for each instance
429 for i, source in ipairs(_sources) do
430 -- fixup properties for per instance mode...
431 if opts.per_instance then
432 source.instance = instance
433 source.rrd = self:mkrrdpath( plugin, plugin_instance, source.type, instance )
439 -- create CDEF required for calculating totals
442 -- create CDEF statements for each instance in reversed order
443 for i, source in ipairs(_sources) do
444 __cdef( _sources[1 + #_sources - i] )
447 -- create LINE1, AREA and GPRINT statements for each instance
448 for i, source in ipairs(_sources) do
453 -- prepend image path to arg stack
454 _ti( _args, 1, self:mkpngpath( plugin, plugin_instance, index .. instance ) )
456 -- push arg stack to definition list
468 function Graph.render( self, plugin, plugin_instance )
470 dtype_instances = dtype_instances or { "" }
473 -- check for a whole graph handler
474 local plugin_def = "luci.statistics.rrdtool.definitions." .. plugin
475 local stat, def = pcall( require, plugin_def )
477 if stat and def and type(def.rrdargs) == "function" then
479 -- temporary image matrix
482 -- get diagram definitions
483 for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance ) ) ) do
487 -- get diagram definition instances
488 local diagrams = self:_generic( opts, plugin, plugin_instance, nil, i )
490 -- render all diagrams
491 for j, def in ipairs( diagrams ) do
494 _images[i][j] = def[1]
501 -- remember images - XXX: fixme (will cause probs with asymmetric data)
502 for y = 1, #_images[1] do
503 for x = 1, #_images do
504 table.insert( pngs, _images[x][y] )
509 -- no graph handler, iterate over data types
510 for i, dtype in ipairs( self.tree:data_types( plugin, plugin_instance ) ) do
512 -- check for data type handler
513 local dtype_def = plugin_def .. "." .. dtype
514 local stat, def = pcall( require, dtype_def )
516 if stat and def and type(def.rrdargs) == "function" then
518 -- temporary image matrix
521 -- get diagram definitions
522 for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance, dtype ) ) ) do
526 -- get diagram definition instances
527 local diagrams = self:_generic( opts, plugin, plugin_instance, dtype, i )
529 -- render all diagrams
530 for j, def in ipairs( diagrams ) do
533 _images[i][j] = def[1]
540 -- remember images - XXX: fixme (will cause probs with asymmetric data)
541 for y = 1, #_images[1] do
542 for x = 1, #_images do
543 table.insert( pngs, _images[x][y] )
548 -- no data type handler, fall back to builtin definition
549 if type(self.defs.definitions[dtype]) == "table" then
551 -- iterate over data type instances
552 for i, inst in ipairs( self.tree:data_instances( plugin, plugin_instance, dtype ) ) do
554 local title = self:mktitle( plugin, plugin_instance, dtype, inst )
555 local png = self:mkpngpath( plugin, plugin_instance, dtype, inst )
556 local rrd = self:mkrrdpath( plugin, plugin_instance, dtype, inst )
557 local args = { png, "-t", title }
559 for i, o in ipairs(self.defs.definitions[dtype]) do
560 table.insert( args, o )
564 table.insert( pngs, png )
567 self:_rrdtool( args, rrd )