* ffluci/statistics: diagram model updates, added ping to genconfig, fixed random...
[project/luci.git] / applications / luci-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 <xm@leipzig.freifunk.net>
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("ffluci.model.uci")
20 require("ffluci.sys.iptparser")
21 require("ffluci.util")
22
23 local ipt = ffluci.sys.iptparser.IptParser()
24 local uci = ffluci.model.uci.Session()
25 local sections, names = uci:sections( "luci_statistics" )
26
27
28 function section( plugin )
29
30 local config = sections[ "collectd_" .. plugin ] or sections["general"]
31
32 if type(config) == "table" and ( plugin == "general" or config.enable == "1" ) then
33
34 local params = ""
35
36 if type( plugins[plugin] ) == "function" then
37 params = plugins[plugin]( config )
38 else
39 params = config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3], plugin == "general" )
40 end
41
42
43 if plugin ~= "general" then
44 print( "LoadPlugin " .. plugin )
45
46 if params:len() > 0 then
47 print( "<Plugin " .. plugin .. ">\n" .. params .. "</Plugin>\n" )
48 else
49 print( "" )
50 end
51 else
52 print( params .. "\n" )
53 end
54 end
55 end
56
57 function config_generic( c, singles, bools, lists, nopad )
58 local str = ""
59
60 if type(c) == "table" then
61
62 if type(singles) == "table" then
63 for i, key in ipairs( singles ) do
64 str = str .. _string( c[key], key, nopad )
65 end
66 end
67
68 if type(bools) == "table" then
69 for i, key in ipairs( bools ) do
70 str = str .. _bool( c[key], key, nopad )
71 end
72 end
73
74 if type(lists) == "table" then
75 str = str .. _list_expand( c, lists, nopad )
76 end
77 end
78
79 return str
80 end
81
82 function config_exec( c )
83 local str = ""
84
85 for s in pairs(sections) do
86 for key, type in pairs({ Exec="collectd_exec_input", NotificationExec="collectd_exec_notify" }) do
87 if sections[s][".type"] == type then
88
89 cmd = sections[s].cmdline
90 user = sections[s].cmduser or "nobody"
91 group = sections[s].cmdgroup or "nogroup"
92
93 str = str .. "\t" .. key .. " " .. user .. ":" .. group .. ' "' .. cmd .. "\"\n"
94 end
95 end
96 end
97
98 return str
99 end
100
101 function config_iptables( c )
102 local str = ""
103
104 for s in pairs(sections) do
105 if sections[s][".type"] == "collectd_iptables_match" then
106
107 search = { }
108
109 for i, k in ipairs( {
110 "table", "chain", "target", "protocol", "source", "destination",
111 "inputif", "outputif", "options"
112 } ) do
113 v = sections[s][k]
114
115 if type(v) == "string" then
116 if k == "options" then v = ffluci.util.split( v, "%s+", nil, true ) end
117 search[k] = v
118 end
119 end
120
121 for i, rule in ipairs( ipt:find( search ) ) do
122
123 name = sections[s].name:gsub( "%s+", "_" )
124 if i > 1 then name = name .. "_(" .. i .. ")" end
125
126 str = str .. "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. "\"\n"
127 end
128 end
129 end
130
131 return str
132 end
133
134 function config_network( c )
135 local str = ""
136
137 for s in pairs(sections) do
138 for key, type in pairs({ Listen="collectd_network_listen", Server="collectd_network_server" }) do
139 if sections[s][".type"] == type then
140
141 host = sections[s].host
142 port = sections[s].port
143
144 if host then
145 if port then
146 str = str .. "\t" .. key .. " " .. host .. " " .. port .. "\n"
147 else
148 str = str .. "\t" .. key .. " " .. host .. "\n"
149 end
150 end
151 end
152 end
153 end
154
155 return str .. _string( c["TimeToLive"], "TimeToLive" )
156 .. _string( c["CacheFlush"], "CacheFlush" )
157 .. _bool( c["Forward"], "Forward" )
158 end
159
160
161 function _list_expand( c, l, nopad )
162 local str = ""
163
164 for i, n in ipairs(l) do
165 if c[n] then
166 if n:find("(%w+)ses") then
167 k = n:gsub("(%w+)ses", "%1s")
168 else
169 k = n:gsub("(%w+)s", "%1")
170 end
171
172 str = str .. _expand( c[n], k, nopad )
173 end
174 end
175
176 return str
177 end
178
179 function _expand( s, n, nopad )
180 if type(s) == "string" then
181 local str = ""
182
183 for i, v in ipairs( ffluci.util.split( s, "%s+", nil, true ) ) do
184 str = str .. _string( v, n, nopad )
185 end
186
187 return str
188 end
189 end
190
191 function _bool( s, n, nopad )
192
193 local str = ""
194 local pad = ""
195 if not nopad then pad = "\t" end
196
197 if s and s == "1" then
198 str = pad .. n .. " true"
199 else
200 str = pad .. n .. " false"
201 end
202
203 return str .. "\n"
204 end
205
206 function _string( s, n, nopad )
207
208 local str = ""
209 local pad = ""
210 if not nopad then pad = "\t" end
211
212 if s then
213 if s:find("[^%d]") then
214 if not s:find("[^%w]") then
215 str = pad .. n .. " " .. s
216 else
217 str = pad .. n .. ' "' .. s .. '"'
218 end
219 else
220 str = pad .. n .. " " .. s
221 end
222
223 str = str .. "\n"
224 end
225
226 return str
227 end
228
229
230 plugins = {
231 general = {
232 { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads" },
233 { },
234 { }
235 },
236
237 csv = {
238 { "DataDir" },
239 { "StoreRates" },
240 { }
241 },
242
243 df = {
244 { },
245 { "IgnoreSelected" },
246 { "Devices", "MountPoints", "FSTypes" }
247 },
248
249 disk = {
250 { },
251 { "IgnoreSelected" },
252 { "Disks" }
253 },
254
255 dns = {
256 { },
257 { },
258 { "Interfaces", "IgnoreSources" }
259 },
260
261 email = {
262 { "SocketFile", "SocketGroup", "SocketPerms", "MaxConns" },
263 { },
264 { }
265 },
266
267 exec = config_exec,
268
269 interface = {
270 { },
271 { "IgnoreSelected" },
272 { "Interfaces" }
273 },
274
275 iptables = config_iptables,
276
277 irq = {
278 { },
279 { "IgnoreSelected" },
280 { "Irqs" }
281 },
282
283 logfile = {
284 { "LogLevel", "File" },
285 { "Timestamp" },
286 { }
287 },
288
289 netlink = {
290 { },
291 { "IgnoreSelected" },
292 { "Interfaces", "VerboseInterfaces", "QDiscs", "Classes", "Filters" }
293 },
294
295 network = config_network,
296
297 ping = {
298 { "TTL" },
299 { },
300 { "Hosts" }
301 },
302
303 processes = {
304 { },
305 { },
306 { "Processes" }
307 },
308
309 rrdtool = {
310 { "DataDir", "StepSize", "HeartBeat", "RRARows", "XFF", "CacheFlush", "CacheTimeout" },
311 { "RRASingle" },
312 { "RRATimespans" }
313 },
314
315 tcpconns = {
316 { },
317 { "ListeningPorts" },
318 { "LocalPorts", "RemotePorts" }
319 },
320
321 unixsock = {
322 { "SocketFile", "SocketGroup", "SocketPerms" },
323 { },
324 { }
325 },
326
327 wireless = {
328 { },
329 { },
330 { }
331 },
332
333 }
334
335
336 section("general")
337
338 for plugin in pairs(plugins) do
339 if plugin ~= "general" then
340 section( plugin )
341 end
342 end