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