summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-statistics/src/statistics/rrdtool.lua
blob: 56894eb6cf20216a757920288f5de4c6c6900179 (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
module("ffluci.statistics.rrdtool", package.seeall)

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


Graph = ffluci.util.class()

function Graph.__init__( self, timespan, opts )

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

	self.colors = ffluci.statistics.rrdtool.colors.Instance()
	self.defs   = ffluci.statistics.rrdtool.definitions.Instance()
	self.tree   = ffluci.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._rrdtool( self, png, rrd )
	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

	local rrdtool = io.popen( cmdline )
	rrdtool:read("*a")
	rrdtool:close()
end

function Graph._generic( self, optlist )

	local images = { }

	if type(optlist[1]) ~= "table" then
		optlist = { optlist }
	end

	for i, opts in ipairs(optlist) do
		-- remember images
		table.insert( images, opts.image )

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

		-- construct an array of safe instance names
		local inst_names = { }
		for i, source in ipairs(opts.sources) do
			inst_names[i] = i .. source.name:gsub("[^A-Za-z0-9%-_]","_")
		end

		-- create DEF statements for each instance, find longest instance name
		local longest_name = 0
		for i, source in ipairs(opts.sources) do
			if source.name:len() > longest_name then
				longest_name = source.name:len()
			end

			self:_push( "DEF:" .. inst_names[i] .. "_min=" ..source.rrd .. ":value:MIN" )
			self:_push( "DEF:" .. inst_names[i] .. "_avg=" ..source.rrd .. ":value:AVERAGE" )
			self:_push( "DEF:" .. inst_names[i] .. "_max=" ..source.rrd .. ":value:MAX" )
			self:_push( "CDEF:" .. inst_names[i] .. "_nnl=" .. inst_names[i] .. "_avg,UN,0," .. inst_names[i] .. "_avg,IF" )
		end

		-- create CDEF statement for last instance name
		self:_push( "CDEF:" .. inst_names[#inst_names] .. "_stk=" .. inst_names[#inst_names] .. "_nnl" )

		-- create CDEF statements for each instance
		for i, source in ipairs(inst_names) do
			if i > 1 then
				self:_push(
					"CDEF:" ..
					inst_names[1 + #inst_names - i] .. "_stk=" ..
					inst_names[1 + #inst_names - i] .. "_nnl," ..
					inst_names[2 + #inst_names - i] .. "_stk,+"
				)
			end
		end

		-- create LINE and GPRINT statements for each instance
		for i, source in ipairs(opts.sources) do

			local legend = string.format(
				"%-" .. longest_name .. "s",
				source.name
			)

			local numfmt = opts.number_format or "%6.1lf"

			local line_color
			local area_color

			if type(opts.colors[source.name]) == "string" then
				line_color = opts.colors[source.name]
				area_color = self.colors:from_string( line_color )
			else
				area_color = self.colors:random()
				line_color = self.colors:to_string( area_color )
			end

			area_color = self.colors:to_string(
				self.colors:faded( area_color )
			)

			self:_push( "AREA:"   .. inst_names[i] .. "_stk#" .. area_color )
			self:_push( "LINE1:"  .. inst_names[i] .. "_stk#" .. line_color .. ":" .. legend )
			self:_push( "GPRINT:" .. inst_names[i] .. "_min:MIN:" .. numfmt .. " Min" )
			self:_push( "GPRINT:" .. inst_names[i] .. "_avg:AVERAGE:" .. numfmt .. " Avg" )
			self:_push( "GPRINT:" .. inst_names[i] .. "_max:MAX:" .. numfmt .. " Max" )
			self:_push( "GPRINT:" .. inst_names[i] .. "_avg:LAST:" .. numfmt .. " Last\\l" )
		end
	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 = "ffluci.statistics.rrdtool.definitions." .. plugin
	local stat, def = pcall( require, plugin_def )

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

			-- exec
			self:_rrdtool( png )

			-- clear args
			self:_clearargs()
		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, png in ipairs( self:_generic( def.rrdargs( self, host, plugin, plugin_instance, dtype ) ) ) do
					table.insert( pngs, png )

					-- exec
					self:_rrdtool( png )

					-- clear args
					self:_clearargs()
				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