82431fbd3919edc12d9bb91b4edb2858d5101908
[project/luci.git] / applications / luci-app-statistics / root / usr / bin / stat-genconfig
1 #!/usr/bin/lua
2
3 --[[
4
5 Luci statistics - collectd configuration generator
6 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
7
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13
14 $Id$
15
16 ]]--
17
18
19 require("luci.model.uci")
20 require("luci.sys.iptparser")
21 require("luci.util")
22 require("luci.i18n")
23 require("nixio.fs")
24
25 local ipt = luci.sys.iptparser.IptParser()
26 local uci = luci.model.uci.cursor()
27 local sections = uci:get_all( "luci_statistics" )
28
29
30 function print(...)
31 nixio.stdout:write(...)
32 nixio.stdout:write("\n")
33 end
34
35 function section( plugin )
36
37 local config = sections[ "collectd_" .. plugin ] or sections["collectd"]
38
39 if type(config) == "table" and ( plugin == "collectd" or config.enable == "1" ) then
40
41 local params = ""
42
43 if type( plugins[plugin] ) == "function" then
44 params = plugins[plugin]( config )
45 else
46 params = config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3], plugin == "collectd" )
47 end
48
49
50 if plugin ~= "collectd" then
51 print( "LoadPlugin " .. plugin )
52
53 if params:len() > 0 then
54 print( "<Plugin " .. plugin .. ">\n" .. params .. "</Plugin>\n" )
55 else
56 print( "" )
57 end
58 else
59 print( params .. "\n" )
60 end
61 end
62 end
63
64 function config_generic( c, singles, bools, lists, nopad )
65 local str = ""
66
67 if type(c) == "table" then
68
69 if type(singles) == "table" then
70 for i, key in ipairs( singles ) do
71 if preprocess[key] then
72 c[key] = preprocess[key](c[key])
73 end
74
75 str = str .. _string( c[key], key, nopad )
76 end
77 end
78
79 if type(bools) == "table" then
80 for i, key in ipairs( bools ) do
81 if preprocess[key] then
82 c[key] = preprocess[key](c[key])
83 end
84
85 str = str .. _bool( c[key], key, nopad )
86 end
87 end
88
89 if type(lists) == "table" then
90 str = str .. _list_expand( c, lists, nopad )
91 end
92 end
93
94 return str
95 end
96
97 function config_exec( c )
98 local str = ""
99
100 for s in pairs(sections) do
101 for key, type in pairs({ Exec="collectd_exec_input", NotificationExec="collectd_exec_notify" }) do
102 if sections[s][".type"] == type then
103
104 cmd = sections[s].cmdline
105
106 if cmd then
107 cmd = cmd:gsub("^%s+", ""):gsub("%s+$", "")
108 user = sections[s].cmduser or "nobody"
109 group = sections[s].cmdgroup
110
111 str = str .. "\t" .. key .. ' "' ..
112 user .. ( group and ":" .. group or "" ) .. '" "' ..
113 cmd:gsub('%s+', '" "') .. '"\n'
114 end
115 end
116 end
117 end
118
119 return str
120 end
121
122 function config_curl( c )
123 local str = ""
124
125 for s in pairs(sections) do
126 if sections[s][".type"] == "collectd_curl_page" then
127 str = str .. "\t<Page \"" .. sections[s].name .. "\">\n" ..
128 "\t\tURL \"" .. sections[s].url .. "\"\n" ..
129 "\t\tMeasureResponseTime true\n" ..
130 "\t</Page>\n"
131 end
132 end
133
134 return str
135 end
136
137 function config_iptables( c )
138 local str = ""
139
140 for s in pairs(sections) do
141 if sections[s][".type"] == "collectd_iptables_match" then
142
143 search = { }
144
145 for i, k in ipairs( {
146 "table", "chain", "target", "protocol", "source", "destination",
147 "inputif", "outputif", "options"
148 } ) do
149 v = sections[s][k]
150
151 if type(v) == "string" then
152 if k == "options" then v = luci.util.split( v, "%s+", nil, true ) end
153 search[k] = v
154 end
155 end
156
157 for i, rule in ipairs( ipt:find( search ) ) do
158
159 name = sections[s].name:gsub( "%s+", "_" )
160 if i > 1 then name = name .. "_(" .. i .. ")" end
161
162 str = str .. "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. "\"\n"
163 end
164 end
165 end
166
167 return str
168 end
169
170 function config_network( c )
171 local str = ""
172
173 for s in pairs(sections) do
174 for key, type in pairs({ Listen="collectd_network_listen", Server="collectd_network_server" }) do
175 if sections[s][".type"] == type then
176
177 host = sections[s].host
178 port = sections[s].port
179
180 if host then
181 if port then
182 str = str .. "\t" .. key .. " \"" .. host .. "\" \"" .. port .. "\"\n"
183 else
184 str = str .. "\t" .. key .. " \"" .. host .. "\"\n"
185 end
186 end
187 end
188 end
189 end
190
191 return str .. _string( c["TimeToLive"], "TimeToLive" )
192 .. _string( c["CacheFlush"], "CacheFlush" )
193 .. _bool( c["Forward"], "Forward" )
194 end
195
196
197 function _list_expand( c, l, nopad )
198 local str = ""
199
200 for i, n in ipairs(l) do
201 if c[n] then
202 if preprocess[n] then
203 c[n] = preprocess[n](c[n])
204 end
205
206 if n:find("(%w+)ses") then
207 k = n:gsub("(%w+)ses$", "%1s")
208 else
209 k = n:gsub("(%w+)s$", "%1")
210 end
211
212 str = str .. _expand( c[n], k, nopad )
213 end
214 end
215
216 return str
217 end
218
219 function _expand( s, n, nopad )
220 local str = ""
221
222 if type(s) == "string" then
223 for i, v in ipairs( luci.util.split( s, "%s+", nil, true ) ) do
224 str = str .. _string( v, n, nopad )
225 end
226 elseif type(s) == "table" then
227 for i, v in ipairs(s) do
228 str = str .. _string( v, n, nopad )
229 end
230 end
231
232 return str
233 end
234
235 function _bool( s, n, nopad )
236
237 local str = ""
238 local pad = ""
239 if not nopad then pad = "\t" end
240
241 if s and s == "1" then
242 str = pad .. n .. " true"
243 else
244 str = pad .. n .. " false"
245 end
246
247 return str .. "\n"
248 end
249
250 function _string( s, n, nopad )
251
252 local str = ""
253 local pad = ""
254 if not nopad then pad = "\t" end
255
256 if s then
257 if s:find("[^%d]") or n == "Port" then
258 if not s:find("[^%w]") and n ~= "Port" then
259 str = pad .. n .. " " .. luci.util.trim(s)
260 else
261 str = pad .. n .. ' "' .. luci.util.trim(s) .. '"'
262 end
263 else
264 str = pad .. n .. " " .. luci.util.trim(s)
265 end
266
267 str = str .. "\n"
268 end
269
270 return str
271 end
272
273
274 plugins = {
275 collectd = {
276 { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads", "Hostname" },
277 { },
278 { }
279 },
280 logfile = {
281 { "LogLevel", "File" },
282 { "Timestamp" },
283 { }
284 },
285 }
286
287 local plugin_dir = "/usr/lib/lua/luci/statistics/plugins/"
288 for filename in nixio.fs.dir(plugin_dir) do
289 local plugin_fun = loadfile(plugin_dir .. filename)
290 setfenv(plugin_fun, { _ = luci.i18n.translate })
291 local plugin = plugin_fun()
292 local name = filename:gsub("%.lua", "")
293 if (name == "exec") then
294 plugins[name] = config_exec
295 elseif (name == "iptables") then
296 plugins[name] = config_iptables
297 elseif (name == "curl") then
298 plugins[name] = config_curl
299 elseif (name == "network") then
300 plugins[name] = config_network
301 else
302 plugins[name] = plugin.legend
303 end
304 end
305
306
307 preprocess = {
308 RRATimespans = function(val)
309 local rv = { }
310 for time in val:gmatch("[^%s]+") do
311 table.insert( rv, luci.util.parse_units(time) )
312 end
313 return table.concat(rv, " ")
314 end
315 }
316
317
318 section("collectd")
319
320 section("logfile")
321
322 for plugin in pairs(plugins) do
323 if (plugin ~= "collectd") and (plugin ~= "logfile") then
324 section( plugin )
325 end
326 end