1 module("luci.statistics.rrdtool", package.seeall)
3 require("luci.statistics.datatree")
4 require("luci.statistics.rrdtool.colors")
5 require("luci.statistics.rrdtool.definitions")
11 Graph = luci.util.class()
13 function Graph.__init__( self, timespan, opts )
16 opts.width = opts.width or "400"
18 self.colors = luci.statistics.rrdtool.colors.Instance()
19 self.defs = luci.statistics.rrdtool.definitions.Instance()
20 self.tree = luci.statistics.datatree.Instance()
22 -- rrdtool defalt args
25 "-s", "NOW-" .. ( timespan or 900 ),
30 function Graph.mktitle( self, host, plugin, plugin_instance, dtype, dtype_instance )
31 local t = host .. "/" .. plugin
32 if type(plugin_instance) == "string" and plugin_instance:len() > 0 then
33 t = t .. "-" .. plugin_instance
36 if type(dtype_instance) == "string" and dtype_instance:len() > 0 then
37 t = t .. "-" .. dtype_instance
42 function Graph.mkrrdpath( self, ... )
43 return string.format( "/tmp/%s.rrd", self:mktitle( ... ) )
46 function Graph.mkpngpath( self, ... )
47 return string.format( "/tmp/rrdimg/%s.png", self:mktitle( ... ) )
50 function Graph._push( self, elem )
52 if type(elem) == "string" then
53 table.insert( self.args, elem )
55 for i, item in ipairs(elem) do
56 table.insert( self.args, item )
63 function Graph._clearargs( self )
64 for i = #self.args, 7, -1 do
65 table.remove( self.args, i )
69 function Graph._forcelol( self, list )
70 if type(list[1]) ~= "table" then
76 function Graph._rrdtool( self, png, rrd )
79 local dir = png:gsub("/[^/]+$","")
80 luci.fs.mkdir( dir, true )
82 -- construct commandline
83 local cmdline = "rrdtool graph " .. png
85 for i, opt in ipairs(self.args) do
87 opt = opt .. "" -- force string
90 opt = opt:gsub( "{file}", rrd )
93 if opt:match("[^%w]") then
94 cmdline = cmdline .. " '" .. opt .. "'"
96 cmdline = cmdline .. " " .. opt
101 local rrdtool = io.popen( cmdline )
105 function Graph._generic( self, opts )
110 table.insert( images, opts.image )
112 -- insert provided addition rrd options
113 self:_push( { "-t", opts.title or "Unknown title" } )
114 self:_push( opts.rrd )
116 -- construct an array of safe instance names
117 local inst_names = { }
118 for i, source in ipairs(opts.sources) do
119 inst_names[i] = i .. source.name:gsub("[^A-Za-z0-9%-_]","_")
122 -- create DEF statements for each instance, find longest instance name
123 local longest_name = 0
124 for i, source in ipairs(opts.sources) do
125 if source.name:len() > longest_name then
126 longest_name = source.name:len()
129 local ds = source.ds or "value"
131 self:_push( "DEF:" .. inst_names[i] .. "_min=" ..source.rrd .. ":" .. ds .. ":MIN" )
132 self:_push( "DEF:" .. inst_names[i] .. "_avg=" ..source.rrd .. ":" .. ds .. ":AVERAGE" )
133 self:_push( "DEF:" .. inst_names[i] .. "_max=" ..source.rrd .. ":" .. ds .. ":MAX" )
134 self:_push( "CDEF:" .. inst_names[i] .. "_nnl=" .. inst_names[i] .. "_avg,UN,0," .. inst_names[i] .. "_avg,IF" )
137 -- create CDEF statement for last instance name
138 self:_push( "CDEF:" .. inst_names[#inst_names] .. "_stk=" .. inst_names[#inst_names] .. "_nnl" )
140 -- create CDEF statements for each instance
141 for i, source in ipairs(inst_names) do
145 inst_names[1 + #inst_names - i] .. "_stk=" ..
146 inst_names[1 + #inst_names - i] .. "_nnl," ..
147 inst_names[2 + #inst_names - i] .. "_stk,+"
152 -- create LINE and GPRINT statements for each instance
153 for i, source in ipairs(opts.sources) do
155 local legend = string.format(
156 "%-" .. longest_name .. "s",
160 local numfmt = opts.number_format or "%6.1lf"
165 -- find color: try source, then opts.colors; fall back to random color
166 if type(source.color) == "string" then
167 line_color = source.color
168 area_color = self.colors:from_string( line_color )
169 elseif type(opts.colors[source.name:gsub("[^%w]","_")]) == "string" then
170 line_color = opts.colors[source.name:gsub("[^%w]","_")]
171 area_color = self.colors:from_string( line_color )
173 area_color = self.colors:random()
174 line_color = self.colors:to_string( area_color )
177 -- derive area background color from line color
178 area_color = self.colors:to_string( self.colors:faded( area_color ) )
181 self:_push( "AREA:" .. inst_names[i] .. "_stk#" .. area_color )
182 self:_push( "LINE1:" .. inst_names[i] .. "_stk#" .. line_color .. ":" .. legend )
183 self:_push( "GPRINT:" .. inst_names[i] .. "_min:MIN:" .. numfmt .. " Min" )
184 self:_push( "GPRINT:" .. inst_names[i] .. "_avg:AVERAGE:" .. numfmt .. " Avg" )
185 self:_push( "GPRINT:" .. inst_names[i] .. "_max:MAX:" .. numfmt .. " Max" )
186 self:_push( "GPRINT:" .. inst_names[i] .. "_avg:LAST:" .. numfmt .. " Last\\l" )
192 function Graph.render( self, host, plugin, plugin_instance )
194 dtype_instances = dtype_instances or { "" }
197 -- check for a whole graph handler
198 local plugin_def = "luci.statistics.rrdtool.definitions." .. plugin
199 local stat, def = pcall( require, plugin_def )
201 if stat and def and type(def.rrdargs) == "function" then
202 for i, opts in ipairs( self:_forcelol( def.rrdargs( self, host, plugin, plugin_instance, dtype ) ) ) do
203 for i, png in ipairs( self:_generic( opts ) ) do
204 table.insert( pngs, png )
215 -- no graph handler, iterate over data types
216 for i, dtype in ipairs( self.tree:data_types( plugin, plugin_instance ) ) do
218 -- check for data type handler
219 local dtype_def = plugin_def .. "." .. dtype
220 local stat, def = pcall( require, dtype_def )
222 if stat and def and type(def.rrdargs) == "function" then
223 for i, opts in ipairs( self:_forcelol( def.rrdargs( self, host, plugin, plugin_instance, dtype ) ) ) do
224 for i, png in ipairs( self:_generic( opts ) ) do
225 table.insert( pngs, png )
236 -- no data type handler, fall back to builtin definition
237 if type(self.defs.definitions[dtype]) == "table" then
239 -- iterate over data type instances
240 for i, inst in ipairs( self.tree:data_instances( plugin, plugin_instance, dtype ) ) do
242 local title = self:mktitle( host, plugin, plugin_instance, dtype, inst )
243 local png = self:mkpngpath( host, plugin, plugin_instance, dtype, inst )
244 local rrd = self:mkrrdpath( host, plugin, plugin_instance, dtype, inst )
246 self:_push( { "-t", title } )
247 self:_push( self.defs.definitions[dtype] )
249 table.insert( pngs, png )
252 self:_rrdtool( png, rrd )