* Merged Luci to use native UCI-library
[project/luci.git] / applications / luci-statistics / luasrc / statistics / rrdtool.lua
1 --[[
2
3 Luci statistics - rrdtool interface library / diagram model interpreter
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
16 module("luci.statistics.rrdtool", package.seeall)
17
18 require("luci.statistics.datatree")
19 require("luci.statistics.rrdtool.colors")
20 require("luci.statistics.rrdtool.definitions")
21 require("luci.statistics.i18n")
22 require("luci.model.uci")
23 require("luci.util")
24 require("luci.sys")
25 require("luci.fs")
26
27
28 Graph = luci.util.class()
29
30 function Graph.__init__( self, timespan, opts )
31
32         opts = opts or { }
33
34         local uci = luci.model.uci
35         local sections = uci.get_all( "luci_statistics" )
36
37         -- helper classes
38         self.colors = luci.statistics.rrdtool.colors.Instance()
39         self.defs   = luci.statistics.rrdtool.definitions.Instance()
40         self.tree   = luci.statistics.datatree.Instance()
41         self.i18n   = luci.statistics.i18n.Instance( self )
42
43         -- options
44         opts.timespan  = timespan       or sections.rrdtool.default_timespan or 900
45         opts.rrasingle = opts.rrasingle or ( sections.collectd_rrdtool.RRASingle ~= "0" )
46         opts.host      = opts.host      or sections.collectd.Hostname        or luci.sys.hostname()
47         opts.width     = opts.width     or sections.rrdtool.image_width      or 400
48         opts.rrdpath   = opts.rrdpath   or sections.collectd_rrdtool.DataDir or "/tmp/rrd"
49         opts.imgpath   = opts.imgpath   or sections.rrdtool.image_path       or "/tmp/rrdimg"
50         opts.rrdpath   = opts.rrdpath:gsub("/$","")
51         opts.imgpath   = opts.imgpath:gsub("/$","")
52
53         -- rrdtool default args
54         self.args = {
55                 "-a", "PNG",
56                 "-s", "NOW-" .. opts.timespan,
57                 "-w", opts.width
58         }
59
60         -- store options
61         self.opts = opts
62 end
63
64 function Graph._mkpath( self, plugin, plugin_instance, dtype, dtype_instance )
65         local t = self.opts.host .. "/" .. plugin
66         if type(plugin_instance) == "string" and plugin_instance:len() > 0 then
67                 t = t .. "-" .. plugin_instance
68         end
69         t = t .. "/" .. dtype
70         if type(dtype_instance) == "string" and dtype_instance:len() > 0 then
71                 t = t .. "-" .. dtype_instance
72         end
73         return t
74 end
75
76 function Graph.mkrrdpath( self, ... )
77         return string.format( "%s/%s.rrd", self.opts.rrdpath, self:_mkpath( ... ) )
78 end
79
80 function Graph.mkpngpath( self, ... )
81         return string.format( "%s/%s.%i.png", self.opts.imgpath, self:_mkpath( ... ), self.opts.timespan )
82 end
83
84 function Graph.strippngpath( self, path )
85         return path:sub( self.opts.imgpath:len() + 2 )
86 end
87
88 function Graph._forcelol( self, list )
89         if type(list[1]) ~= "table" then
90                 return( { list } )
91         end
92         return( list )
93 end
94
95 function Graph._rrdtool( self, def, rrd )
96
97         -- prepare directory
98         local dir = def[1]:gsub("/[^/]+$","")
99         luci.fs.mkdir( dir, true )
100
101         -- construct commandline
102         local cmdline = "rrdtool graph"
103
104         -- copy default arguments to def stack
105         for i, opt in ipairs(self.args) do
106                 table.insert( def, 1 + i, opt )
107         end
108
109         -- construct commandline from def stack
110         for i, opt in ipairs(def) do
111                 opt = opt .. ""    -- force string
112
113                 if rrd then
114                         opt = opt:gsub( "{file}", rrd )
115                 end
116
117                 if opt:match("[^%w]") then
118                         cmdline = cmdline .. " '" .. opt .. "'"
119                 else
120                         cmdline = cmdline .. " " .. opt
121                 end
122         end
123
124         -- execute rrdtool
125         local rrdtool = io.popen( cmdline )
126         rrdtool:close()
127 end
128
129 function Graph._generic( self, opts, plugin, plugin_instance, dtype, index )
130
131         -- generated graph defs
132         local defs = { }
133
134         -- internal state variables
135         local _args         = { }
136         local _sources      = { }
137         local _stack_neg    = { }
138         local _stack_pos    = { }
139         local _longest_name = 0
140         local _has_totals   = false
141
142         -- some convenient aliases
143         local _ti           = table.insert
144         local _sf           = string.format
145
146         -- local helper: append a string.format() formatted string to given table
147         function _tif( list, fmt, ... )
148                 table.insert( list, string.format( fmt, ... ) )
149         end
150
151         -- local helper: create definitions for min, max, avg and create *_nnl (not null) variable from avg
152         function __def(source)
153
154                 local inst = source.sname
155                 local rrd  = source.rrd
156                 local ds   = source.ds
157
158                 if not ds or ds:len() == 0 then ds = "value" end
159
160                 _tif( _args, "DEF:%s_avg=%s:%s:AVERAGE", inst, rrd, ds )
161
162                 if not self.opts.rrasingle then
163                         _tif( _args, "DEF:%s_min=%s:%s:MIN", inst, rrd, ds )
164                         _tif( _args, "DEF:%s_max=%s:%s:MAX", inst, rrd, ds )
165                 end
166
167                 _tif( _args, "CDEF:%s_nnl=%s_avg,UN,0,%s_avg,IF", inst, inst, inst )
168         end
169
170         -- local helper: create cdefs depending on source options like flip and overlay
171         function __cdef(source)
172
173                 local prev
174
175                 -- find previous source, choose stack depending on flip state
176                 if source.flip then
177                         prev = _stack_neg[#_stack_neg]
178                 else
179                         prev = _stack_pos[#_stack_pos]
180                 end
181
182                 -- is first source in stack or overlay source: source_stk = source_nnl
183                 if not prev or source.overlay then
184                         -- create cdef statement
185                         _tif( _args, "CDEF:%s_stk=%s_nnl", source.sname, source.sname )
186
187                 -- is subsequent source without overlay: source_stk = source_nnl + previous_stk
188                 else
189                         -- create cdef statement
190                         _tif( _args, "CDEF:%s_stk=%s_nnl,%s_stk,+", source.sname, source.sname, prev )
191                 end
192
193                 -- create multiply by minus one cdef if flip is enabled
194                 if source.flip then
195
196                         -- create cdef statement: source_stk = source_stk * -1
197                         _tif( _args, "CDEF:%s_neg=%s_stk,-1,*", source.sname, source.sname )
198
199                         -- push to negative stack if overlay is disabled
200                         if not source.overlay then
201                                 _ti( _stack_neg, source.sname )
202                         end
203
204                 -- no flipping, push to positive stack if overlay is disabled
205                 elseif not source.overlay then
206
207                         -- push to positive stack
208                         _ti( _stack_pos, source.sname )
209                 end
210
211                 -- calculate total amount of data if requested
212                 if source.total then
213                         _tif( _args,
214                                 "CDEF:%s_avg_sample=%s_avg,UN,0,%s_avg,IF,sample_len,*",
215                                 source.sname, source.sname, source.sname
216                         )
217
218                         _tif( _args,
219                                 "CDEF:%s_avg_sum=PREV,UN,0,PREV,IF,%s_avg_sample,+",
220                                 source.sname, source.sname, source.sname
221                         )
222                 end
223         end
224
225         -- local helper: create cdefs required for calculating total values
226         function __cdef_totals()
227                 if _has_totals then
228                         _tif( _args, "CDEF:mytime=%s_avg,TIME,TIME,IF", _sources[1].sname    )
229                         _ti(  _args, "CDEF:sample_len_raw=mytime,PREV(mytime),-"             )
230                         _ti(  _args, "CDEF:sample_len=sample_len_raw,UN,0,sample_len_raw,IF" )
231                 end
232         end
233
234         -- local helper: create line and area statements
235         function __line(source)
236
237                 local line_color
238                 local area_color
239                 local legend
240                 local var
241
242                 -- find colors: try source, then opts.colors; fall back to random color
243                 if type(source.color) == "string" then
244                         line_color = source.color
245                         area_color = self.colors:from_string( line_color )
246                 elseif type(opts.colors[source.name:gsub("[^%w]","_")]) == "string" then
247                         line_color = opts.colors[source.name:gsub("[^%w]","_")]
248                         area_color = self.colors:from_string( line_color )
249                 else
250                         area_color = self.colors:random()
251                         line_color = self.colors:to_string( area_color )
252                 end
253
254                 -- derive area background color from line color
255                 area_color = self.colors:to_string( self.colors:faded( area_color ) )
256
257                 -- choose source_stk or source_neg variable depending on flip state
258                 if source.flip then
259                         var = "neg"
260                 else
261                         var = "stk"
262                 end
263
264                 -- create legend
265                 legend = _sf( "%-" .. _longest_name .. "s", source.title )
266
267                 -- create area if not disabled
268                 if not source.noarea then
269                         _tif( _args, "AREA:%s_%s#%s", source.sname, var, area_color )
270                 end
271
272                 -- create line1 statement
273                 _tif( _args, "LINE1:%s_%s#%s:%s", source.sname, var, line_color, legend )
274         end
275
276         -- local helper: create gprint statements
277         function __gprint(source)
278
279                 local numfmt = opts.number_format or "%6.1lf"
280                 local totfmt = opts.totals_format or "%5.1lf%s"
281
282                 -- don't include MIN if rrasingle is enabled
283                 if not self.opts.rrasingle then
284                         _tif( _args, "GPRINT:%s_min:MIN:%s Min", source.sname, numfmt )
285                 end
286
287                 -- always include AVERAGE
288                 _tif( _args, "GPRINT:%s_avg:AVERAGE:%s Avg", source.sname, numfmt )
289
290                 -- don't include MAX if rrasingle is enabled
291                 if not self.opts.rrasingle then
292                         _tif( _args, "GPRINT:%s_max:MAX:%s Max", source.sname, numfmt )
293                 end
294
295                 -- include total count if requested else include LAST
296                 if source.total then
297                         _tif( _args, "GPRINT:%s_avg_sum:LAST:(ca. %s Total)\\l", source.sname, totfmt )
298                 else
299                         _tif( _args, "GPRINT:%s_avg:LAST:%s Last\\l", source.sname, numfmt )
300                 end
301         end
302
303
304         --
305         -- find all data sources
306         --
307
308         -- find data types
309         local data_types
310
311         if dtype then
312                 data_types = { dtype }
313         else
314                 data_types = opts.data.types or { }
315         end
316
317         if not ( dtype or opts.data.types ) then
318                 if opts.data.instances then
319                         for k, v in pairs(opts.data.instances) do
320                                 _ti( data_types, k )
321                         end
322                 elseif opts.data.sources then
323                         for k, v in pairs(opts.data.sources) do
324                                 _ti( data_types, k )
325                         end
326                 end
327         end
328
329
330         -- iterate over data types
331         for i, dtype in ipairs(data_types) do
332
333                 -- find instances
334
335                 local data_instances
336
337                 if not opts.per_instance then
338                         if type(opts.data.instances) == "table" and type(opts.data.instances[dtype]) == "table" then
339                                 data_instances = opts.data.instances[dtype]
340                         else
341                                 data_instances = self.tree:data_instances( plugin, plugin_instance, dtype )
342                         end
343                 end
344
345                 if type(data_instances) ~= "table" or #data_instances == 0 then data_instances = { "" } end
346
347
348                 -- iterate over data instances
349                 for i, dinst in ipairs(data_instances) do
350
351                         -- construct combined data type / instance name
352                         local dname = dtype
353
354                         if dinst:len() > 0 then
355                                 dname = dname .. "_" .. dinst
356                         end
357
358
359                         -- find sources
360                         local data_sources = { "value" }
361
362                         if type(opts.data.sources) == "table" then
363                                 if type(opts.data.sources[dname]) == "table" then
364                                         data_sources = opts.data.sources[dname]
365                                 elseif type(opts.data.sources[dtype]) == "table" then
366                                         data_sources = opts.data.sources[dtype]
367                                 end
368                         end
369
370
371                         -- iterate over data sources
372                         for i, dsource in ipairs(data_sources) do
373
374                                 local dsname  = dtype .. "_" .. dinst:gsub("[^%w]","_") .. "_" .. dsource
375                                 local altname = dtype .. "__" .. dsource
376
377                                 -- find datasource options
378                                 local dopts = { }
379
380                                 if type(opts.data.options) == "table" then
381                                         if type(opts.data.options[dsname]) == "table" then
382                                                 dopts = opts.data.options[dsname]
383                                         elseif type(opts.data.options[altname]) == "table" then
384                                                 dopts = opts.data.options[altname]
385                                         elseif type(opts.data.options[dname]) == "table" then
386                                                 dopts = opts.data.options[dname]
387                                         elseif type(opts.data.options[dtype]) == "table" then
388                                                 dopts = opts.data.options[dtype]
389                                         end
390                                 end
391
392
393                                 -- store values
394                                 _ti( _sources, {
395                                         rrd      = dopts.rrd     or self:mkrrdpath( plugin, plugin_instance, dtype, dinst ),
396                                         color    = dopts.color   or self.colors:to_string( self.colors:random() ),
397                                         flip     = dopts.flip    or false,
398                                         total    = dopts.total   or false,
399                                         overlay  = dopts.overlay or false,
400                                         noarea   = dopts.noarea  or false,
401                                         ds       = dsource,
402                                         type     = dtype,
403                                         instance = dinst,
404                                         index    = #_sources + 1,
405                                         sname    = ( #_sources + 1 ) .. dtype
406                                 } )
407
408
409                                 -- generate datasource title
410                                 _sources[#_sources].title = self.i18n:ds( _sources[#_sources] )
411
412
413                                 -- find longest name ...
414                                 if _sources[#_sources].title:len() > _longest_name then
415                                         _longest_name = _sources[#_sources].title:len()
416                                 end
417
418
419                                 -- has totals?
420                                 if _sources[#_sources].total then
421                                         _has_totals = true
422                                 end
423                         end
424                 end
425         end
426
427
428         --
429         -- construct diagrams
430         --
431
432         -- if per_instance is enabled then find all instances from the first datasource in diagram
433         -- if per_instance is disabled then use an empty pseudo instance and use model provided values
434         local instances = { "" }
435
436         if opts.per_instance then
437                 instances = self.tree:data_instances( plugin, plugin_instance, _sources[1].type )
438         end
439
440
441         -- iterate over instances
442         for i, instance in ipairs(instances) do
443
444                 -- store title and vlabel
445                 _ti( _args, "-t" )
446                 _ti( _args, opts.title  or self.i18n:title( plugin, plugin_instance, _sources[1].type, instance ) )
447                 _ti( _args, "-v" )
448                 _ti( _args, opts.vlabel or self.i18n:label( plugin, plugin_instance, _sources[1].type, instance ) )
449
450                 -- store additional rrd options
451                 if opts.rrdopts then
452                         for i, o in ipairs(opts.rrdopts) do _ti( _args, o ) end
453                 end
454
455
456                 -- create DEF statements for each instance
457                 for i, source in ipairs(_sources) do
458                         -- fixup properties for per instance mode...
459                         if opts.per_instance then
460                                 source.instance = instance
461                                 source.rrd      = self:mkrrdpath( plugin, plugin_instance, source.type, instance )
462                         end
463
464                         __def( source )
465                 end
466
467                 -- create CDEF required for calculating totals
468                 __cdef_totals()
469
470                 -- create CDEF statements for each instance in reversed order
471                 for i, source in ipairs(_sources) do
472                         __cdef( _sources[1 + #_sources - i] )
473                 end
474
475                 -- create LINE1, AREA and GPRINT statements for each instance
476                 for i, source in ipairs(_sources) do
477                         __line( source )
478                         __gprint( source )
479                 end
480
481                 -- prepend image path to arg stack
482                 _ti( _args, 1, self:mkpngpath( plugin, plugin_instance, index .. instance ) )
483
484                 -- push arg stack to definition list
485                 _ti( defs, _args )
486
487                 -- reset stacks
488                 _args         = { }
489                 _stack_pos    = { }
490                 _stack_neg    = { }
491         end
492
493         return defs
494 end
495
496 function Graph.render( self, plugin, plugin_instance )
497
498         dtype_instances = dtype_instances or { "" }
499         local pngs = { }
500
501         -- check for a whole graph handler
502         local plugin_def = "luci.statistics.rrdtool.definitions." .. plugin
503         local stat, def = pcall( require, plugin_def )
504
505         if stat and def and type(def.rrdargs) == "function" then
506
507                 -- temporary image matrix
508                 local _images = { }
509
510                 -- get diagram definitions
511                 for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance ) ) ) do
512
513                         _images[i] = { }
514
515                         -- get diagram definition instances
516                         local diagrams = self:_generic( opts, plugin, plugin_instance, nil, i )
517
518                         -- render all diagrams
519                         for j, def in ipairs( diagrams ) do
520
521                                 -- remember image
522                                 _images[i][j] = def[1]
523
524                                 -- exec
525                                 self:_rrdtool( def )
526                         end
527                 end
528
529                 -- remember images - XXX: fixme (will cause probs with asymmetric data)
530                 for y = 1, #_images[1] do
531                         for x = 1, #_images do
532                                 table.insert( pngs, _images[x][y] )
533                         end
534                 end
535         else
536
537                 -- no graph handler, iterate over data types
538                 for i, dtype in ipairs( self.tree:data_types( plugin, plugin_instance ) ) do
539
540                         -- check for data type handler
541                         local dtype_def = plugin_def .. "." .. dtype
542                         local stat, def = pcall( require, dtype_def )
543
544                         if stat and def and type(def.rrdargs) == "function" then
545
546                                 -- temporary image matrix
547                                 local _images = { }
548
549                                 -- get diagram definitions
550                                 for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance, dtype ) ) ) do
551
552                                         _images[i] = { }
553
554                                         -- get diagram definition instances
555                                         local diagrams = self:_generic( opts, plugin, plugin_instance, dtype, i )
556
557                                         -- render all diagrams
558                                         for j, def in ipairs( diagrams ) do
559
560                                                 -- remember image
561                                                 _images[i][j] = def[1]
562
563                                                 -- exec
564                                                 self:_rrdtool( def )
565                                         end
566                                 end
567
568                                 -- remember images - XXX: fixme (will cause probs with asymmetric data)
569                                 for y = 1, #_images[1] do
570                                         for x = 1, #_images do
571                                                 table.insert( pngs, _images[x][y] )
572                                         end
573                                 end
574                         else
575
576                                 -- no data type handler, fall back to builtin definition
577                                 if type(self.defs.definitions[dtype]) == "table" then
578
579                                         -- iterate over data type instances
580                                         for i, inst in ipairs( self.tree:data_instances( plugin, plugin_instance, dtype ) ) do
581
582                                                 local title = self.i18n:title( plugin, plugin_instance, dtype, inst )
583                                                 local label = self.i18n:label( plugin, plugin_instance, dtype, inst )
584                                                 local png   = self:mkpngpath( plugin, plugin_instance, dtype, inst )
585                                                 local rrd   = self:mkrrdpath( plugin, plugin_instance, dtype, inst )
586                                                 local args  = { png, "-t", title, "-v", label }
587
588                                                 for i, o in ipairs(self.defs.definitions[dtype]) do
589                                                         -- XXX: this is a somewhat ugly hack to exclude min/max RRAs when rrasingle is on
590                                                         if not ( self.opts.rrasingle and ( o:match("_min") or o:match("_max") ) ) then
591                                                                 table.insert( args, o )
592                                                         end
593                                                 end
594
595                                                 -- remember image
596                                                 table.insert( pngs, png )
597
598                                                 -- exec
599                                                 self:_rrdtool( args, rrd )
600                                         end
601                                 end
602                         end
603                 end
604         end
605
606         return pngs
607 end