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