3 Luci statistics - rrdtool interface library / diagram model interpreter
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
16 module("luci.statistics.rrdtool", package.seeall)
18 require("luci.statistics.datatree")
19 require("luci.statistics.rrdtool.colors")
20 require("luci.statistics.rrdtool.definitions")
21 require("luci.statistics.i18n")
22 require("luci.model.uci")
28 Graph = luci.util.class()
30 function Graph.__init__( self, timespan, opts )
34 local uci = luci.model.uci.Session()
35 local sections = uci:sections( "luci_statistics" )
38 self.colors = luci.statistics.rrdtool.colors.Instance()
39 self.defs = luci.statistics.rrdtool.definitions.Instance()
40 self.tree = luci.statistics.datatree.Instance()
41 self.i18n = luci.statistics.i18n.Instance( self )
44 opts.timespan = timespan or sections.rrdtool.default_timespan or 900
45 opts.rrasingle = opts.rrasingle or ( sections.collectd_rrdtool.RRASingle ~= "0" )
46 opts.host = opts.host or sections.collectd.Hostname or luci.sys.hostname()
47 opts.width = opts.width or sections.rrdtool.image_width or 400
48 opts.rrdpath = opts.rrdpath or sections.collectd_rrdtool.DataDir or "/tmp/rrd"
49 opts.imgpath = opts.imgpath or sections.rrdtool.image_path or "/tmp/rrdimg"
50 opts.rrdpath = opts.rrdpath:gsub("/$","")
51 opts.imgpath = opts.imgpath:gsub("/$","")
53 -- rrdtool default args
56 "-s", "NOW-" .. opts.timespan,
64 function Graph._mkpath( self, plugin, plugin_instance, dtype, dtype_instance )
65 local t = self.opts.host .. "/" .. plugin
66 if type(plugin_instance) == "string" and plugin_instance:len() > 0 then
67 t = t .. "-" .. plugin_instance
70 if type(dtype_instance) == "string" and dtype_instance:len() > 0 then
71 t = t .. "-" .. dtype_instance
76 function Graph.mkrrdpath( self, ... )
77 return string.format( "%s/%s.rrd", self.opts.rrdpath, self:_mkpath( ... ) )
80 function Graph.mkpngpath( self, ... )
81 return string.format( "%s/%s.%i.png", self.opts.imgpath, self:_mkpath( ... ), self.opts.timespan )
84 function Graph.strippngpath( self, path )
85 return path:sub( self.opts.imgpath:len() + 2 )
88 function Graph.mktitle( self, plugin, plugin_instance, dtype, dtype_instance )
90 -- try various strings to retrieve a diagram title from the langfile
94 function Graph._forcelol( self, list )
95 if type(list[1]) ~= "table" then
101 function Graph._rrdtool( self, def, rrd )
104 local dir = def[1]:gsub("/[^/]+$","")
105 luci.fs.mkdir( dir, true )
107 -- construct commandline
108 local cmdline = "rrdtool graph"
110 -- copy default arguments to def stack
111 for i, opt in ipairs(self.args) do
112 table.insert( def, 1 + i, opt )
115 -- construct commandline from def stack
116 for i, opt in ipairs(def) do
117 opt = opt .. "" -- force string
120 opt = opt:gsub( "{file}", rrd )
123 if opt:match("[^%w]") then
124 cmdline = cmdline .. " '" .. opt .. "'"
126 cmdline = cmdline .. " " .. opt
131 local rrdtool = io.popen( cmdline )
135 function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
137 -- generated graph defs
140 -- internal state variables
143 local _stack_neg = { }
144 local _stack_pos = { }
145 local _longest_name = 0
146 local _has_totals = false
148 -- some convenient aliases
149 local _ti = table.insert
150 local _sf = string.format
152 -- local helper: append a string.format() formatted string to given table
153 function _tif( list, fmt, ... )
154 table.insert( list, string.format( fmt, ... ) )
157 -- local helper: create definitions for min, max, avg and create *_nnl (not null) variable from avg
158 function __def(source)
160 local inst = source.sname
161 local rrd = source.rrd
164 if not ds or ds:len() == 0 then ds = "value" end
166 _tif( _args, "DEF:%s_avg=%s:%s:AVERAGE", inst, rrd, ds )
168 if not self.opts.rrasingle then
169 _tif( _args, "DEF:%s_min=%s:%s:MIN", inst, rrd, ds )
170 _tif( _args, "DEF:%s_max=%s:%s:MAX", inst, rrd, ds )
173 _tif( _args, "CDEF:%s_nnl=%s_avg,UN,0,%s_avg,IF", inst, inst, inst )
176 -- local helper: create cdefs depending on source options like flip and overlay
177 function __cdef(source)
181 -- find previous source, choose stack depending on flip state
183 prev = _stack_neg[#_stack_neg]
185 prev = _stack_pos[#_stack_pos]
188 -- is first source in stack or overlay source: source_stk = source_nnl
189 if not prev or source.overlay then
190 -- create cdef statement
191 _tif( _args, "CDEF:%s_stk=%s_nnl", source.sname, source.sname )
193 -- is subsequent source without overlay: source_stk = source_nnl + previous_stk
195 -- create cdef statement
196 _tif( _args, "CDEF:%s_stk=%s_nnl,%s_stk,+", source.sname, source.sname, prev )
199 -- create multiply by minus one cdef if flip is enabled
202 -- create cdef statement: source_stk = source_stk * -1
203 _tif( _args, "CDEF:%s_neg=%s_stk,-1,*", source.sname, source.sname )
205 -- push to negative stack if overlay is disabled
206 if not source.overlay then
207 _ti( _stack_neg, source.sname )
210 -- no flipping, push to positive stack if overlay is disabled
211 elseif not source.overlay then
213 -- push to positive stack
214 _ti( _stack_pos, source.sname )
217 -- calculate total amount of data if requested
220 "CDEF:%s_avg_sample=%s_avg,UN,0,%s_avg,IF,sample_len,*",
221 source.sname, source.sname, source.sname
225 "CDEF:%s_avg_sum=PREV,UN,0,PREV,IF,%s_avg_sample,+",
226 source.sname, source.sname, source.sname
231 -- local helper: create cdefs required for calculating total values
232 function __cdef_totals()
234 _tif( _args, "CDEF:mytime=%s_avg,TIME,TIME,IF", _sources[1].sname )
235 _ti( _args, "CDEF:sample_len_raw=mytime,PREV(mytime),-" )
236 _ti( _args, "CDEF:sample_len=sample_len_raw,UN,0,sample_len_raw,IF" )
240 -- local helper: create line and area statements
241 function __line(source)
248 -- find colors: try source, then opts.colors; fall back to random color
249 if type(source.color) == "string" then
250 line_color = source.color
251 area_color = self.colors:from_string( line_color )
252 elseif type(opts.colors[source.name:gsub("[^%w]","_")]) == "string" then
253 line_color = opts.colors[source.name:gsub("[^%w]","_")]
254 area_color = self.colors:from_string( line_color )
256 area_color = self.colors:random()
257 line_color = self.colors:to_string( area_color )
260 -- derive area background color from line color
261 area_color = self.colors:to_string( self.colors:faded( area_color ) )
263 -- choose source_stk or source_neg variable depending on flip state
271 legend = _sf( "%-" .. _longest_name .. "s", source.title )
273 -- create area if not disabled
274 if not source.noarea then
275 _tif( _args, "AREA:%s_%s#%s", source.sname, var, area_color )
278 -- create line1 statement
279 _tif( _args, "LINE1:%s_%s#%s:%s", source.sname, var, line_color, legend )
282 -- local helper: create gprint statements
283 function __gprint(source)
285 local numfmt = opts.number_format or "%6.1lf"
286 local totfmt = opts.totals_format or "%5.1lf%s"
288 -- don't include MIN if rrasingle is enabled
289 if not self.opts.rrasingle then
290 _tif( _args, "GPRINT:%s_min:MIN:%s Min", source.sname, numfmt )
293 -- always include AVERAGE
294 _tif( _args, "GPRINT:%s_avg:AVERAGE:%s Avg", source.sname, numfmt )
296 -- don't include MAX if rrasingle is enabled
297 if not self.opts.rrasingle then
298 _tif( _args, "GPRINT:%s_max:MAX:%s Max", source.sname, numfmt )
301 -- include total count if requested else include LAST
303 _tif( _args, "GPRINT:%s_avg_sum:LAST:(ca. %s Total)\\l", source.sname, totfmt )
305 _tif( _args, "GPRINT:%s_avg:LAST:%s Last\\l", source.sname, numfmt )
311 -- find all data sources
318 data_types = { dtype }
320 data_types = opts.data.types or { }
323 if not ( dtype or opts.data.types ) then
324 if opts.data.instances then
325 for k, v in pairs(opts.data.instances) do
328 elseif opts.data.sources then
329 for k, v in pairs(opts.data.sources) do
336 -- iterate over data types
337 for i, dtype in ipairs(data_types) do
343 if not opts.per_instance then
344 if type(opts.data.instances) == "table" and type(opts.data.instances[dtype]) == "table" then
345 data_instances = opts.data.instances[dtype]
347 data_instances = self.tree:data_instances( plugin, plugin_instance, dtype )
351 if type(data_instances) ~= "table" or #data_instances == 0 then data_instances = { "" } end
354 -- iterate over data instances
355 for i, dinst in ipairs(data_instances) do
357 -- construct combined data type / instance name
360 if dinst:len() > 0 then
361 dname = dname .. "_" .. dinst
366 local data_sources = { "value" }
368 if type(opts.data.sources) == "table" then
369 if type(opts.data.sources[dname]) == "table" then
370 data_sources = opts.data.sources[dname]
371 elseif type(opts.data.sources[dtype]) == "table" then
372 data_sources = opts.data.sources[dtype]
377 -- iterate over data sources
378 for i, dsource in ipairs(data_sources) do
380 local dsname = dtype .. "_" .. dinst:gsub("[^%w]","_") .. "_" .. dsource
381 local altname = dtype .. "__" .. dsource
383 -- find datasource options
386 if type(opts.data.options) == "table" then
387 if type(opts.data.options[dsname]) == "table" then
388 dopts = opts.data.options[dsname]
389 elseif type(opts.data.options[altname]) == "table" then
390 dopts = opts.data.options[altname]
391 elseif type(opts.data.options[dname]) == "table" then
392 dopts = opts.data.options[dname]
393 elseif type(opts.data.options[dtype]) == "table" then
394 dopts = opts.data.options[dtype]
401 rrd = dopts.rrd or self:mkrrdpath( plugin, plugin_instance, dtype, dinst ),
402 color = dopts.color or self.colors:to_string( self.colors:random() ),
403 flip = dopts.flip or false,
404 total = dopts.total or false,
405 overlay = dopts.overlay or false,
406 noarea = dopts.noarea or false,
410 index = #_sources + 1,
411 sname = ( #_sources + 1 ) .. dtype
415 -- generate datasource title
416 _sources[#_sources].title = self.i18n:ds( _sources[#_sources] )
419 -- find longest name ...
420 if _sources[#_sources].title:len() > _longest_name then
421 _longest_name = _sources[#_sources].title:len()
426 if _sources[#_sources].total then
435 -- construct diagrams
438 -- if per_instance is enabled then find all instances from the first datasource in diagram
439 -- if per_instance is disabled then use an empty pseudo instance and use model provided values
440 local instances = { "" }
442 if opts.per_instance then
443 instances = self.tree:data_instances( plugin, plugin_instance, _sources[1].type )
447 -- iterate over instances
448 for i, instance in ipairs(instances) do
450 -- store title and vlabel
452 _ti( _args, opts.title or self.i18n:title( plugin, plugin_instance, _sources[1].type, instance ) )
454 _ti( _args, opts.vlabel or self.i18n:label( plugin, plugin_instance, _sources[1].type, instance ) )
456 -- store additional rrd options
458 for i, o in ipairs(opts.rrdopts) do _ti( _args, o ) end
462 -- create DEF statements for each instance
463 for i, source in ipairs(_sources) do
464 -- fixup properties for per instance mode...
465 if opts.per_instance then
466 source.instance = instance
467 source.rrd = self:mkrrdpath( plugin, plugin_instance, source.type, instance )
473 -- create CDEF required for calculating totals
476 -- create CDEF statements for each instance in reversed order
477 for i, source in ipairs(_sources) do
478 __cdef( _sources[1 + #_sources - i] )
481 -- create LINE1, AREA and GPRINT statements for each instance
482 for i, source in ipairs(_sources) do
487 -- prepend image path to arg stack
488 _ti( _args, 1, self:mkpngpath( plugin, plugin_instance, index .. instance ) )
490 -- push arg stack to definition list
502 function Graph.render( self, plugin, plugin_instance )
504 dtype_instances = dtype_instances or { "" }
507 -- check for a whole graph handler
508 local plugin_def = "luci.statistics.rrdtool.definitions." .. plugin
509 local stat, def = pcall( require, plugin_def )
511 if stat and def and type(def.rrdargs) == "function" then
513 -- temporary image matrix
516 -- get diagram definitions
517 for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance ) ) ) do
521 -- get diagram definition instances
522 local diagrams = self:_generic( opts, plugin, plugin_instance, nil, i )
524 -- render all diagrams
525 for j, def in ipairs( diagrams ) do
528 _images[i][j] = def[1]
535 -- remember images - XXX: fixme (will cause probs with asymmetric data)
536 for y = 1, #_images[1] do
537 for x = 1, #_images do
538 table.insert( pngs, _images[x][y] )
543 -- no graph handler, iterate over data types
544 for i, dtype in ipairs( self.tree:data_types( plugin, plugin_instance ) ) do
546 -- check for data type handler
547 local dtype_def = plugin_def .. "." .. dtype
548 local stat, def = pcall( require, dtype_def )
550 if stat and def and type(def.rrdargs) == "function" then
552 -- temporary image matrix
555 -- get diagram definitions
556 for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance, dtype ) ) ) do
560 -- get diagram definition instances
561 local diagrams = self:_generic( opts, plugin, plugin_instance, dtype, i )
563 -- render all diagrams
564 for j, def in ipairs( diagrams ) do
567 _images[i][j] = def[1]
574 -- remember images - XXX: fixme (will cause probs with asymmetric data)
575 for y = 1, #_images[1] do
576 for x = 1, #_images do
577 table.insert( pngs, _images[x][y] )
582 -- no data type handler, fall back to builtin definition
583 if type(self.defs.definitions[dtype]) == "table" then
585 -- iterate over data type instances
586 for i, inst in ipairs( self.tree:data_instances( plugin, plugin_instance, dtype ) ) do
588 local title = self:mktitle( plugin, plugin_instance, dtype, inst )
589 local png = self:mkpngpath( plugin, plugin_instance, dtype, inst )
590 local rrd = self:mkrrdpath( plugin, plugin_instance, dtype, inst )
591 local args = { png, "-t", title }
593 for i, o in ipairs(self.defs.definitions[dtype]) do
594 table.insert( args, o )
598 table.insert( pngs, png )
601 self:_rrdtool( args, rrd )