luci-statistics: add transform_rpn option to graphs, use it for nut battery life
[project/luci.git] / applications / luci-statistics / luasrc / statistics / rrdtool.lua
index b399866..e93ac06 100644 (file)
@@ -1,11 +1,28 @@
+--[[
+
+Luci statistics - rrdtool interface library / diagram model interpreter
+(c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+        http://www.apache.org/licenses/LICENSE-2.0
+
+$Id$
+
+]]--
+
 module("luci.statistics.rrdtool", package.seeall)
 
 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.bits")
-require("luci.fs")
+require("luci.sys")
+
+local fs = require "nixio.fs"
 
 
 Graph = luci.util.class()
@@ -13,22 +30,38 @@ Graph = luci.util.class()
 function Graph.__init__( self, timespan, opts )
 
        opts = opts or { }
-       opts.width = opts.width or "400"
 
+       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 == "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.imgpath   = opts.imgpath   or sections.rrdtool.image_path       or "/tmp/rrdimg"
+       opts.rrdpath   = opts.rrdpath:gsub("/$","")
+       opts.imgpath   = opts.imgpath:gsub("/$","")
+
+       -- helper classes
        self.colors = luci.statistics.rrdtool.colors.Instance()
-       self.defs   = luci.statistics.rrdtool.definitions.Instance()
-       self.tree   = luci.statistics.datatree.Instance()
+       self.tree   = luci.statistics.datatree.Instance(opts.host)
+       self.i18n   = luci.statistics.i18n.Instance( self )
 
-       -- rrdtool defalt args
-       self.args   = {
+       -- rrdtool default args
+       self.args = {
                "-a", "PNG",
-               "-s", "NOW-" .. ( timespan or 900 ),
+               "-s", "NOW-" .. opts.timespan,
                "-w", opts.width
        }
+
+       -- store options
+       self.opts = opts
 end
 
-function Graph.mktitle( self, host, plugin, plugin_instance, dtype, dtype_instance )
-       local t = host .. "/" .. plugin
+function Graph._mkpath( self, plugin, plugin_instance, dtype, dtype_instance )
+       local t = self.opts.host .. "/" .. plugin
        if type(plugin_instance) == "string" and plugin_instance:len() > 0 then
                t = t .. "-" .. plugin_instance
        end
@@ -40,30 +73,15 @@ function Graph.mktitle( self, host, plugin, plugin_instance, dtype, dtype_instan
 end
 
 function Graph.mkrrdpath( self, ... )
-       return string.format( "/tmp/%s.rrd", self:mktitle( ... ) )
+       return string.format( "%s/%s.rrd", self.opts.rrdpath, self:_mkpath( ... ) )
 end
 
 function Graph.mkpngpath( self, ... )
-       return string.format( "/tmp/rrdimg/%s.png", self:mktitle( ... ) )
+       return string.format( "%s/%s.%i.png", self.opts.imgpath, self:_mkpath( ... ), self.opts.timespan )
 end
 
-function Graph._push( self, elem )
-
-       if type(elem) == "string" then
-               table.insert( self.args, elem )
-       else
-               for i, item in ipairs(elem) do
-                       table.insert( self.args, item )
-               end
-       end
-
-       return( self.args )
-end
-
-function Graph._clearargs( self )
-       for i = #self.args, 7, -1 do
-               table.remove( self.args, i )
-       end
+function Graph.strippngpath( self, path )
+       return path:sub( self.opts.imgpath:len() + 2 )
 end
 
 function Graph._forcelol( self, list )
@@ -73,19 +91,24 @@ function Graph._forcelol( self, list )
        return( list )
 end
 
-function Graph._rrdtool( self, png, rrd )
+function Graph._rrdtool( self, def, rrd )
 
        -- prepare directory
-       local dir = png:gsub("/[^/]+$","")
-       luci.fs.mkdir( dir, true )
+       local dir = def[1]:gsub("/[^/]+$","")
+       fs.mkdirr( dir )
 
        -- construct commandline
-       local cmdline = "rrdtool graph " .. png
+       local cmdline = "rrdtool graph"
 
+       -- copy default arguments to def stack
        for i, opt in ipairs(self.args) do
+               table.insert( def, 1 + i, opt )
+       end
 
+       -- construct commandline from def stack
+       for i, opt in ipairs(def) do
                opt = opt .. ""    -- force string
-               
+
                if rrd then
                        opt = opt:gsub( "{file}", rrd )
                end
@@ -102,67 +125,126 @@ function Graph._rrdtool( self, png, rrd )
        rrdtool:close()
 end
 
-function Graph._generic( self, opts )
+function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
 
-       local images = { }
+       -- generated graph defs
+       local defs = { }
 
-       -- remember images
-       table.insert( images, opts.image )
+       -- internal state variables
+       local _args         = { }
+       local _sources      = { }
+       local _stack_neg    = { }
+       local _stack_pos    = { }
+       local _longest_name = 0
+       local _has_totals   = false
 
-       -- insert provided addition rrd options
-       self:_push( { "-t", opts.title or "Unknown title" } )
-       self:_push( opts.rrd )
+       -- some convenient aliases
+       local _ti           = table.insert
+       local _sf           = string.format
 
-       -- construct an array of safe instance names
-       local inst_names = { }
-       for i, source in ipairs(opts.sources) do
-               inst_names[i] = i .. source.name:gsub("[^A-Za-z0-9%-_]","_")
+       -- local helper: append a string.format() formatted string to given table
+       function _tif( list, fmt, ... )
+               table.insert( list, string.format( fmt, ... ) )
        end
 
-       -- create DEF statements for each instance, find longest instance name
-       local longest_name = 0
-       for i, source in ipairs(opts.sources) do
-               if source.name:len() > longest_name then
-                       longest_name = source.name:len()
-               end
+       -- local helper: create definitions for min, max, avg and create *_nnl (not null) variable from avg
+       function __def(source)
+
+               local inst = source.sname
+               local rrd  = source.rrd
+               local ds   = source.ds
 
-               local ds = source.ds or "value"
+               if not ds or ds:len() == 0 then ds = "value" end
 
-               self:_push( "DEF:" .. inst_names[i] .. "_min=" ..source.rrd .. ":" .. ds .. ":MIN" )
-               self:_push( "DEF:" .. inst_names[i] .. "_avg=" ..source.rrd .. ":" .. ds .. ":AVERAGE" )
-               self:_push( "DEF:" .. inst_names[i] .. "_max=" ..source.rrd .. ":" .. ds .. ":MAX" )
-               self:_push( "CDEF:" .. inst_names[i] .. "_nnl=" .. inst_names[i] .. "_avg,UN,0," .. inst_names[i] .. "_avg,IF" )
+               _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_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 )
        end
 
-       -- create CDEF statement for last instance name
-       self:_push( "CDEF:" .. inst_names[#inst_names] .. "_stk=" .. inst_names[#inst_names] .. "_nnl" )
-
-       -- create CDEF statements for each instance
-       for i, source in ipairs(inst_names) do
-               if i > 1 then
-                       self:_push(
-                               "CDEF:" ..
-                               inst_names[1 + #inst_names - i] .. "_stk=" ..
-                               inst_names[1 + #inst_names - i] .. "_nnl," ..
-                               inst_names[2 + #inst_names - i] .. "_stk,+"
+       -- local helper: create cdefs depending on source options like flip and overlay
+       function __cdef(source)
+
+               local prev
+
+               -- find previous source, choose stack depending on flip state
+               if source.flip then
+                       prev = _stack_neg[#_stack_neg]
+               else
+                       prev = _stack_pos[#_stack_pos]
+               end
+
+               -- is first source in stack or overlay source: source_stk = source_nnl
+               if not prev or source.overlay then
+                       -- 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_plot,-1,*", source.sname, source.sname )
+
+                       -- push to negative stack if overlay is disabled
+                       if not source.overlay then
+                               _ti( _stack_neg, source.sname )
+                       end
+
+               -- no flipping, push to positive stack if overlay is disabled
+               elseif not source.overlay then
+
+                       -- push to positive stack
+                       _ti( _stack_pos, source.sname )
+               end
+
+               -- calculate total amount of data if requested
+               if source.total then
+                       _tif( _args,
+                               "CDEF:%s_avg_sample=%s_avg,UN,0,%s_avg,IF,sample_len,*",
+                               source.sname, source.sname, source.sname
+                       )
+
+                       _tif( _args,
+                               "CDEF:%s_avg_sum=PREV,UN,0,PREV,IF,%s_avg_sample,+",
+                               source.sname, source.sname, source.sname
                        )
                end
        end
 
-       -- create LINE and GPRINT statements for each instance
-       for i, source in ipairs(opts.sources) do
-
-               local legend = string.format(
-                       "%-" .. longest_name .. "s",
-                       source.name
-               )
+       -- local helper: create cdefs required for calculating total values
+       function __cdef_totals()
+               if _has_totals then
+                       _tif( _args, "CDEF:mytime=%s_avg,TIME,TIME,IF", _sources[1].sname    )
+                       _ti(  _args, "CDEF:sample_len_raw=mytime,PREV(mytime),-"             )
+                       _ti(  _args, "CDEF:sample_len=sample_len_raw,UN,0,sample_len_raw,IF" )
+               end
+       end
 
-               local numfmt = opts.number_format or "%6.1lf"
+       -- local helper: create line and area statements
+       function __line(source)
 
                local line_color
                local area_color
+               local legend
+               local var
 
-               -- find color: try source, then opts.colors; fall back to random color
+               -- find colors: try source, then opts.colors; fall back to random color
                if type(source.color) == "string" then
                        line_color = source.color
                        area_color = self.colors:from_string( line_color )
@@ -177,88 +259,291 @@ function Graph._generic( self, opts )
                -- derive area background color from line color
                area_color = self.colors:to_string( self.colors:faded( area_color ) )
 
+               -- choose source_plot or source_neg variable depending on flip state
+               if source.flip then
+                       var = "neg"
+               else
+                       var = "plot"
+               end
+
+               -- create legend
+               legend = _sf( "%-" .. _longest_name .. "s", source.title )
+
+               -- create area if not disabled
+               if not source.noarea then
+                       _tif( _args, "AREA:%s_%s#%s", source.sname, var, area_color )
+               end
 
-               self:_push( "AREA:"   .. inst_names[i] .. "_stk#" .. area_color )
-               self:_push( "LINE1:"  .. inst_names[i] .. "_stk#" .. line_color .. ":" .. legend )
-               self:_push( "GPRINT:" .. inst_names[i] .. "_min:MIN:" .. numfmt .. " Min" )
-               self:_push( "GPRINT:" .. inst_names[i] .. "_avg:AVERAGE:" .. numfmt .. " Avg" )
-               self:_push( "GPRINT:" .. inst_names[i] .. "_max:MAX:" .. numfmt .. " Max" )
-               self:_push( "GPRINT:" .. inst_names[i] .. "_avg:LAST:" .. numfmt .. " Last\\l" )
+               -- create line1 statement
+               _tif( _args, "LINE%d:%s_%s#%s:%s",
+                       source.noarea and 2 or 1,
+                       source.sname, var, line_color, legend )
        end
 
-       return images
-end
+       -- local helper: create gprint statements
+       function __gprint(source)
 
-function Graph.render( self, host, plugin, plugin_instance )
+               local numfmt = opts.number_format or "%6.1lf"
+               local totfmt = opts.totals_format or "%5.1lf%s"
 
-       dtype_instances = dtype_instances or { "" }
-       local pngs = { }
+               -- don't include MIN if rrasingle is enabled
+               if not self.opts.rrasingle then
+                       _tif( _args, "GPRINT:%s_min:MIN:\tMin\\: %s", source.sname, numfmt )
+               end
 
-       -- check for a whole graph handler
-       local plugin_def = "luci.statistics.rrdtool.definitions." .. plugin
-       local stat, def = pcall( require, plugin_def )
+               -- always include AVERAGE
+               _tif( _args, "GPRINT:%s_avg:AVERAGE:\tAvg\\: %s", source.sname, numfmt )
 
-       if stat and def and type(def.rrdargs) == "function" then
-               for i, opts in ipairs( self:_forcelol( def.rrdargs( self, host, plugin, plugin_instance, dtype ) ) ) do
-                       for i, png in ipairs( self:_generic( opts ) ) do
-                               table.insert( pngs, png )
+               -- don't include MAX if rrasingle is enabled
+               if not self.opts.rrasingle then
+                       _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:\tLast\\: %s\\l", source.sname, numfmt )
+               end
+       end
+
+
+       --
+       -- find all data sources
+       --
 
-                               -- exec
-                               self:_rrdtool( png )
+       -- find data types
+       local data_types
 
-                               -- clear args
-                               self:_clearargs()
+       if dtype then
+               data_types = { dtype }
+       else
+               data_types = opts.data.types or { }
+       end
+
+       if not ( dtype or opts.data.types ) then
+               if opts.data.instances then
+                       for k, v in pairs(opts.data.instances) do
+                               _ti( data_types, k )
+                       end
+               elseif opts.data.sources then
+                       for k, v in pairs(opts.data.sources) do
+                               _ti( data_types, k )
                        end
                end
-       else
+       end
 
-               -- 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 = pcall( require, dtype_def )
+       -- iterate over data types
+       for i, dtype in ipairs(data_types) do
 
-                       if stat and def and type(def.rrdargs) == "function" then
-                               for i, opts in ipairs( self:_forcelol( def.rrdargs( self, host, plugin, plugin_instance, dtype ) ) ) do
-                                       for i, png in ipairs( self:_generic( opts ) ) do
-                                               table.insert( pngs, png )
+               -- find instances
 
-                                               -- exec
-                                               self:_rrdtool( png )
+               local data_instances
 
-                                               -- clear args
-                                               self:_clearargs()
-                                       end
-                               end
+               if not opts.per_instance then
+                       if type(opts.data.instances) == "table" and type(opts.data.instances[dtype]) == "table" then
+                               data_instances = opts.data.instances[dtype]
                        else
+                               data_instances = self.tree:data_instances( plugin, plugin_instance, dtype )
+                       end
+               end
+
+               if type(data_instances) ~= "table" or #data_instances == 0 then data_instances = { "" } end
 
-                               -- 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
+               -- iterate over data instances
+               for i, dinst in ipairs(data_instances) do
 
-                                               local title = self:mktitle( host, plugin, plugin_instance, dtype, inst )
-                                               local png   = self:mkpngpath( host, plugin, plugin_instance, dtype, inst )
-                                               local rrd   = self:mkrrdpath( host, plugin, plugin_instance, dtype, inst )
+                       -- construct combined data type / instance name
+                       local dname = dtype
 
-                                               self:_push( { "-t", title } )
-                                               self:_push( self.defs.definitions[dtype] )
+                       if dinst:len() > 0 then
+                               dname = dname .. "_" .. dinst
+                       end
+
+
+                       -- find sources
+                       local data_sources = { "value" }
 
-                                               table.insert( pngs, png )
+                       if type(opts.data.sources) == "table" then
+                               if type(opts.data.sources[dname]) == "table" then
+                                       data_sources = opts.data.sources[dname]
+                               elseif type(opts.data.sources[dtype]) == "table" then
+                                       data_sources = opts.data.sources[dtype]
+                               end
+                       end
 
-                                               -- exec
-                                               self:_rrdtool( png, rrd )
 
-                                               -- clear args
-                                               self:_clearargs()
+                       -- iterate over data sources
+                       for i, dsource in ipairs(data_sources) do
+
+                               local dsname  = dtype .. "_" .. dinst:gsub("[^%w]","_") .. "_" .. dsource
+                               local altname = dtype .. "__" .. dsource
+
+                               --assert(dtype ~= "ping", dsname .. " or " .. altname)
+
+                               -- find datasource options
+                               local dopts = { }
+
+                               if type(opts.data.options) == "table" then
+                                       if type(opts.data.options[dsname]) == "table" then
+                                               dopts = opts.data.options[dsname]
+                                       elseif type(opts.data.options[altname]) == "table" then
+                                               dopts = opts.data.options[altname]
+                                       elseif type(opts.data.options[dname]) == "table" then
+                                               dopts = opts.data.options[dname]
+                                       elseif type(opts.data.options[dtype]) == "table" then
+                                               dopts = opts.data.options[dtype]
                                        end
                                end
+
+
+                               -- store values
+                               _ti( _sources, {
+                                       rrd      = dopts.rrd     or self:mkrrdpath( plugin, plugin_instance, dtype, dinst ),
+                                       color    = dopts.color   or self.colors:to_string( self.colors:random() ),
+                                       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,
+                                       index    = #_sources + 1,
+                                       sname    = ( #_sources + 1 ) .. dtype
+                               } )
+
+
+                               -- generate datasource title
+                               _sources[#_sources].title = self.i18n:ds( _sources[#_sources] )
+
+
+                               -- find longest name ...
+                               if _sources[#_sources].title:len() > _longest_name then
+                                       _longest_name = _sources[#_sources].title:len()
+                               end
+
+
+                               -- has totals?
+                               if _sources[#_sources].total then
+                                       _has_totals = true
+                               end
                        end
                end
        end
 
-       return pngs
+
+       --
+       -- construct diagrams
+       --
+
+       -- if per_instance is enabled then find all instances from the first datasource in diagram
+       -- if per_instance is disabled then use an empty pseudo instance and use model provided values
+       local instances = { "" }
+
+       if opts.per_instance then
+               instances = self.tree:data_instances( plugin, plugin_instance, _sources[1].type )
+       end
+
+
+       -- iterate over instances
+       for i, instance in ipairs(instances) do
+
+               -- store title and vlabel
+               _ti( _args, "-t" )
+               _ti( _args, self.i18n:title( plugin, plugin_instance, _sources[1].type, instance, opts.title ) )
+               _ti( _args, "-v" )
+               _ti( _args, self.i18n:label( plugin, plugin_instance, _sources[1].type, instance, opts.vlabel ) )
+
+               -- store additional rrd options
+               if opts.rrdopts then
+                       for i, o in ipairs(opts.rrdopts) do _ti( _args, o ) end
+               end
+
+
+               -- create DEF statements for each instance
+               for i, source in ipairs(_sources) do
+                       -- fixup properties for per instance mode...
+                       if opts.per_instance then
+                               source.instance = instance
+                               source.rrd      = self:mkrrdpath( plugin, plugin_instance, source.type, instance )
+                       end
+
+                       __def( source )
+               end
+
+               -- create CDEF required for calculating totals
+               __cdef_totals()
+
+               -- create CDEF statements for each instance in reversed order
+               for i, source in ipairs(_sources) do
+                       __cdef( _sources[1 + #_sources - i] )
+               end
+
+               -- create LINE1, AREA and GPRINT statements for each instance
+               for i, source in ipairs(_sources) do
+                       __line( source )
+                       __gprint( source )
+               end
+
+               -- prepend image path to arg stack
+               _ti( _args, 1, self:mkpngpath( plugin, plugin_instance, index .. instance ) )
+
+               -- push arg stack to definition list
+               _ti( defs, _args )
+
+               -- reset stacks
+               _args         = { }
+               _stack_pos    = { }
+               _stack_neg    = { }
+       end
+
+       return defs
 end
 
+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 = pcall( require, plugin_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, nil, is_index ) ) ) do
+                       if not is_index or not opts.detail then
+                               _images[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]
+
+                                       -- exec
+                                       self:_rrdtool( def )
+                               end
+                       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
+       end
+
+       return pngs
+end