5 Luci statistics - collectd configuration generator
6 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
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
12 http://www.apache.org/licenses/LICENSE-2.0
19 require("ffluci.model.uci")
20 require("ffluci.sys.iptparser")
21 require("ffluci.util")
23 local ipt = ffluci.sys.iptparser.IptParser()
24 local uci = ffluci.model.uci.Session()
25 local sections, names = uci:sections( "luci_statistics" )
28 function section( plugin )
30 local config = sections[ "collectd_" .. plugin ] or sections["general"]
32 if type(config) == "table" and ( plugin == "general" or config.enable == "1" ) then
34 if plugin ~= "general" then print( "<Plugin " .. plugin .. ">" ) end
36 if type( plugins[plugin] ) == "function" then
37 plugins[plugin]( config )
39 config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3], plugin == "general" )
42 if plugin ~= "general" then
43 print( "</Plugin>\n" )
51 function config_generic( c, singles, bools, lists, nopad )
53 if type(c) == "table" then
55 if type(singles) == "table" then
56 for i, key in ipairs( singles ) do
57 _string( c[key], key, nopad )
61 if type(bools) == "table" then
62 for i, key in ipairs( bools ) do
63 _bool( c[key], key, nopad )
67 if type(lists) == "table" then
68 _list_expand( c, lists, nopad )
75 function config_exec( c )
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
81 cmd = sections[s].cmdline
82 user = sections[s].cmduser or "root"
83 group = sections[s].cmdgroup or "root"
85 print( "\t" .. key .. " " .. user .. ":" .. group .. ' "' .. cmd .. '"' )
91 function config_iptables( c )
93 for s in pairs(sections) do
94 if sections[s][".type"] == "collectd_iptables_match" then
99 "table", "chain", "target", "protocol", "source", "destination",
100 "inputif", "outputif", "options"
104 if type(v) == "string" then
105 if k == "options" then v = ffluci.util.split( v, "%s+", nil, true ) end
110 for i, rule in ipairs( ipt:find( search ) ) do
112 name = sections[s].name
113 if i > 1 then name = name .. " (" .. i .. ")" end
115 print( "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. '"' )
122 function config_network( c )
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
128 host = sections[s].host
129 port = sections[s].port
133 print( "\t" .. key .. " " .. host .. " " .. port )
135 print( "\t" .. key .. " " .. host )
142 _string( c["TimeToLive"], "TimeToLive" )
143 _string( c["CacheFlush"], "CacheFlush" )
144 _bool( c["Forward"], "Forward" )
148 function _list_expand( c, l, nopad )
149 for i, n in ipairs(l) do
151 _expand( c[n], n:gsub( "(%w+)s", "%1" ), nopad )
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 )
164 function _bool( s, n, nopad )
167 if not nopad then pad = "\t" end
169 if s and s == "1" then
170 print( pad .. n .. " true" )
172 print( pad .. n .. " false" )
176 function _string( s, n, nopad )
179 if not nopad then pad = "\t" end
182 if not s:find("%d") then
183 if not s:find("%s") then
184 print( pad .. n .. " " .. s )
186 print( pad .. n .. ' "' .. s '"' )
189 print( pad .. n .. " " .. s )
197 { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads" },
210 { "IgnoreSelected" },
211 { "Devices", "MountPoints", "FSTypes" }
216 { "IgnoreSelected" },
223 { "Interfaces", "IgnoreSources" }
227 { "SocketFile", "SocketUser", "SocketPerms", "MaxConns" },
236 { "IgnoreSelected" },
240 iptables = config_iptables,
244 { "IgnoreSelected" },
249 { "LogLevel", "File" },
256 { "IgnoreSelected" },
257 { "Interfaces", "VerboseInterfaces", "QDiscs", "Classs", "Filters" }
260 network = config_network,
270 { "ListeningPorts" },
271 { "LocalPorts", "RemotePorts" }
275 { "SocketFile", "SocketUser", "SocketPerms" },
285 for plugin in pairs(plugins) do
286 if plugin ~= "general" then