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