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