+--[[
+
+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.fs")
+require("luci.sys")
+
+local fs = require "nixio.fs"
Graph = luci.util.class()
opts = opts or { }
- 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.rrasingle = opts.rrasingle or true -- XXX: fixme (uci)
- opts.host = opts.host or "OpenWrt" -- XXX: fixme (uci)
- opts.timespan = opts.timespan or 900 -- XXX: fixme (uci)
- opts.width = opts.width or 400 -- XXX: fixme (uci)
+ 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.tree = luci.statistics.datatree.Instance(opts.host)
+ self.i18n = luci.statistics.i18n.Instance( self )
-- rrdtool default args
self.args = {
end
function Graph.mkrrdpath( self, ... )
- return string.format( "/tmp/%s.rrd", self:_mkpath( ... ) )
+ return string.format( "%s/%s.rrd", self.opts.rrdpath, self:_mkpath( ... ) )
end
function Graph.mkpngpath( self, ... )
- return string.format( "/tmp/rrdimg/%s.png", self:_mkpath( ... ) )
+ return string.format( "%s/%s.%i.png", self.opts.imgpath, self:_mkpath( ... ), self.opts.timespan )
end
-function Graph.mktitle( self, plugin, plugin_instance, dtype, dtype_instance )
-
- -- try various strings to retrieve a diagram title from the langfile
- return "XXX"
+function Graph.strippngpath( self, path )
+ return path:sub( self.opts.imgpath:len() + 2 )
end
function Graph._forcelol( self, list )
-- prepare directory
local dir = def[1]:gsub("/[^/]+$","")
- luci.fs.mkdir( dir, true )
+ fs.mkdirr( dir )
-- construct commandline
local cmdline = "rrdtool graph"
-- 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
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
+ -- 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,
for i, instance in ipairs(instances) do
-- store title and vlabel
- -- XXX: i18n
_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 = { }
local _images = { }
-- get diagram definitions
- for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance ) ) ) do
-
- _images[i] = { }
-
- -- get diagram definition instances
- local diagrams = self:_generic( opts, plugin, plugin_instance, nil, i )
+ 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] = { }
- -- render all diagrams
- for j, def in ipairs( diagrams ) do
+ -- get diagram definition instances
+ local diagrams = self:_generic( opts, plugin, plugin_instance, nil, i )
- -- 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 = pcall( 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:mktitle( 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 }
-
- for i, o in ipairs(self.defs.definitions[dtype]) do
- table.insert( args, o )
- end
-
- -- remember image
- table.insert( pngs, png )
-
- -- exec
- self:_rrdtool( args, rrd )
- end
- end
- end
- end
end
return pngs