8d3482c161a4db67dce8d71cd59169bf2db8ee2f
[project/luci.git] / applications / luci-app-statistics / luasrc / statistics / rrdtool.lua
1 -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
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 ds = dsource,
401 type = dtype,
402 instance = dinst,
403 index = #_sources + 1,
404 sname = ( #_sources + 1 ) .. dtype
405 } )
406
407
408 -- generate datasource title
409 _sources[#_sources].title = self.i18n:ds( _sources[#_sources] )
410
411
412 -- find longest name ...
413 if _sources[#_sources].title:len() > _longest_name then
414 _longest_name = _sources[#_sources].title:len()
415 end
416
417
418 -- has totals?
419 if _sources[#_sources].total then
420 _has_totals = true
421 end
422 end
423 end
424 end
425
426
427 --
428 -- construct diagrams
429 --
430
431 -- if per_instance is enabled then find all instances from the first datasource in diagram
432 -- if per_instance is disabled then use an empty pseudo instance and use model provided values
433 local instances = { "" }
434
435 if opts.per_instance then
436 instances = self.tree:data_instances( plugin, plugin_instance, _sources[1].type )
437 end
438
439
440 -- iterate over instances
441 for i, instance in ipairs(instances) do
442
443 -- store title and vlabel
444 _ti( _args, "-t" )
445 _ti( _args, self.i18n:title( plugin, plugin_instance, _sources[1].type, instance, opts.title ) )
446 _ti( _args, "-v" )
447 _ti( _args, self.i18n:label( plugin, plugin_instance, _sources[1].type, instance, opts.vlabel ) )
448 if opts.y_max then
449 _ti ( _args, "-u" )
450 _ti ( _args, opts.y_max )
451 end
452 if opts.y_min then
453 _ti ( _args, "-l" )
454 _ti ( _args, opts.y_min )
455 end
456 if opts.units_exponent then
457 _ti ( _args, "-X" )
458 _ti ( _args, opts.units_exponent )
459 end
460
461 -- store additional rrd options
462 if opts.rrdopts then
463 for i, o in ipairs(opts.rrdopts) do _ti( _args, o ) end
464 end
465
466
467 -- create DEF statements for each instance
468 for i, source in ipairs(_sources) do
469 -- fixup properties for per instance mode...
470 if opts.per_instance then
471 source.instance = instance
472 source.rrd = self:mkrrdpath( plugin, plugin_instance, source.type, instance )
473 end
474
475 __def( source )
476 end
477
478 -- create CDEF required for calculating totals
479 __cdef_totals()
480
481 -- create CDEF statements for each instance in reversed order
482 for i, source in ipairs(_sources) do
483 __cdef( _sources[1 + #_sources - i] )
484 end
485
486 -- create LINE1, AREA and GPRINT statements for each instance
487 for i, source in ipairs(_sources) do
488 __line( source )
489 __gprint( source )
490 end
491
492 -- prepend image path to arg stack
493 _ti( _args, 1, self:mkpngpath( plugin, plugin_instance, index .. instance ) )
494
495 -- push arg stack to definition list
496 _ti( defs, _args )
497
498 -- reset stacks
499 _args = { }
500 _stack_pos = { }
501 _stack_neg = { }
502 end
503
504 return defs
505 end
506
507 function Graph.render( self, plugin, plugin_instance, is_index )
508
509 dtype_instances = dtype_instances or { "" }
510 local pngs = { }
511
512 -- check for a whole graph handler
513 local plugin_def = "luci.statistics.rrdtool.definitions." .. plugin
514 local stat, def = pcall( require, plugin_def )
515
516 if stat and def and type(def.rrdargs) == "function" then
517
518 -- temporary image matrix
519 local _images = { }
520
521 -- get diagram definitions
522 for i, opts in ipairs( self:_forcelol( def.rrdargs( self, plugin, plugin_instance, nil, is_index ) ) ) do
523 if not is_index or not opts.detail then
524 _images[i] = { }
525
526 -- get diagram definition instances
527 local diagrams = self:_generic( opts, plugin, plugin_instance, nil, i )
528
529 -- render all diagrams
530 for j, def in ipairs( diagrams ) do
531 -- remember image
532 _images[i][j] = def[1]
533
534 -- exec
535 self:_rrdtool( def )
536 end
537 end
538 end
539
540 -- remember images - XXX: fixme (will cause probs with asymmetric data)
541 for y = 1, #_images[1] do
542 for x = 1, #_images do
543 table.insert( pngs, _images[x][y] )
544 end
545 end
546 end
547
548 return pngs
549 end