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.cursor()
35 local sections = uci:get_all( "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 == "1" )
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._forcelol( self, list )
89 if type(list[1]) ~= "table" then
95 function Graph._rrdtool( self, def, rrd )
98 local dir = def[1]:gsub("/[^/]+$","")
99 luci.fs.mkdir( dir, true )
101 -- construct commandline
102 local cmdline = "rrdtool graph"
104 -- copy default arguments to def stack
105 for i, opt in ipairs(self.args) do
106 table.insert( def, 1 + i, opt )
109 -- construct commandline from def stack
110 for i, opt in ipairs(def) do
111 opt = opt .. "" -- force string
114 opt = opt:gsub( "{file}", rrd )
117 if opt:match("[^%w]") then
118 cmdline = cmdline .. " '" .. opt .. "'"
120 cmdline = cmdline .. " " .. opt
125 local rrdtool = io.popen( cmdline )
129 function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
131 -- generated graph defs
134 -- internal state variables
137 local _stack_neg = { }
138 local _stack_pos = { }
139 local _longest_name = 0
140 local _has_totals = false
142 -- some convenient aliases
143 local _ti = table.insert
144 local _sf = string.format
146 -- local helper: append a string.format() formatted string to given table
147 function _tif( list, fmt, ... )
148 table.insert( list, string.format( fmt, ... ) )
151 -- local helper: create definitions for min, max, avg and create *_nnl (not null) variable from avg
152 function __def(source)
154 local inst = source.sname
155 local rrd = source.rrd
158 if not ds or ds:len() == 0 then ds = "value" end
160 _tif( _args, "DEF:%s_avg=%s:%s:AVERAGE", inst, rrd, ds )
162 if not self.opts.rrasingle then
163 _tif( _args, "DEF:%s_min=%s:%s:MIN", inst, rrd, ds )
164 _tif( _args, "DEF:%s_max=%s:%s:MAX", inst, rrd, ds )
167 _tif( _args, "CDEF:%s_nnl=%s_avg,UN,0,%s_avg,IF", inst, inst, inst )
170 -- local helper: create cdefs depending on source options like flip and overlay
171 function __cdef(source)
175 -- find previous source, choose stack depending on flip state
177 prev = _stack_neg[#_stack_neg]
179 prev = _stack_pos[#_stack_pos]
182 -- is first source in stack or overlay source: source_stk = source_nnl
183 if not prev or source.overlay then
184 -- create cdef statement
185 _tif( _args, "CDEF:%s_stk=%s_nnl", source.sname, source.sname )
187 -- is subsequent source without overlay: source_stk = source_nnl + previous_stk
189 -- create cdef statement
190 _tif( _args, "CDEF:%s_stk=%s_nnl,%s_stk,+", source.sname, source.sname, prev )
193 -- create multiply by minus one cdef if flip is enabled
196 -- create cdef statement: source_stk = source_stk * -1
197 _tif( _args, "CDEF:%s_neg=%s_stk,-1,*", source.sname, source.sname )
199 -- push to negative stack if overlay is disabled
200 if not source.overlay then
201 _ti( _stack_neg, source.sname )
204 -- no flipping, push to positive stack if overlay is disabled
205 elseif not source.overlay then
207 -- push to positive stack
208 _ti( _stack_pos, source.sname )
211 -- calculate total amount of data if requested
214 "CDEF:%s_avg_sample=%s_avg,UN,0,%s_avg,IF,sample_len,*",
215 source.sname, source.sname, source.sname
219 "CDEF:%s_avg_sum=PREV,UN,0,PREV,IF,%s_avg_sample,+",
220 source.sname, source.sname, source.sname
225 -- local helper: create cdefs required for calculating total values
226 function __cdef_totals()
228 _tif( _args, "CDEF:mytime=%s_avg,TIME,TIME,IF", _sources[1].sname )
229 _ti( _args, "CDEF:sample_len_raw=mytime,PREV(mytime),-" )
230 _ti( _args, "CDEF:sample_len=sample_len_raw,UN,0,sample_len_raw,IF" )
234 -- local helper: create line and area statements
235 function __line(source)
242 -- find colors: try source, then opts.colors; fall back to random color
243 if type(source.color) == "string" then
244 line_color = source.color
245 area_color = self.colors:from_string( line_color )
246 elseif type(opts.colors[source.name:gsub("[^%w]","_")]) == "string" then
247 line_color = opts.colors[source.name:gsub("[^%w]","_")]
248 area_color = self.colors:from_string( line_color )
250 area_color = self.colors:random()
251 line_color = self.colors:to_string( area_color )
254 -- derive area background color from line color
255 area_color = self.colors:to_string( self.colors:faded( area_color ) )
257 -- choose source_stk or source_neg variable depending on flip state
265 legend = _sf( "%-" .. _longest_name .. "s", source.title )
267 -- create area if not disabled
268 if not source.noarea then
269 _tif( _args, "AREA:%s_%s#%s", source.sname, var, area_color )
272 -- create line1 statement
273 _tif( _args, "LINE1:%s_%s#%s:%s", source.sname, var, line_color, legend )
276 -- local helper: create gprint statements
277 function __gprint(source)
279 local numfmt = opts.number_format or "%6.1lf"
280 local totfmt = opts.totals_format or "%5.1lf%s"
282 -- don't include MIN if rrasingle is enabled
283 if not self.opts.rrasingle then
284 _tif( _args, "GPRINT:%s_min:MIN:%s Min", source.sname, numfmt )
287 -- always include AVERAGE
288 _tif( _args, "GPRINT:%s_avg:AVERAGE:%s Avg", source.sname, numfmt )
290 -- don't include MAX if rrasingle is enabled
291 if not self.opts.rrasingle then
292 _tif( _args, "GPRINT:%s_max:MAX:%s Max", source.sname, numfmt )
295 -- include total count if requested else include LAST
297 _tif( _args, "GPRINT:%s_avg_sum:LAST:(ca. %s Total)\\l", source.sname, totfmt )
299 _tif( _args, "GPRINT:%s_avg:LAST:%s Last\\l", source.sname, numfmt )
305 -- find all data sources
312 data_types = { dtype }
314 data_types = opts.data.types or { }
317 if not ( dtype or opts.data.types ) then
318 if opts.data.instances then
319 for k, v in pairs(opts.data.instances) do
322 elseif opts.data.sources then
323 for k, v in pairs(opts.data.sources) do
330 -- iterate over data types
331 for i, dtype in ipairs(data_types) do
337 if not opts.per_instance then
338 if type(opts.data.instances) == "table" and type(opts.data.instances[dtype]) == "table" then
339 data_instances = opts.data.instances[dtype]
341 data_instances = self.tree:data_instances( plugin, plugin_instance, dtype )
345 if type(data_instances) ~= "table" or #data_instances == 0 then data_instances = { "" } end
348 -- iterate over data instances
349 for i, dinst in ipairs(data_instances) do
351 -- construct combined data type / instance name
354 if dinst:len() > 0 then
355 dname = dname .. "_" .. dinst
360 local data_sources = { "value" }
362 if type(opts.data.sources) == "table" then
363 if type(opts.data.sources[dname]) == "table" then
364 data_sources = opts.data.sources[dname]
365 elseif type(opts.data.sources[dtype]) == "table" then
366 data_sources = opts.data.sources[dtype]
371 -- iterate over data sources
372 for i, dsource in ipairs(data_sources) do
374 local dsname = dtype .. "_" .. dinst:gsub("[^%w]","_") .. "_" .. dsource
375 local altname = dtype .. "__" .. dsource
377 -- find datasource options
380 if type(opts.data.options) == "table" then
381 if type(opts.data.options[dsname]) == "table" then
382 dopts = opts.data.options[dsname]
383 elseif type(opts.data.options[altname]) == "table" then
384 dopts = opts.data.options[altname]
385 elseif type(opts.data.options[dname]) == "table" then
386 dopts = opts.data.options[dname]
387 elseif type(opts.data.options[dtype]) == "table" then
388 dopts = opts.data.options[dtype]
395 rrd = dopts.rrd or self:mkrrdpath( plugin, plugin_instance, dtype, dinst ),
396 color = dopts.color or self.colors:to_string( self.colors:random() ),
397 flip = dopts.flip or false,
398 total = dopts.total or false,
399 overlay = dopts.overlay or false,
400 noarea = dopts.noarea or false,
404 index = #_sources + 1,
405 sname = ( #_sources + 1 ) .. dtype
409 -- generate datasource title
410 _sources[#_sources].title = self.i18n:ds( _sources[#_sources] )
413 -- find longest name ...
414 if _sources[#_sources].title:len() > _longest_name then
415 _longest_name = _sources[#_sources].title:len()
420 if _sources[#_sources].total then
429 -- construct diagrams
432 -- if per_instance is enabled then find all instances from the first datasource in diagram
433 -- if per_instance is disabled then use an empty pseudo instance and use model provided values
434 local instances = { "" }
436 if opts.per_instance then
437 instances = self.tree:data_instances( plugin, plugin_instance, _sources[1].type )
441 -- iterate over instances
442 for i, instance in ipairs(instances) do
444 -- store title and vlabel
446 _ti( _args, opts.title or self.i18n:title( plugin, plugin_instance, _sources[1].type, instance ) )
448 _ti( _args, opts.vlabel or self.i18n:label( plugin, plugin_instance, _sources[1].type, instance ) )
450 -- store additional rrd options
452 for i, o in ipairs(opts.rrdopts) do _ti( _args, o ) end
456 -- create DEF statements for each instance
457 for i, source in ipairs(_sources) do
458 -- fixup properties for per instance mode...
459 if opts.per_instance then
460 source.instance = instance
461 source.rrd = self:mkrrdpath( plugin, plugin_instance, source.type, instance )
467 -- create CDEF required for calculating totals
470 -- create CDEF statements for each instance in reversed order
471 for i, source in ipairs(_sources) do
472 __cdef( _sources[1 + #_sources - i] )
475 -- create LINE1, AREA and GPRINT statements for each instance
476 for i, source in ipairs(_sources) do
481 -- prepend image path to arg stack
482 _ti( _args, 1, self:mkpngpath( plugin, plugin_instance, index .. instance ) )
484 -- push arg stack to definition list
496 function Graph.render( self, plugin, plugin_instance )
498 dtype_instances = dtype_instances or { "" }
501 -- check for a whole graph handler
502 local plugin_def = "luci.statistics.rrdtool.definitions." .. plugin
503 local stat, def = luci.util.copcall( require, plugin_def )
505 if stat and def and type(def.rrdargs) == "function" then
507 -- temporary image matrix
510 -- get diagram definitions
511 for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance ) ) ) do
515 -- get diagram definition instances
516 local diagrams = self:_generic( opts, plugin, plugin_instance, nil, i )
518 -- render all diagrams
519 for j, def in ipairs( diagrams ) do
522 _images[i][j] = def[1]
529 -- remember images - XXX: fixme (will cause probs with asymmetric data)
530 for y = 1, #_images[1] do
531 for x = 1, #_images do
532 table.insert( pngs, _images[x][y] )
537 -- no graph handler, iterate over data types
538 for i, dtype in ipairs( self.tree:data_types( plugin, plugin_instance ) ) do
540 -- check for data type handler
541 local dtype_def = plugin_def .. "." .. dtype
542 local stat, def = luci.util.copcall( require, dtype_def )
544 if stat and def and type(def.rrdargs) == "function" then
546 -- temporary image matrix
549 -- get diagram definitions
550 for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance, dtype ) ) ) do
554 -- get diagram definition instances
555 local diagrams = self:_generic( opts, plugin, plugin_instance, dtype, i )
557 -- render all diagrams
558 for j, def in ipairs( diagrams ) do
561 _images[i][j] = def[1]
568 -- remember images - XXX: fixme (will cause probs with asymmetric data)
569 for y = 1, #_images[1] do
570 for x = 1, #_images do
571 table.insert( pngs, _images[x][y] )
576 -- no data type handler, fall back to builtin definition
577 if type(self.defs.definitions[dtype]) == "table" then
579 -- iterate over data type instances
580 for i, inst in ipairs( self.tree:data_instances( plugin, plugin_instance, dtype ) ) do
582 local title = self.i18n:title( plugin, plugin_instance, dtype, inst )
583 local label = self.i18n:label( plugin, plugin_instance, dtype, inst )
584 local png = self:mkpngpath( plugin, plugin_instance, dtype, inst )
585 local rrd = self:mkrrdpath( plugin, plugin_instance, dtype, inst )
586 local args = { png, "-t", title, "-v", label }
588 for i, o in ipairs(self.defs.definitions[dtype]) do
589 -- XXX: this is a somewhat ugly hack to exclude min/max RRAs when rrasingle is on
590 if not ( self.opts.rrasingle and ( o:match("_min") or o:match("_max") ) ) then
591 table.insert( args, o )
596 table.insert( pngs, png )
599 self:_rrdtool( args, rrd )