* luci/statistics: include cpu plugin in stat-genconfig
[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.Session()
25 local sections, names = uci:sections( "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                                 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 = luci.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( luci.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         collectd = {
232                 { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads" },
233                 { },
234                 { }
235         },
236
237         cpu     = {
238                 { },
239                 { },
240                 { }
241         },
242
243         csv     = { 
244                 { "DataDir" },
245                 { "StoreRates" },
246                 { }
247         },
248
249         df      = {
250                 { },
251                 { "IgnoreSelected" },
252                 { "Devices", "MountPoints", "FSTypes" }
253         },
254
255         disk    = {
256                 { },
257                 { "IgnoreSelected" },
258                 { "Disks" }
259         },
260
261         dns     = {
262                 { },
263                 { },
264                 { "Interfaces", "IgnoreSources" }
265         },
266
267         email   = {
268                 { "SocketFile", "SocketGroup", "SocketPerms", "MaxConns" },
269                 { },
270                 { }
271         },
272
273         exec    = config_exec,
274
275         interface = {
276                 { },
277                 { "IgnoreSelected" },
278                 { "Interfaces" }
279         },
280
281         iptables = config_iptables,
282
283         irq     = {
284                 { },
285                 { "IgnoreSelected" },
286                 { "Irqs" }
287         },
288
289         logfile = {
290                 { "LogLevel", "File" },
291                 { "Timestamp" },
292                 { }
293         },
294
295         netlink = {
296                 { },
297                 { "IgnoreSelected" },
298                 { "Interfaces", "VerboseInterfaces", "QDiscs", "Classes", "Filters" }
299         },
300
301         network = config_network,
302
303         ping    = {
304                 { "TTL" },
305                 { },
306                 { "Hosts" }
307         },
308
309         processes = {
310                 { },
311                 { },
312                 { "Processes" }
313         },
314
315         rrdtool = {
316                 { "DataDir", "StepSize", "HeartBeat", "RRARows", "XFF", "CacheFlush", "CacheTimeout" },
317                 { "RRASingle" },
318                 { "RRATimespans" }
319         },
320
321         tcpconns = {
322                 { },
323                 { "ListeningPorts" },
324                 { "LocalPorts", "RemotePorts" }
325         },
326
327         unixsock = {
328                 { "SocketFile", "SocketGroup", "SocketPerms" },
329                 { },
330                 { }
331         },
332
333         wireless = {
334                 { },
335                 { },
336                 { }
337         },
338
339 }
340
341
342 section("collectd")
343
344 for plugin in pairs(plugins) do
345         if plugin ~= "collectd" then
346                 section( plugin )
347         end
348 end