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