1 module("luci.statistics.rrdtool", package.seeall)
3 require("luci.statistics.datatree")
4 require("luci.statistics.rrdtool.colors")
5 require("luci.statistics.rrdtool.definitions")
6 require("luci.statistics.i18n")
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()
20 self.i18n = luci.statistics.i18n.Instance( self )
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 function Graph._mkpath( self, plugin, plugin_instance, dtype, dtype_instance )
40 local t = self.opts.host .. "/" .. plugin
41 if type(plugin_instance) == "string" and plugin_instance:len() > 0 then
42 t = t .. "-" .. plugin_instance
45 if type(dtype_instance) == "string" and dtype_instance:len() > 0 then
46 t = t .. "-" .. dtype_instance
51 function Graph.mkrrdpath( self, ... )
52 return string.format( "/tmp/%s.rrd", self:_mkpath( ... ) )
55 function Graph.mkpngpath( self, ... )
56 return string.format( "/tmp/rrdimg/%s.png", self:_mkpath( ... ) )
59 function Graph.mktitle( self, plugin, plugin_instance, dtype, dtype_instance )
61 -- try various strings to retrieve a diagram title from the langfile
65 function Graph._forcelol( self, list )
66 if type(list[1]) ~= "table" then
72 function Graph._rrdtool( self, def, rrd )
75 local dir = def[1]:gsub("/[^/]+$","")
76 luci.fs.mkdir( dir, true )
78 -- construct commandline
79 local cmdline = "rrdtool graph"
81 -- copy default arguments to def stack
82 for i, opt in ipairs(self.args) do
83 table.insert( def, 1 + i, opt )
86 -- construct commandline from def stack
87 for i, opt in ipairs(def) do
88 opt = opt .. "" -- force string
91 opt = opt:gsub( "{file}", rrd )
94 if opt:match("[^%w]") then
95 cmdline = cmdline .. " '" .. opt .. "'"
97 cmdline = cmdline .. " " .. opt
102 local rrdtool = io.popen( cmdline )
106 function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
108 -- generated graph defs
111 -- internal state variables
114 local _stack_neg = { }
115 local _stack_pos = { }
116 local _longest_name = 0
117 local _has_totals = false
119 -- some convenient aliases
120 local _ti = table.insert
121 local _sf = string.format
123 -- local helper: append a string.format() formatted string to given table
124 function _tif( list, fmt, ... )
125 table.insert( list, string.format( fmt, ... ) )
128 -- local helper: create definitions for min, max, avg and create *_nnl (not null) variable from avg
129 function __def(source)
131 local inst = source.sname
132 local rrd = source.rrd
135 if not ds or ds:len() == 0 then ds = "value" end
137 _tif( _args, "DEF:%s_avg=%s:%s:AVERAGE", inst, rrd, ds )
139 if not self.opts.rrasingle then
140 _tif( _args, "DEF:%s_min=%s:%s:MIN", inst, rrd, ds )
141 _tif( _args, "DEF:%s_max=%s:%s:MAX", inst, rrd, ds )
144 _tif( _args, "CDEF:%s_nnl=%s_avg,UN,0,%s_avg,IF", inst, inst, inst )
147 -- local helper: create cdefs depending on source options like flip and overlay
148 function __cdef(source)
152 -- find previous source, choose stack depending on flip state
154 prev = _stack_neg[#_stack_neg]
156 prev = _stack_pos[#_stack_pos]
159 -- is first source in stack or overlay source: source_stk = source_nnl
160 if not prev or source.overlay then
161 -- create cdef statement
162 _tif( _args, "CDEF:%s_stk=%s_nnl", source.sname, source.sname )
164 -- is subsequent source without overlay: source_stk = source_nnl + previous_stk
166 -- create cdef statement
167 _tif( _args, "CDEF:%s_stk=%s_nnl,%s_stk,+", source.sname, source.sname, prev )
170 -- create multiply by minus one cdef if flip is enabled
173 -- create cdef statement: source_stk = source_stk * -1
174 _tif( _args, "CDEF:%s_neg=%s_stk,-1,*", source.sname, source.sname )
176 -- push to negative stack if overlay is disabled
177 if not source.overlay then
178 _ti( _stack_neg, source.sname )
181 -- no flipping, push to positive stack if overlay is disabled
182 elseif not source.overlay then
184 -- push to positive stack
185 _ti( _stack_pos, source.sname )
188 -- calculate total amount of data if requested
191 "CDEF:%s_avg_sample=%s_avg,UN,0,%s_avg,IF,sample_len,*",
192 source.sname, source.sname, source.sname
196 "CDEF:%s_avg_sum=PREV,UN,0,PREV,IF,%s_avg_sample,+",
197 source.sname, source.sname, source.sname
202 -- local helper: create cdefs required for calculating total values
203 function __cdef_totals()
205 _tif( _args, "CDEF:mytime=%s_avg,TIME,TIME,IF", _sources[1].sname )
206 _ti( _args, "CDEF:sample_len_raw=mytime,PREV(mytime),-" )
207 _ti( _args, "CDEF:sample_len=sample_len_raw,UN,0,sample_len_raw,IF" )
211 -- local helper: create line and area statements
212 function __line(source)
219 -- find colors: try source, then opts.colors; fall back to random color
220 if type(source.color) == "string" then
221 line_color = source.color
222 area_color = self.colors:from_string( line_color )
223 elseif type(opts.colors[source.name:gsub("[^%w]","_")]) == "string" then
224 line_color = opts.colors[source.name:gsub("[^%w]","_")]
225 area_color = self.colors:from_string( line_color )
227 area_color = self.colors:random()
228 line_color = self.colors:to_string( area_color )
231 -- derive area background color from line color
232 area_color = self.colors:to_string( self.colors:faded( area_color ) )
234 -- choose source_stk or source_neg variable depending on flip state
242 legend = _sf( "%-" .. _longest_name .. "s", source.title )
244 -- create area if not disabled
245 if not source.noarea then
246 _tif( _args, "AREA:%s_%s#%s", source.sname, var, area_color )
249 -- create line1 statement
250 _tif( _args, "LINE1:%s_%s#%s:%s", source.sname, var, line_color, legend )
253 -- local helper: create gprint statements
254 function __gprint(source)
256 local numfmt = opts.number_format or "%6.1lf"
257 local totfmt = opts.totals_format or "%5.1lf%s"
259 -- don't include MIN if rrasingle is enabled
260 if not self.opts.rrasingle then
261 _tif( _args, "GPRINT:%s_min:MIN:%s Min", source.sname, numfmt )
264 -- always include AVERAGE
265 _tif( _args, "GPRINT:%s_avg:AVERAGE:%s Avg", source.sname, numfmt )
267 -- don't include MAX if rrasingle is enabled
268 if not self.opts.rrasingle then
269 _tif( _args, "GPRINT:%s_max:MAX:%s Max", source.sname, numfmt )
272 -- include total count if requested else include LAST
274 _tif( _args, "GPRINT:%s_avg_sum:LAST:(ca. %s Total)\\l", source.sname, totfmt )
276 _tif( _args, "GPRINT:%s_avg:LAST:%s Last\\l", source.sname, numfmt )
282 -- find all data sources
289 data_types = { dtype }
291 data_types = opts.data.types or { }
294 if not ( dtype or opts.data.types ) then
295 if opts.data.instances then
296 for k, v in pairs(opts.data.instances) do
299 elseif opts.data.sources then
300 for k, v in pairs(opts.data.sources) do
307 -- iterate over data types
308 for i, dtype in ipairs(data_types) do
314 if not opts.per_instance then
315 if type(opts.data.instances) == "table" and type(opts.data.instances[dtype]) == "table" then
316 data_instances = opts.data.instances[dtype]
318 data_instances = self.tree:data_instances( plugin, plugin_instance, dtype )
322 if type(data_instances) ~= "table" or #data_instances == 0 then data_instances = { "" } end
325 -- iterate over data instances
326 for i, dinst in ipairs(data_instances) do
328 -- construct combined data type / instance name
331 if dinst:len() > 0 then
332 dname = dname .. "_" .. dinst
337 local data_sources = { "value" }
339 if type(opts.data.sources) == "table" then
340 if type(opts.data.sources[dname]) == "table" then
341 data_sources = opts.data.sources[dname]
342 elseif type(opts.data.sources[dtype]) == "table" then
343 data_sources = opts.data.sources[dtype]
348 -- iterate over data sources
349 for i, dsource in ipairs(data_sources) do
351 local dsname = dtype .. "_" .. dinst:gsub("[^%w]","_") .. "_" .. dsource
352 local altname = dtype .. "__" .. dsource
354 -- find datasource options
357 if type(opts.data.options) == "table" then
358 if type(opts.data.options[dsname]) == "table" then
359 dopts = opts.data.options[dsname]
360 elseif type(opts.data.options[altname]) == "table" then
361 dopts = opts.data.options[altname]
362 elseif type(opts.data.options[dname]) == "table" then
363 dopts = opts.data.options[dname]
364 elseif type(opts.data.options[dtype]) == "table" then
365 dopts = opts.data.options[dtype]
372 rrd = dopts.rrd or self:mkrrdpath( plugin, plugin_instance, dtype, dinst ),
373 color = dopts.color or self.colors:to_string( self.colors:random() ),
374 flip = dopts.flip or false,
375 total = dopts.total or false,
376 overlay = dopts.overlay or false,
377 noarea = dopts.noarea or false,
381 index = #_sources + 1,
382 sname = ( #_sources + 1 ) .. dtype
386 -- generate datasource title
387 _sources[#_sources].title = self.i18n:ds( _sources[#_sources] )
390 -- find longest name ...
391 if _sources[#_sources].title:len() > _longest_name then
392 _longest_name = _sources[#_sources].title:len()
397 if _sources[#_sources].total then
406 -- construct diagrams
409 -- if per_instance is enabled then find all instances from the first datasource in diagram
410 -- if per_instance is disabled then use an empty pseudo instance and use model provided values
411 local instances = { "" }
413 if opts.per_instance then
414 instances = self.tree:data_instances( plugin, plugin_instance, _sources[1].type )
418 -- iterate over instances
419 for i, instance in ipairs(instances) do
421 -- store title and vlabel
424 _ti( _args, opts.title or self.i18n:title( plugin, plugin_instance, _sources[1].type, instance ) )
426 _ti( _args, opts.vlabel or self.i18n:label( plugin, plugin_instance, _sources[1].type, instance ) )
428 -- store additional rrd options
430 for i, o in ipairs(opts.rrdopts) do _ti( _args, o ) end
434 -- create DEF statements for each instance
435 for i, source in ipairs(_sources) do
436 -- fixup properties for per instance mode...
437 if opts.per_instance then
438 source.instance = instance
439 source.rrd = self:mkrrdpath( plugin, plugin_instance, source.type, instance )
445 -- create CDEF required for calculating totals
448 -- create CDEF statements for each instance in reversed order
449 for i, source in ipairs(_sources) do
450 __cdef( _sources[1 + #_sources - i] )
453 -- create LINE1, AREA and GPRINT statements for each instance
454 for i, source in ipairs(_sources) do
459 -- prepend image path to arg stack
460 _ti( _args, 1, self:mkpngpath( plugin, plugin_instance, index .. instance ) )
462 -- push arg stack to definition list
474 function Graph.render( self, plugin, plugin_instance )
476 dtype_instances = dtype_instances or { "" }
479 -- check for a whole graph handler
480 local plugin_def = "luci.statistics.rrdtool.definitions." .. plugin
481 local stat, def = pcall( require, plugin_def )
483 if stat and def and type(def.rrdargs) == "function" then
485 -- temporary image matrix
488 -- get diagram definitions
489 for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance ) ) ) do
493 -- get diagram definition instances
494 local diagrams = self:_generic( opts, plugin, plugin_instance, nil, i )
496 -- render all diagrams
497 for j, def in ipairs( diagrams ) do
500 _images[i][j] = def[1]
507 -- remember images - XXX: fixme (will cause probs with asymmetric data)
508 for y = 1, #_images[1] do
509 for x = 1, #_images do
510 table.insert( pngs, _images[x][y] )
515 -- no graph handler, iterate over data types
516 for i, dtype in ipairs( self.tree:data_types( plugin, plugin_instance ) ) do
518 -- check for data type handler
519 local dtype_def = plugin_def .. "." .. dtype
520 local stat, def = pcall( require, dtype_def )
522 if stat and def and type(def.rrdargs) == "function" then
524 -- temporary image matrix
527 -- get diagram definitions
528 for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance, dtype ) ) ) do
532 -- get diagram definition instances
533 local diagrams = self:_generic( opts, plugin, plugin_instance, dtype, i )
535 -- render all diagrams
536 for j, def in ipairs( diagrams ) do
539 _images[i][j] = def[1]
546 -- remember images - XXX: fixme (will cause probs with asymmetric data)
547 for y = 1, #_images[1] do
548 for x = 1, #_images do
549 table.insert( pngs, _images[x][y] )
554 -- no data type handler, fall back to builtin definition
555 if type(self.defs.definitions[dtype]) == "table" then
557 -- iterate over data type instances
558 for i, inst in ipairs( self.tree:data_instances( plugin, plugin_instance, dtype ) ) do
560 local title = self:mktitle( plugin, plugin_instance, dtype, inst )
561 local png = self:mkpngpath( plugin, plugin_instance, dtype, inst )
562 local rrd = self:mkrrdpath( plugin, plugin_instance, dtype, inst )
563 local args = { png, "-t", title }
565 for i, o in ipairs(self.defs.definitions[dtype]) do
566 table.insert( args, o )
570 table.insert( pngs, png )
573 self:_rrdtool( args, rrd )