require("luci.statistics.datatree")
require("luci.statistics.rrdtool.colors")
-require("luci.statistics.rrdtool.definitions")
require("luci.statistics.i18n")
require("luci.model.uci")
require("luci.util")
require("luci.sys")
-require("luci.fs")
+
+local fs = require "nixio.fs"
Graph = luci.util.class()
opts = opts or { }
- local uci = luci.model.uci
- local sections = uci.get_all( "luci_statistics" )
-
- -- helper classes
- self.colors = luci.statistics.rrdtool.colors.Instance()
- self.defs = luci.statistics.rrdtool.definitions.Instance()
- self.tree = luci.statistics.datatree.Instance()
- self.i18n = luci.statistics.i18n.Instance( self )
+ local uci = luci.model.uci.cursor()
+ local sections = uci:get_all( "luci_statistics" )
-- options
opts.timespan = timespan or sections.rrdtool.default_timespan or 900
- opts.rrasingle = opts.rrasingle or ( sections.collectd_rrdtool.RRASingle ~= "0" )
+ opts.rrasingle = opts.rrasingle or ( sections.collectd_rrdtool.RRASingle == "1" )
opts.host = opts.host or sections.collectd.Hostname or luci.sys.hostname()
opts.width = opts.width or sections.rrdtool.image_width or 400
opts.rrdpath = opts.rrdpath or sections.collectd_rrdtool.DataDir or "/tmp/rrd"
opts.rrdpath = opts.rrdpath:gsub("/$","")
opts.imgpath = opts.imgpath:gsub("/$","")
+ -- helper classes
+ self.colors = luci.statistics.rrdtool.colors.Instance()
+ self.tree = luci.statistics.datatree.Instance(opts.host)
+ self.i18n = luci.statistics.i18n.Instance( self )
+
-- rrdtool default args
self.args = {
"-a", "PNG",
-- prepare directory
local dir = def[1]:gsub("/[^/]+$","")
- luci.fs.mkdir( dir, true )
+ fs.mkdirr( dir )
-- construct commandline
local cmdline = "rrdtool graph"
if not ds or ds:len() == 0 then ds = "value" end
- _tif( _args, "DEF:%s_avg=%s:%s:AVERAGE", inst, rrd, ds )
+ _tif( _args, "DEF:%s_avg_raw=%s:%s:AVERAGE", inst, rrd, ds )
+ _tif( _args, "CDEF:%s_avg=%s_avg_raw,%s", inst, inst, source.transform_rpn )
if not self.opts.rrasingle then
- _tif( _args, "DEF:%s_min=%s:%s:MIN", inst, rrd, ds )
- _tif( _args, "DEF:%s_max=%s:%s:MAX", inst, rrd, ds )
+ _tif( _args, "DEF:%s_min_raw=%s:%s:MIN", inst, rrd, ds )
+ _tif( _args, "CDEF:%s_min=%s_min_raw,%s", inst, inst, source.transform_rpn )
+ _tif( _args, "DEF:%s_max_raw=%s:%s:MAX", inst, rrd, ds )
+ _tif( _args, "CDEF:%s_max=%s_max_raw,%s", inst, inst, source.transform_rpn )
end
_tif( _args, "CDEF:%s_nnl=%s_avg,UN,0,%s_avg,IF", inst, inst, inst )
-- is first source in stack or overlay source: source_stk = source_nnl
if not prev or source.overlay then
- -- create cdef statement
+ -- create cdef statement for cumulative stack (no NaNs) and also
+ -- for display (preserving NaN where no points should be displayed)
_tif( _args, "CDEF:%s_stk=%s_nnl", source.sname, source.sname )
+ _tif( _args, "CDEF:%s_plot=%s_avg", source.sname, source.sname )
-- is subsequent source without overlay: source_stk = source_nnl + previous_stk
else
-- create cdef statement
_tif( _args, "CDEF:%s_stk=%s_nnl,%s_stk,+", source.sname, source.sname, prev )
+ _tif( _args, "CDEF:%s_plot=%s_avg,%s_stk,+", source.sname, source.sname, prev )
end
-- create multiply by minus one cdef if flip is enabled
if source.flip then
-- create cdef statement: source_stk = source_stk * -1
- _tif( _args, "CDEF:%s_neg=%s_stk,-1,*", source.sname, source.sname )
+ _tif( _args, "CDEF:%s_neg=%s_plot,-1,*", source.sname, source.sname )
-- push to negative stack if overlay is disabled
if not source.overlay then
-- derive area background color from line color
area_color = self.colors:to_string( self.colors:faded( area_color ) )
- -- choose source_stk or source_neg variable depending on flip state
+ -- choose source_plot or source_neg variable depending on flip state
if source.flip then
var = "neg"
else
- var = "stk"
+ var = "plot"
end
-- create legend
end
-- create line1 statement
- _tif( _args, "LINE1:%s_%s#%s:%s", source.sname, var, line_color, legend )
+ _tif( _args, "LINE%d:%s_%s#%s:%s",
+ source.noarea and 2 or 1,
+ source.sname, var, line_color, legend )
end
-- local helper: create gprint statements
-- don't include MIN if rrasingle is enabled
if not self.opts.rrasingle then
- _tif( _args, "GPRINT:%s_min:MIN:%s Min", source.sname, numfmt )
+ _tif( _args, "GPRINT:%s_min:MIN:\tMin\\: %s", source.sname, numfmt )
end
-- always include AVERAGE
- _tif( _args, "GPRINT:%s_avg:AVERAGE:%s Avg", source.sname, numfmt )
+ _tif( _args, "GPRINT:%s_avg:AVERAGE:\tAvg\\: %s", source.sname, numfmt )
-- don't include MAX if rrasingle is enabled
if not self.opts.rrasingle then
- _tif( _args, "GPRINT:%s_max:MAX:%s Max", source.sname, numfmt )
+ _tif( _args, "GPRINT:%s_max:MAX:\tMax\\: %s", source.sname, numfmt )
end
-- include total count if requested else include LAST
if source.total then
_tif( _args, "GPRINT:%s_avg_sum:LAST:(ca. %s Total)\\l", source.sname, totfmt )
else
- _tif( _args, "GPRINT:%s_avg:LAST:%s Last\\l", source.sname, numfmt )
+ _tif( _args, "GPRINT:%s_avg:LAST:\tLast\\: %s\\l", source.sname, numfmt )
end
end
local dsname = dtype .. "_" .. dinst:gsub("[^%w]","_") .. "_" .. dsource
local altname = dtype .. "__" .. dsource
+ --assert(dtype ~= "ping", dsname .. " or " .. altname)
+
-- find datasource options
local dopts = { }
flip = dopts.flip or false,
total = dopts.total or false,
overlay = dopts.overlay or false,
+ transform_rpn = dopts.transform_rpn or "0,+",
noarea = dopts.noarea or false,
+ title = dopts.title or nil,
ds = dsource,
type = dtype,
instance = dinst,
-- store title and vlabel
_ti( _args, "-t" )
- _ti( _args, opts.title or self.i18n:title( plugin, plugin_instance, _sources[1].type, instance ) )
+ _ti( _args, self.i18n:title( plugin, plugin_instance, _sources[1].type, instance, opts.title ) )
_ti( _args, "-v" )
- _ti( _args, opts.vlabel or self.i18n:label( plugin, plugin_instance, _sources[1].type, instance ) )
+ _ti( _args, self.i18n:label( plugin, plugin_instance, _sources[1].type, instance, opts.vlabel ) )
-- store additional rrd options
if opts.rrdopts then
return defs
end
-function Graph.render( self, plugin, plugin_instance )
+function Graph.render( self, plugin, plugin_instance, is_index )
dtype_instances = dtype_instances or { "" }
local pngs = { }
-- check for a whole graph handler
local plugin_def = "luci.statistics.rrdtool.definitions." .. plugin
- local stat, def = luci.util.copcall( require, plugin_def )
+ local stat, def = pcall( require, plugin_def )
if stat and def and type(def.rrdargs) == "function" then
local _images = { }
-- get diagram definitions
- for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance ) ) ) do
+ for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance, nil, is_index ) ) ) do
+ if not is_index or not opts.detail then
+ _images[i] = { }
- _images[i] = { }
+ -- get diagram definition instances
+ local diagrams = self:_generic( opts, plugin, plugin_instance, nil, i )
- -- get diagram definition instances
- local diagrams = self:_generic( opts, plugin, plugin_instance, nil, i )
+ -- render all diagrams
+ for j, def in ipairs( diagrams ) do
+ -- remember image
+ _images[i][j] = def[1]
- -- render all diagrams
- for j, def in ipairs( diagrams ) do
-
- -- remember image
- _images[i][j] = def[1]
-
- -- exec
- self:_rrdtool( def )
+ -- exec
+ self:_rrdtool( def )
+ end
end
end
table.insert( pngs, _images[x][y] )
end
end
- else
-
- -- no graph handler, iterate over data types
- for i, dtype in ipairs( self.tree:data_types( plugin, plugin_instance ) ) do
-
- -- check for data type handler
- local dtype_def = plugin_def .. "." .. dtype
- local stat, def = luci.util.copcall( require, dtype_def )
-
- if stat and def and type(def.rrdargs) == "function" then
-
- -- temporary image matrix
- local _images = { }
-
- -- get diagram definitions
- for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance, dtype ) ) ) do
-
- _images[i] = { }
-
- -- get diagram definition instances
- local diagrams = self:_generic( opts, plugin, plugin_instance, dtype, i )
-
- -- render all diagrams
- for j, def in ipairs( diagrams ) do
-
- -- remember image
- _images[i][j] = def[1]
-
- -- exec
- self:_rrdtool( def )
- end
- end
-
- -- remember images - XXX: fixme (will cause probs with asymmetric data)
- for y = 1, #_images[1] do
- for x = 1, #_images do
- table.insert( pngs, _images[x][y] )
- end
- end
- else
-
- -- no data type handler, fall back to builtin definition
- if type(self.defs.definitions[dtype]) == "table" then
-
- -- iterate over data type instances
- for i, inst in ipairs( self.tree:data_instances( plugin, plugin_instance, dtype ) ) do
-
- local title = self.i18n:title( plugin, plugin_instance, dtype, inst )
- local label = self.i18n:label( plugin, plugin_instance, dtype, inst )
- local png = self:mkpngpath( plugin, plugin_instance, dtype, inst )
- local rrd = self:mkrrdpath( plugin, plugin_instance, dtype, inst )
- local args = { png, "-t", title, "-v", label }
-
- for i, o in ipairs(self.defs.definitions[dtype]) do
- -- XXX: this is a somewhat ugly hack to exclude min/max RRAs when rrasingle is on
- if not ( self.opts.rrasingle and ( o:match("_min") or o:match("_max") ) ) then
- table.insert( args, o )
- end
- end
-
- -- remember image
- table.insert( pngs, png )
-
- -- exec
- self:_rrdtool( args, rrd )
- end
- end
- end
- end
end
return pngs