2b2e19dcad0f438ceac8e60ff52ce0b717b2bb49
[project/luci.git] / applications / luci-statistics / files / 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                 if plugin ~= "general" then print( "<Plugin " .. plugin .. ">" ) end
35
36                 if type( plugins[plugin] ) == "function" then
37                         plugins[plugin]( config )
38                 else
39                         config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3], plugin == "general" )
40                 end
41
42                 if plugin ~= "general" then
43                         print( "</Plugin>\n" )
44                 else
45                         print( "\n" )
46                 end
47
48         end
49 end
50
51 function config_generic( c, singles, bools, lists, nopad )
52
53         if type(c) == "table" then
54
55                 if type(singles) == "table" then
56                         for i, key in ipairs( singles ) do
57                                 _string( c[key], key, nopad )
58                         end
59                 end
60
61                 if type(bools) == "table" then
62                         for i, key in ipairs( bools ) do
63                                 _bool( c[key], key, nopad )
64                         end
65                 end
66
67                 if type(lists) == "table" then
68                         _list_expand( c, lists, nopad )
69                 end
70
71         end
72
73 end
74
75 function config_exec( c )
76
77         for s in pairs(sections) do
78                 for key, type in pairs({ Exec="collectd_exec_input", NotificationExec="collectd_exec_notify" }) do
79                         if sections[s][".type"] == type then
80
81                                 cmd   = sections[s].cmdline
82                                 user  = sections[s].cmduser  or "root"
83                                 group = sections[s].cmdgroup or "root"
84
85                                 print( "\t" .. key .. " " .. user .. ":" .. group .. ' "' .. cmd .. '"' )
86                         end
87                 end
88         end
89 end
90
91 function config_iptables( c )
92
93         for s in pairs(sections) do
94                 if sections[s][".type"] == "collectd_iptables_match" then
95
96                         search = { }
97
98                         for i, k in ipairs( {
99                                 "table", "chain", "target", "protocol", "source", "destination",
100                                 "inputif", "outputif", "options"
101                         } ) do
102                                 v = sections[s][k]
103
104                                 if type(v) == "string" then
105                                         if k == "options" then v = ffluci.util.split( v, "%s+", nil, true ) end
106                                         search[k] = v
107                                 end
108                         end
109
110                         for i, rule in ipairs( ipt:find( search ) ) do
111
112                                 name = sections[s].name
113                                 if i > 1 then name = name .. " (" .. i .. ")" end
114
115                                 print( "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. '"' )
116
117                         end
118                 end
119         end
120 end
121
122 function config_network( c )
123
124         for s in pairs(sections) do
125                 for key, type in pairs({ Listen="collectd_network_listen", Server="collectd_network_server" }) do
126                         if sections[s][".type"] == type then
127
128                                 host = sections[s].host
129                                 port = sections[s].port
130
131                                 if host then
132                                         if port then
133                                                 print( "\t" .. key .. " " .. host .. " " .. port )
134                                         else
135                                                 print( "\t" .. key .. " " .. host )
136                                         end
137                                 end
138                         end
139                 end
140         end
141
142         _string( c["TimeToLive"], "TimeToLive" )
143         _string( c["CacheFlush"], "CacheFlush" )
144         _bool(   c["Forward"],    "Forward"    )
145 end
146
147
148 function _list_expand( c, l, nopad )
149         for i, n in ipairs(l) do
150                 if c[n] then
151                         _expand( c[n], n:gsub( "(%w+)s", "%1" ), nopad )
152                 end
153         end
154 end
155
156 function _expand( s, n, nopad )
157         if type(s) == "string" then
158                 for i, v in ipairs( ffluci.util.split( s, "%s+", nil, true ) ) do
159                         _string( v, n, nopad )
160                 end
161         end
162 end
163
164 function _bool( s, n, nopad )
165
166         local pad = ""
167         if not nopad then pad = "\t" end
168
169         if s and s == "1" then
170                 print( pad .. n .. " true" )
171         else
172                 print( pad .. n .. " false" )
173         end
174 end
175
176 function _string( s, n, nopad )
177
178         local pad = ""
179         if not nopad then pad = "\t" end
180
181         if s then
182                 if not s:find("%d") then
183                         if not s:find("%s") then
184                                 print( pad .. n .. " " .. s )
185                         else
186                                 print( pad .. n .. ' "' .. s '"' )
187                         end
188                 else
189                         print( pad .. n .. " " .. s )
190                 end
191         end
192 end             
193
194
195 plugins = {
196         general = {
197                 { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads" },
198                 { },
199                 { }
200         },
201
202         csv     = { 
203                 { "DataDir" },
204                 { "StoreRates" },
205                 { }
206         },
207
208         df      = {
209                 { },
210                 { "IgnoreSelected" },
211                 { "Devices", "MountPoints", "FSTypes" }
212         },
213
214         disk    = {
215                 { },
216                 { "IgnoreSelected" },
217                 { "Disks" }
218         },
219
220         dns     = {
221                 { },
222                 { },
223                 { "Interfaces", "IgnoreSources" }
224         },
225
226         email   = {
227                 { "SocketFile", "SocketUser", "SocketPerms", "MaxConns" },
228                 { },
229                 { }
230         },
231
232         exec    = config_exec,
233
234         interface = {
235                 { },
236                 { "IgnoreSelected" },
237                 { "Interfaces" }
238         },
239
240         iptables = config_iptables,
241
242         irq     = {
243                 { },
244                 { "IgnoreSelected" },
245                 { "Irqs" }
246         },
247
248         logfile = {
249                 { "LogLevel", "File" },
250                 { "Timestamp" },
251                 { }
252         },
253
254         netlink = {
255                 { },
256                 { "IgnoreSelected" },
257                 { "Interfaces", "VerboseInterfaces", "QDiscs", "Classs", "Filters" }
258         },
259
260         network = config_network,
261
262         processes = {
263                 { },
264                 { },
265                 { "Processs" }
266         },
267
268         tcpconns = {
269                 { },
270                 { "ListeningPorts" },
271                 { "LocalPorts", "RemotePorts" }
272         },
273
274         unixsock = {
275                 { "SocketFile", "SocketUser", "SocketPerms" },
276                 { },
277                 { }
278         },
279
280 }
281
282
283 section("general")
284
285 for plugin in pairs(plugins) do
286         if plugin ~= "general" then
287                 section( plugin )
288         end
289 end