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