1 -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
4 module("luci.statistics.rrdtool", package.seeall)
6 require("luci.statistics.datatree")
7 require("luci.statistics.rrdtool.colors")
8 require("luci.statistics.i18n")
9 require("luci.model.uci")
13 local fs = require "nixio.fs"
16 Graph = luci.util.class()
18 function Graph.__init__( self, timespan, opts )
22 local uci = luci.model.uci.cursor()
23 local sections = uci:get_all( "luci_statistics" )
26 opts.timespan = timespan or sections.rrdtool.default_timespan or 900
27 opts.rrasingle = opts.rrasingle or ( sections.collectd_rrdtool.RRASingle == "1" )
28 opts.rramax = opts.rramax or ( sections.collectd_rrdtool.RRAMax == "1" )
29 opts.host = opts.host or sections.collectd.Hostname or luci.sys.hostname()
30 opts.width = opts.width or sections.rrdtool.image_width or 400
31 opts.rrdpath = opts.rrdpath or sections.collectd_rrdtool.DataDir or "/tmp/rrd"
32 opts.imgpath = opts.imgpath or sections.rrdtool.image_path or "/tmp/rrdimg"
33 opts.rrdpath = opts.rrdpath:gsub("/$","")
34 opts.imgpath = opts.imgpath:gsub("/$","")
37 self.colors = luci.statistics.rrdtool.colors.Instance()
38 self.tree = luci.statistics.datatree.Instance(opts.host)
39 self.i18n = luci.statistics.i18n.Instance( self )
41 -- rrdtool default args
44 "-s", "NOW-" .. opts.timespan,
52 function Graph._mkpath( self, plugin, plugin_instance, dtype, dtype_instance )
53 local t = self.opts.host .. "/" .. plugin
54 if type(plugin_instance) == "string" and plugin_instance:len() > 0 then
55 t = t .. "-" .. plugin_instance
58 if type(dtype_instance) == "string" and dtype_instance:len() > 0 then
59 t = t .. "-" .. dtype_instance
64 function Graph.mkrrdpath( self, ... )
65 return string.format( "%s/%s.rrd", self.opts.rrdpath, self:_mkpath( ... ) )
68 function Graph.mkpngpath( self, ... )
69 return string.format( "%s/%s.%i.png", self.opts.imgpath, self:_mkpath( ... ), self.opts.timespan )
72 function Graph.strippngpath( self, path )
73 return path:sub( self.opts.imgpath:len() + 2 )
76 function Graph._forcelol( self, list )
77 if type(list[1]) ~= "table" then
83 function Graph._rrdtool( self, def, rrd )
86 local dir = def[1]:gsub("/[^/]+$","")
89 -- construct commandline
90 local cmdline = { "rrdtool", "graph" }
92 -- copy default arguments to def stack
93 for i, opt in ipairs(self.args) do
94 table.insert( def, 1 + i, opt )
97 -- construct commandline from def stack
98 for i, opt in ipairs(def) do
99 opt = opt .. "" -- force string
102 opt = opt:gsub( "{file}", rrd )
105 cmdline[#cmdline+1] = luci.util.shellquote(opt)
109 local rrdtool = io.popen(table.concat(cmdline, " "))
113 function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
115 -- generated graph defs
118 -- internal state variables
121 local _stack_neg = { }
122 local _stack_pos = { }
123 local _longest_name = 0
124 local _has_totals = false
126 -- some convenient aliases
127 local _ti = table.insert
128 local _sf = string.format
130 -- local helper: append a string.format() formatted string to given table
131 function _tif( list, fmt, ... )
132 table.insert( list, string.format( fmt, ... ) )
135 -- local helper: create definitions for min, max, avg and create *_nnl (not null) variable from avg
136 function __def(source)
138 local inst = source.sname
139 local rrd = source.rrd
142 if not ds or ds:len() == 0 then ds = "value" end
144 _tif( _args, "DEF:%s_avg_raw=%s:%s:AVERAGE", inst, rrd, ds )
145 _tif( _args, "CDEF:%s_avg=%s_avg_raw,%s", inst, inst, source.transform_rpn )
147 if not self.opts.rrasingle then
148 _tif( _args, "DEF:%s_min_raw=%s:%s:MIN", inst, rrd, ds )
149 _tif( _args, "CDEF:%s_min=%s_min_raw,%s", inst, inst, source.transform_rpn )
150 _tif( _args, "DEF:%s_max_raw=%s:%s:MAX", inst, rrd, ds )
151 _tif( _args, "CDEF:%s_max=%s_max_raw,%s", inst, inst, source.transform_rpn )
154 _tif( _args, "CDEF:%s_nnl=%s_avg,UN,0,%s_avg,IF", inst, inst, inst )
157 -- local helper: create cdefs depending on source options like flip and overlay
158 function __cdef(source)
162 -- find previous source, choose stack depending on flip state
164 prev = _stack_neg[#_stack_neg]
166 prev = _stack_pos[#_stack_pos]
169 -- is first source in stack or overlay source: source_stk = source_nnl
170 if not prev or source.overlay then
171 if self.opts.rrasingle or not self.opts.rramax then
172 -- create cdef statement for cumulative stack (no NaNs) and also
173 -- for display (preserving NaN where no points should be displayed)
174 _tif( _args, "CDEF:%s_stk=%s_nnl", source.sname, source.sname )
175 _tif( _args, "CDEF:%s_plot=%s_avg", source.sname, source.sname )
177 -- create cdef statement for cumulative stack (no NaNs) and also
178 -- for display (preserving NaN where no points should be displayed)
179 _tif( _args, "CDEF:%s_stk=%s_nnl", source.sname, source.sname )
180 _tif( _args, "CDEF:%s_plot=%s_max", source.sname, source.sname )
183 -- is subsequent source without overlay: source_stk = source_nnl + previous_stk
185 if self.opts.rrasingle or not self.opts.rramax then
186 -- create cdef statement
187 _tif( _args, "CDEF:%s_stk=%s_nnl,%s_stk,+", source.sname, source.sname, prev )
188 _tif( _args, "CDEF:%s_plot=%s_avg,%s_stk,+", source.sname, source.sname, prev )
190 -- create cdef statement
191 _tif( _args, "CDEF:%s_stk=%s_nnl,%s_stk,+", source.sname, source.sname, prev )
192 _tif( _args, "CDEF:%s_plot=%s_max,%s_stk,+", source.sname, source.sname, prev )
196 -- create multiply by minus one cdef if flip is enabled
199 -- create cdef statement: source_stk = source_stk * -1
200 _tif( _args, "CDEF:%s_neg=%s_plot,-1,*", source.sname, source.sname )
202 -- push to negative stack if overlay is disabled
203 if not source.overlay then
204 _ti( _stack_neg, source.sname )
207 -- no flipping, push to positive stack if overlay is disabled
208 elseif not source.overlay then
210 -- push to positive stack
211 _ti( _stack_pos, source.sname )
214 -- calculate total amount of data if requested
217 "CDEF:%s_avg_sample=%s_avg,UN,0,%s_avg,IF,sample_len,*",
218 source.sname, source.sname, source.sname
222 "CDEF:%s_avg_sum=PREV,UN,0,PREV,IF,%s_avg_sample,+",
223 source.sname, source.sname, source.sname
228 -- local helper: create cdefs required for calculating total values
229 function __cdef_totals()
231 _tif( _args, "CDEF:mytime=%s_avg,TIME,TIME,IF", _sources[1].sname )
232 _ti( _args, "CDEF:sample_len_raw=mytime,PREV(mytime),-" )
233 _ti( _args, "CDEF:sample_len=sample_len_raw,UN,0,sample_len_raw,IF" )
237 -- local helper: create line and area statements
238 function __line(source)
245 -- find colors: try source, then opts.colors; fall back to random color
246 if type(source.color) == "string" then
247 line_color = source.color
248 area_color = self.colors:from_string( line_color )
249 elseif type(opts.colors[source.name:gsub("[^%w]","_")]) == "string" then
250 line_color = opts.colors[source.name:gsub("[^%w]","_")]
251 area_color = self.colors:from_string( line_color )
253 area_color = self.colors:random()
254 line_color = self.colors:to_string( area_color )
257 -- derive area background color from line color
258 area_color = self.colors:to_string( self.colors:faded( area_color ) )
260 -- choose source_plot or source_neg variable depending on flip state
268 legend = _sf( "%-" .. _longest_name .. "s", source.title )
270 -- create area if not disabled
271 if not source.noarea then
272 _tif( _args, "AREA:%s_%s#%s", source.sname, var, area_color )
275 -- create line1 statement
276 _tif( _args, "LINE%d:%s_%s#%s:%s",
277 source.width or (source.noarea and 2 or 1),
278 source.sname, var, line_color, legend )
281 -- local helper: create gprint statements
282 function __gprint(source)
284 local numfmt = opts.number_format or "%6.1lf"
285 local totfmt = opts.totals_format or "%5.1lf%s"
287 -- don't include MIN if rrasingle is enabled
288 if not self.opts.rrasingle then
289 _tif( _args, "GPRINT:%s_min:MIN:\tMin\\: %s", source.sname, numfmt )
292 -- always include AVERAGE
293 _tif( _args, "GPRINT:%s_avg:AVERAGE:\tAvg\\: %s", source.sname, numfmt )
295 -- don't include MAX if rrasingle is enabled
296 if not self.opts.rrasingle then
297 _tif( _args, "GPRINT:%s_max:MAX:\tMax\\: %s", source.sname, numfmt )
300 -- include total count if requested else include LAST
302 _tif( _args, "GPRINT:%s_avg_sum:LAST:(ca. %s Total)\\l", source.sname, totfmt )
304 _tif( _args, "GPRINT:%s_avg:LAST:\tLast\\: %s\\l", source.sname, numfmt )
310 -- find all data sources
317 data_types = { dtype }
319 data_types = opts.data.types or { }
322 if not ( dtype or opts.data.types ) then
323 if opts.data.instances then
324 for k, v in pairs(opts.data.instances) do
327 elseif opts.data.sources then
328 for k, v in pairs(opts.data.sources) do
335 -- iterate over data types
336 for i, dtype in ipairs(data_types) do
342 if not opts.per_instance then
343 if type(opts.data.instances) == "table" and type(opts.data.instances[dtype]) == "table" then
344 data_instances = opts.data.instances[dtype]
346 data_instances = self.tree:data_instances( plugin, plugin_instance, dtype )
350 if type(data_instances) ~= "table" or #data_instances == 0 then data_instances = { "" } end
353 -- iterate over data instances
354 for i, dinst in ipairs(data_instances) do
356 -- construct combined data type / instance name
359 if dinst:len() > 0 then
360 dname = dname .. "_" .. dinst
365 local data_sources = { "value" }
367 if type(opts.data.sources) == "table" then
368 if type(opts.data.sources[dname]) == "table" then
369 data_sources = opts.data.sources[dname]
370 elseif type(opts.data.sources[dtype]) == "table" then
371 data_sources = opts.data.sources[dtype]
376 -- iterate over data sources
377 for i, dsource in ipairs(data_sources) do
379 local dsname = dtype .. "_" .. dinst:gsub("[^%w]","_") .. "_" .. dsource
380 local altname = dtype .. "__" .. dsource
382 --assert(dtype ~= "ping", dsname .. " or " .. altname)
384 -- find datasource options
387 if type(opts.data.options) == "table" then
388 if type(opts.data.options[dsname]) == "table" then
389 dopts = opts.data.options[dsname]
390 elseif type(opts.data.options[altname]) == "table" then
391 dopts = opts.data.options[altname]
392 elseif type(opts.data.options[dname]) == "table" then
393 dopts = opts.data.options[dname]
394 elseif type(opts.data.options[dtype]) == "table" then
395 dopts = opts.data.options[dtype]
402 rrd = dopts.rrd or self:mkrrdpath( plugin, plugin_instance, dtype, dinst ),
403 color = dopts.color or self.colors:to_string( self.colors:random() ),
404 flip = dopts.flip or false,
405 total = dopts.total or false,
406 overlay = dopts.overlay or false,
407 transform_rpn = dopts.transform_rpn or "0,+",
408 noarea = dopts.noarea or false,
409 title = dopts.title or nil,
410 weight = dopts.weight or nil,
414 index = #_sources + 1,
415 sname = ( #_sources + 1 ) .. dtype
419 -- generate datasource title
420 _sources[#_sources].title = self.i18n:ds( _sources[#_sources] )
423 -- find longest name ...
424 if _sources[#_sources].title:len() > _longest_name then
425 _longest_name = _sources[#_sources].title:len()
430 if _sources[#_sources].total then
439 -- construct diagrams
442 -- if per_instance is enabled then find all instances from the first datasource in diagram
443 -- if per_instance is disabled then use an empty pseudo instance and use model provided values
444 local instances = { "" }
446 if opts.per_instance then
447 instances = self.tree:data_instances( plugin, plugin_instance, _sources[1].type )
451 -- iterate over instances
452 for i, instance in ipairs(instances) do
454 -- store title and vlabel
456 _ti( _args, self.i18n:title( plugin, plugin_instance, _sources[1].type, instance, opts.title ) )
458 _ti( _args, self.i18n:label( plugin, plugin_instance, _sources[1].type, instance, opts.vlabel ) )
461 _ti ( _args, opts.y_max )
465 _ti ( _args, opts.y_min )
467 if opts.units_exponent then
469 _ti ( _args, opts.units_exponent )
471 if opts.alt_autoscale then
474 if opts.alt_autoscale_max then
478 -- store additional rrd options
480 for i, o in ipairs(opts.rrdopts) do _ti( _args, o ) end
484 table.sort(_sources, function(a, b)
485 local x = a.weight or a.index or 0
486 local y = b.weight or b.index or 0
490 -- create DEF statements for each instance
491 for i, source in ipairs(_sources) do
492 -- fixup properties for per instance mode...
493 if opts.per_instance then
494 source.instance = instance
495 source.rrd = self:mkrrdpath( plugin, plugin_instance, source.type, instance )
501 -- create CDEF required for calculating totals
504 -- create CDEF statements for each instance in reversed order
505 for i, source in ipairs(_sources) do
506 __cdef( _sources[1 + #_sources - i] )
509 -- create LINE1, AREA and GPRINT statements for each instance
510 for i, source in ipairs(_sources) do
515 -- prepend image path to arg stack
516 _ti( _args, 1, self:mkpngpath( plugin, plugin_instance, index .. instance ) )
518 -- push arg stack to definition list
530 function Graph.render( self, plugin, plugin_instance, is_index )
532 dtype_instances = dtype_instances or { "" }
535 -- check for a whole graph handler
536 local plugin_def = "luci.statistics.rrdtool.definitions." .. plugin
537 local stat, def = pcall( require, plugin_def )
539 if stat and def and type(def.rrdargs) == "function" then
541 -- temporary image matrix
544 -- get diagram definitions
545 for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance, nil, is_index ) ) ) do
546 if not is_index or not opts.detail then
549 -- get diagram definition instances
550 local diagrams = self:_generic( opts, plugin, plugin_instance, nil, i )
552 -- render all diagrams
553 for j, def in ipairs( diagrams ) do
555 _images[i][j] = def[1]
563 -- remember images - XXX: fixme (will cause probs with asymmetric data)
564 for y = 1, #_images[1] do
565 for x = 1, #_images do
566 table.insert( pngs, _images[x][y] )