statistics: Add support for entropy stats
[project/luci.git] / applications / luci-app-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 <jow@openwrt.org>
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 print(...)
29         nixio.stdout:write(...)
30         nixio.stdout:write("\n")
31 end
32
33 function section( plugin )
34
35         local config = sections[ "collectd_" .. plugin ] or sections["collectd"]
36
37         if type(config) == "table" and ( plugin == "collectd" or config.enable == "1" ) then
38
39                 local params = ""
40
41                 if type( plugins[plugin] ) == "function" then
42                         params = plugins[plugin]( config )
43                 else
44                         params = config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3], plugin == "collectd" )
45                 end
46
47
48                 if plugin ~= "collectd" then
49                         print( "LoadPlugin " .. plugin )
50
51                         if params:len() > 0 then
52                                 print( "<Plugin " .. plugin .. ">\n" .. params .. "</Plugin>\n" )
53                         else
54                                 print( "" )
55                         end
56                 else
57                         print( params .. "\n" )
58                 end
59         end
60 end
61
62 function config_generic( c, singles, bools, lists, nopad )
63         local str = ""
64
65         if type(c) == "table" then
66
67                 if type(singles) == "table" then
68                         for i, key in ipairs( singles ) do
69                                 if preprocess[key] then
70                                         c[key] = preprocess[key](c[key])
71                                 end
72
73                                 str = str .. _string( c[key], key, nopad )
74                         end
75                 end
76
77                 if type(bools) == "table" then
78                         for i, key in ipairs( bools ) do
79                                 if preprocess[key] then
80                                         c[key] = preprocess[key](c[key])
81                                 end
82
83                                 str = str .. _bool( c[key], key, nopad )
84                         end
85                 end
86
87                 if type(lists) == "table" then
88                         str = str .. _list_expand( c, lists, nopad )
89                 end
90         end
91
92         return str
93 end
94
95 function config_exec( c )
96         local str = ""
97
98         for s in pairs(sections) do
99                 for key, type in pairs({ Exec="collectd_exec_input", NotificationExec="collectd_exec_notify" }) do
100                         if sections[s][".type"] == type then
101
102                                 cmd = sections[s].cmdline
103
104                                 if cmd then
105                                         cmd   = cmd:gsub("^%s+", ""):gsub("%s+$", "")
106                                         user  = sections[s].cmduser  or "nobody"
107                                         group = sections[s].cmdgroup
108
109                                         str = str .. "\t" .. key .. ' "' ..
110                                                 user .. ( group and ":" .. group or "" ) .. '" "' ..
111                                                 cmd:gsub('%s+', '" "') .. '"\n'
112                                 end
113                         end
114                 end
115         end
116
117         return str
118 end
119
120 function config_iptables( c )
121         local str = ""
122
123         for s in pairs(sections) do
124                 if sections[s][".type"] == "collectd_iptables_match" then
125
126                         search = { }
127
128                         for i, k in ipairs( {
129                                 "table", "chain", "target", "protocol", "source", "destination",
130                                 "inputif", "outputif", "options"
131                         } ) do
132                                 v = sections[s][k]
133
134                                 if type(v) == "string" then
135                                         if k == "options" then v = luci.util.split( v, "%s+", nil, true ) end
136                                         search[k] = v
137                                 end
138                         end
139
140                         for i, rule in ipairs( ipt:find( search ) ) do
141
142                                 name = sections[s].name:gsub( "%s+", "_" )
143                                 if i > 1 then name = name .. "_(" .. i .. ")" end
144
145                                 str = str .. "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. "\"\n"
146                         end
147                 end
148         end
149
150         return str
151 end
152
153 function config_network( c )
154         local str = ""
155
156         for s in pairs(sections) do
157                 for key, type in pairs({ Listen="collectd_network_listen", Server="collectd_network_server" }) do
158                         if sections[s][".type"] == type then
159
160                                 host = sections[s].host
161                                 port = sections[s].port
162
163                                 if host then
164                                         if port then
165                                                 str = str .. "\t" .. key .. " \"" .. host .. "\" \"" .. port .. "\"\n"
166                                         else
167                                                 str = str .. "\t" .. key .. " \"" .. host .. "\"\n"
168                                         end
169                                 end
170                                                 end
171                                 end
172                 end
173
174         return str .. _string( c["TimeToLive"], "TimeToLive" )
175                    .. _string( c["CacheFlush"], "CacheFlush" )
176                    .. _bool(   c["Forward"],    "Forward"    )
177 end
178
179
180 function _list_expand( c, l, nopad )
181         local str = ""
182
183         for i, n in ipairs(l) do
184                 if c[n] then
185                         if preprocess[n] then
186                                 c[n] = preprocess[n](c[n])
187                         end
188
189                         if n:find("(%w+)ses") then
190                                 k = n:gsub("(%w+)ses", "%1s")
191                         else
192                                 k = n:gsub("(%w+)s", "%1")
193                         end
194
195                         str = str .. _expand( c[n], k, nopad )
196                 end
197         end
198
199         return str
200 end
201
202 function _expand( s, n, nopad )
203         local str = ""
204
205         if type(s) == "string" then
206                 for i, v in ipairs( luci.util.split( s, "%s+", nil, true ) ) do
207                         str = str .. _string( v, n, nopad )
208                 end
209         elseif type(s) == "table" then
210                 for i, v in ipairs(s) do
211                         str = str .. _string( v, n, nopad )
212                 end
213         end
214
215         return str
216 end
217
218 function _bool( s, n, nopad )
219
220         local str = ""
221         local pad = ""
222         if not nopad then pad = "\t" end
223
224         if s and s == "1" then
225                 str = pad .. n .. " true"
226         else
227                 str = pad .. n .. " false"
228         end
229
230         return str .. "\n"
231 end
232
233 function _string( s, n, nopad )
234
235         local str = ""
236         local pad = ""
237         if not nopad then pad = "\t" end
238
239         if s then
240                 if s:find("[^%d]") or n == "Port" then
241                         if not s:find("[^%w]") and n ~= "Port" then
242                                 str = pad .. n .. " " .. luci.util.trim(s)
243                         else
244                                 str = pad .. n .. ' "' .. luci.util.trim(s) .. '"'
245                         end
246                 else
247                         str = pad .. n .. " " .. luci.util.trim(s)
248                 end
249
250                 str = str .. "\n"
251         end
252
253         return str
254 end
255
256
257 plugins = {
258         collectd = {
259                 { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads", "Hostname" },
260                 { },
261                 { }
262         },
263
264         conntrack = {
265                 { },
266                 { },
267                 { }
268         },
269
270         cpu     = {
271                 { },
272                 { },
273                 { }
274         },
275
276         csv     = {
277                 { "DataDir" },
278                 { "StoreRates" },
279                 { }
280         },
281
282         df      = {
283                 { },
284                 { "IgnoreSelected" },
285                 { "Devices", "MountPoints", "FSTypes" }
286         },
287
288         disk    = {
289                 { },
290                 { "IgnoreSelected" },
291                 { "Disks" }
292         },
293
294         dns     = {
295                 { },
296                 { },
297                 { "Interfaces", "IgnoreSources" }
298         },
299
300         email   = {
301                 { "SocketFile", "SocketGroup", "SocketPerms", "MaxConns" },
302                 { },
303                 { }
304         },
305
306         entropy = {
307                 { },
308                 { },
309                 { }
310         },
311
312         exec    = config_exec,
313
314         interface = {
315                 { },
316                 { "IgnoreSelected" },
317                 { "Interfaces" }
318         },
319
320         iptables = config_iptables,
321
322         irq     = {
323                 { },
324                 { "IgnoreSelected" },
325                 { "Irqs" }
326         },
327
328         iwinfo = {
329                 { },
330                 { "IgnoreSelected" },
331                 { "Interfaces" }
332         },
333
334         load    = {
335                 { },
336                 { },
337                 { }
338         },
339
340         logfile = {
341                 { "LogLevel", "File" },
342                 { "Timestamp" },
343                 { }
344         },
345
346         madwifi = {
347                 { "WatchSet" },
348                 { },
349                 { "Interfaces", "WatchAdds" }
350         },
351
352         memory = { 
353                 { },
354                 { },
355                 { }
356         },
357
358         netlink = {
359                 { },
360                 { "IgnoreSelected" },
361                 { "Interfaces", "VerboseInterfaces", "QDiscs", "Classes", "Filters" }
362         },
363
364         network = config_network,
365
366         nut = {
367                 { "UPS" },
368                 { },
369                 { }
370         },
371
372         olsrd = {
373                 { "Host", "Port", "CollectLinks","CollectRoutes","CollectTopology"},
374                 { },
375                 { }
376         },
377
378         ping    = {
379                 { "TTL", "Interval" },
380                 { },
381                 { "Hosts" }
382         },
383
384         processes = {
385                 { },
386                 { },
387                 { "Processes" }
388         },
389
390         rrdtool = {
391                 { "DataDir", "StepSize", "HeartBeat", "RRARows", "XFF", "CacheFlush", "CacheTimeout" },
392                 { "RRASingle" },
393                 { "RRATimespans" }
394         },
395
396         splash_leases = {
397           { },
398           { },
399           { }
400         },
401
402         tcpconns = {
403                 { },
404                 { "ListeningPorts" },
405                 { "LocalPorts", "RemotePorts" }
406         },
407
408         unixsock = {
409                 { "SocketFile", "SocketGroup", "SocketPerms" },
410                 { },
411                 { }
412         },
413
414         uptime = {
415                 { },
416                 { },
417                 { }
418         },
419
420         wireless = {
421                 { },
422                 { },
423                 { }
424         },
425 }
426
427 preprocess = {
428         RRATimespans = function(val)
429                 local rv = { }
430                 for time in val:gmatch("[^%s]+") do
431                         table.insert( rv, luci.util.parse_units(time) )
432                 end
433                 return table.concat(rv, " ")
434         end
435 }
436
437
438 section("collectd")
439
440 for plugin in pairs(plugins) do
441         if plugin ~= "collectd" then
442                 section( plugin )
443         end
444 end