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