summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-statistics/luasrc/statistics/rrdtool.lua
blob: 783314e8c902d3cce75d66cc2538f5772cabc8a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
module("luci.statistics.rrdtool", package.seeall)

require("luci.statistics.datatree")
require("luci.statistics.rrdtool.colors")
require("luci.statistics.rrdtool.definitions")
require("luci.util")
require("luci.bits")
require("luci.fs")


Graph = luci.util.class()

function Graph.__init__( self, timespan, opts )

	opts = opts or { }
	opts.width = opts.width or "400"

	self.colors = luci.statistics.rrdtool.colors.Instance()
	self.defs   = luci.statistics.rrdtool.definitions.Instance()
	self.tree   = luci.statistics.datatree.Instance()

	-- rrdtool defalt args
	self.args   = {
		"-a", "PNG",
		"-s", "NOW-" .. ( timespan or 900 ),
		"-w", opts.width
	}
end

function Graph.mktitle( self, host, plugin, plugin_instance, dtype, dtype_instance )
	local t = host .. "/" .. plugin
	if type(plugin_instance) == "string" and plugin_instance:len() > 0 then
		t = t .. "-" .. plugin_instance
	end
	t = t .. "/" .. dtype
	if type(dtype_instance) == "string" and dtype_instance:len() > 0 then
		t = t .. "-" .. dtype_instance
	end
	return t
end

function Graph.mkrrdpath( self, ... )
	return string.format( "/tmp/%s.rrd", self:mktitle( ... ) )
end

function Graph.mkpngpath( self, ... )
	return string.format( "/tmp/rrdimg/%s.png", self:mktitle( ... ) )
end

function Graph._push( self, elem )

	if type(elem) == "string" then
		table.insert( self.args, elem )
	else
		for i, item in ipairs(elem) do
			table.insert( self.args, item )
		end
	end

	return( self.args )
end

function Graph._clearargs( self )
	for i = #self.args, 7, -1 do
		table.remove( self.args, i )
	end
end

function Graph._forcelol( self, list )
	if type(list[1]) ~= "table" then
		return( { list } )
	end
	return( list )
end

function Graph._rrdtool( self, png, rrd )

	-- prepare directory
	local dir = png:gsub("/[^/]+$","")
	luci.fs.mkdir( dir, true )

	-- construct commandline
	local cmdline = "rrdtool graph " .. png

	for i, opt in ipairs(self.args) do

		opt = opt .. ""    -- force string
		
		if rrd then
			opt = opt:gsub( "{file}", rrd )
		end

		if opt:match("[^%w]") then
			cmdline = cmdline .. " '" .. opt .. "'"
		else
			cmdline = cmdline .. " " .. opt
		end
	end

	-- execute rrdtool
	local rrdtool = io.popen( cmdline )
	rrdtool:close()
end

function Graph._generic( self, opts )

	local images     = { }
	local rrasingle  = false	-- XXX: fixme

	-- internal state variables
	local _stack_neg    = { }
	local _stack_pos    = { }
	local _longest_name = 0
	local _has_totals   = false

	-- some convenient aliases
	local _ti	    = table.insert
	local _sf	    = string.format

	-- local helper: create definitions for min, max, avg and create *_nnl (not null) variable from avg
	function __def(source)

		local inst = source.sname
		local rrd  = source.rrd
		local ds   = source.ds

		if not ds or ds:len() == 0 then ds = "value" end

		local rv = { _sf( "DEF:%s_avg=%s:%s:AVERAGE", inst, rrd, ds ) }

		if not rrasingle then
			_ti( rv, _sf( "DEF:%s_min=%s:%s:MIN", inst, rrd, ds ) )
			_ti( rv, _sf( "DEF:%s_max=%s:%s:MAX", inst, rrd, ds ) )
		end

		_ti( rv, _sf( "CDEF:%s_nnl=%s_avg,UN,0,%s_avg,IF", inst, inst, inst ) )

		return rv
	end

	-- local helper: create cdefs depending on source options like flip and overlay
	function __cdef(source)

		local rv = { }
		local prev

		-- find previous source, choose stack depending on flip state
		if source.flip then
			prev = _stack_neg[#_stack_neg]
		else
			prev = _stack_pos[#_stack_pos]
		end

		-- is first source in stack or overlay source: source_stk = source_nnl
		if not prev or source.overlay then
			-- create cdef statement
			_ti( rv, _sf( "CDEF:%s_stk=%s_nnl", source.sname, source.sname ) )

		-- is subsequent source without overlay: source_stk = source_nnl + previous_stk
		else
			-- create cdef statement				
			_ti( rv, _sf(
				"CDEF:%s_stk=%s_nnl,%s_stk,+", source.sname, source.sname, prev
			) )
		end

		-- create multiply by minus one cdef if flip is enabled
		if source.flip then

			-- create cdef statement: source_stk = source_stk * -1
			_ti( rv, _sf( "CDEF:%s_neg=%s_stk,-1,*", source.sname, source.sname ) )

			-- push to negative stack if overlay is disabled
			if not source.overlay then
				_ti( _stack_neg, source.sname )
			end

		-- no flipping, push to positive stack if overlay is disabled
		elseif not source.overlay then

			-- push to positive stack
			_ti( _stack_pos, source.sname )
		end

		-- calculate total amount of data if requested
		if source.total then
			_ti( rv, _sf(
				"CDEF:%s_avg_sample=%s_avg,UN,0,%s_avg,IF,sample_len,*",
				source.sname, source.sname, source.sname
			) )

			_ti( rv, _sf(
				"CDEF:%s_avg_sum=PREV,UN,0,PREV,IF,%s_avg_sample,+",
				source.sname, source.sname, source.sname
			) )
		end


		return rv
	end

	-- local helper: create cdefs required for calculating total values
	function __cdef_totals()
		if _has_totals then
			return {
				_sf( "CDEF:mytime=%s_avg,TIME,TIME,IF", opts.sources[1].sname ),
				"CDEF:sample_len_raw=mytime,PREV(mytime),-",
				"CDEF:sample_len=sample_len_raw,UN,0,sample_len_raw,IF"
			}
		else
			return { }
		end
	end

	-- local helper: create line and area statements
	function __area(source)

		local line_color
		local area_color
		local legend
		local var

		-- find colors: try source, then opts.colors; fall back to random color
		if type(source.color) == "string" then
			line_color = source.color
			area_color = self.colors:from_string( line_color )
		elseif type(opts.colors[source.name:gsub("[^%w]","_")]) == "string" then
			line_color = opts.colors[source.name:gsub("[^%w]","_")]
			area_color = self.colors:from_string( line_color )
		else
			area_color = self.colors:random()
			line_color = self.colors:to_string( area_color )
		end

		-- derive area background color from line color
		area_color = self.colors:to_string( self.colors:faded( area_color ) )

		-- choose source_stk or source_neg variable depending on flip state
		if source.flip then
			var = "neg"
		else
			var = "stk"
		end

		-- create legend
		legend = _sf( "%-" .. _longest_name .. "s", source.name )

		-- create area and line1 statement
		return {
			_sf( "AREA:%s_%s#%s", source.sname, var, area_color ),
			_sf( "LINE1:%s_%s#%s:%s", source.sname, var, line_color, legend )
		}
	end

	-- local helper: create gprint statements
	function __gprint(source)

		local rv     = { }
		local numfmt = opts.number_format or "%6.1lf"
		local totfmt = opts.totals_format or "%5.1lf%s"

		-- don't include MIN if rrasingle is enabled
		if not rrasingle then
			_ti( rv, _sf( "GPRINT:%s_min:MIN:%s Min", source.sname, numfmt ) )
		end

		-- always include AVERAGE
		_ti( rv, _sf( "GPRINT:%s_avg:AVERAGE:%s Avg", source.sname, numfmt ) )

		-- don't include MAX if rrasingle is enabled
		if not rrasingle then
			_ti( rv, _sf( "GPRINT:%s_max:MAX:%s Max", source.sname, numfmt ) )
		end

		-- include total count if requested else include LAST
		if source.total then
			_ti( rv, _sf( "GPRINT:%s_avg_sum:LAST:(ca. %s Total)", source.sname, totfmt ) )
		else
			_ti( rv, _sf( "GPRINT:%s_avg:LAST:%s Last", source.sname, numfmt ) )
		end

		-- end label line
		rv[#rv] = rv[#rv] .. "\\l"


		return rv					
	end


	-- remember images
	_ti( images, opts.image )

	-- insert provided addition rrd options
	self:_push( { "-t", opts.title or "Unknown title" } )
	self:_push( opts.rrd )

	-- store index and safe instance name within each source object,
	-- find longest instance name
	for i, source in ipairs(opts.sources) do

		if source.name:len() > _longest_name then
			_longest_name = source.name:len()
		end

		if source.total then
			_has_totals = true
		end

		source.index = i
		source.sname = i .. source.name:gsub("[^A-Za-z0-9%-_]","_")
	end

	-- create DEF statements for each instance, find longest instance name
	for i, source in ipairs(opts.sources) do
		self:_push( __def( source ) )
	end

	-- create CDEF required for calculating totals
	self:_push( __cdef_totals() )

	-- create CDEF statements for each instance in reversed order
	for i, source in ipairs(opts.sources) do
		self:_push( __cdef( opts.sources[1 + #opts.sources - i] ) )
	end

	-- create LINE1, AREA and GPRINT statements for each instance
	for i, source in ipairs(opts.sources) do
		self:_push( __area( source ) )
		self:_push( __gprint( source ) )
	end

	return images
end

function Graph.render( self, host, plugin, plugin_instance )

	dtype_instances = dtype_instances or { "" }
	local pngs = { }

	-- check for a whole graph handler
	local plugin_def = "luci.statistics.rrdtool.definitions." .. plugin
	local stat, def = pcall( require, plugin_def )

	if stat and def and type(def.rrdargs) == "function" then
		for i, opts in ipairs( self:_forcelol( def.rrdargs( self, host, plugin, plugin_instance, dtype ) ) ) do
			for i, png in ipairs( self:_generic( opts ) ) do
				table.insert( pngs, png )

				-- exec
				self:_rrdtool( png )

				-- clear args
				self:_clearargs()
			end
		end
	else

		-- no graph handler, iterate over data types
		for i, dtype in ipairs( self.tree:data_types( plugin, plugin_instance ) ) do

			-- check for data type handler
			local dtype_def = plugin_def .. "." .. dtype
			local stat, def = pcall( require, dtype_def )

			if stat and def and type(def.rrdargs) == "function" then
				for i, opts in ipairs( self:_forcelol( def.rrdargs( self, host, plugin, plugin_instance, dtype ) ) ) do
					for i, png in ipairs( self:_generic( opts ) ) do
						table.insert( pngs, png )

						-- exec
						self:_rrdtool( png )

						-- clear args
						self:_clearargs()
					end
				end
			else

				-- no data type handler, fall back to builtin definition
				if type(self.defs.definitions[dtype]) == "table" then

					-- iterate over data type instances
					for i, inst in ipairs( self.tree:data_instances( plugin, plugin_instance, dtype ) ) do

						local title = self:mktitle( host, plugin, plugin_instance, dtype, inst )
						local png   = self:mkpngpath( host, plugin, plugin_instance, dtype, inst )
						local rrd   = self:mkrrdpath( host, plugin, plugin_instance, dtype, inst )

						self:_push( { "-t", title } )
						self:_push( self.defs.definitions[dtype] )

						table.insert( pngs, png )

						-- exec
						self:_rrdtool( png, rrd )

						-- clear args
						self:_clearargs()
					end
				end
			end
		end
	end

	return pngs
end