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