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
36 if type( plugins[plugin] ) == "function" then
37 params = plugins[plugin]( config )
39 params = config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3], plugin == "general" )
43 if plugin ~= "general" then
44 print( "LoadPlugin " .. plugin )
46 if params:len() > 0 then
47 print( "<Plugin " .. plugin .. ">\n" .. params .. "</Plugin>\n" )
52 print( params .. "\n" )
57 function config_generic( c, singles, bools, lists, nopad )
60 if type(c) == "table" then
62 if type(singles) == "table" then
63 for i, key in ipairs( singles ) do
64 str = str .. _string( c[key], key, nopad )
68 if type(bools) == "table" then
69 for i, key in ipairs( bools ) do
70 str = str .. _bool( c[key], key, nopad )
74 if type(lists) == "table" then
75 str = str .. _list_expand( c, lists, nopad )
82 function config_exec( c )
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
89 cmd = sections[s].cmdline
90 user = sections[s].cmduser or "nobody"
91 group = sections[s].cmdgroup or "nogroup"
93 str = str .. "\t" .. key .. " " .. user .. ":" .. group .. ' "' .. cmd .. "\"\n"
101 function config_iptables( c )
104 for s in pairs(sections) do
105 if sections[s][".type"] == "collectd_iptables_match" then
109 for i, k in ipairs( {
110 "table", "chain", "target", "protocol", "source", "destination",
111 "inputif", "outputif", "options"
115 if type(v) == "string" then
116 if k == "options" then v = ffluci.util.split( v, "%s+", nil, true ) end
121 for i, rule in ipairs( ipt:find( search ) ) do
123 name = sections[s].name:gsub( "%s+", "_" )
124 if i > 1 then name = name .. "_(" .. i .. ")" end
126 str = str .. "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. "\"\n"
134 function config_network( c )
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
141 host = sections[s].host
142 port = sections[s].port
146 str = str .. "\t" .. key .. " " .. host .. " " .. port .. "\n"
148 str = str .. "\t" .. key .. " " .. host .. "\n"
155 return str .. _string( c["TimeToLive"], "TimeToLive" )
156 .. _string( c["CacheFlush"], "CacheFlush" )
157 .. _bool( c["Forward"], "Forward" )
161 function _list_expand( c, l, nopad )
164 for i, n in ipairs(l) do
166 if n:find("(%w+)ses") then
167 k = n:gsub("(%w+)ses", "%1s")
169 k = n:gsub("(%w+)s", "%1")
172 str = str .. _expand( c[n], k, nopad )
179 function _expand( s, n, nopad )
180 if type(s) == "string" then
183 for i, v in ipairs( ffluci.util.split( s, "%s+", nil, true ) ) do
184 str = str .. _string( v, n, nopad )
191 function _bool( s, n, nopad )
195 if not nopad then pad = "\t" end
197 if s and s == "1" then
198 str = pad .. n .. " true"
200 str = pad .. n .. " false"
206 function _string( s, n, nopad )
210 if not nopad then pad = "\t" end
213 if s:find("[^%d]") then
214 if not s:find("[^%w]") then
215 str = pad .. n .. " " .. s
217 str = pad .. n .. ' "' .. s .. '"'
220 str = pad .. n .. " " .. s
232 { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads" },
245 { "IgnoreSelected" },
246 { "Devices", "MountPoints", "FSTypes" }
251 { "IgnoreSelected" },
258 { "Interfaces", "IgnoreSources" }
262 { "SocketFile", "SocketGroup", "SocketPerms", "MaxConns" },
271 { "IgnoreSelected" },
275 iptables = config_iptables,
279 { "IgnoreSelected" },
284 { "LogLevel", "File" },
291 { "IgnoreSelected" },
292 { "Interfaces", "VerboseInterfaces", "QDiscs", "Classes", "Filters" }
295 network = config_network,
310 { "DataDir", "StepSize", "HeartBeat", "RRARows", "XFF", "CacheFlush", "CacheTimeout" },
317 { "ListeningPorts" },
318 { "LocalPorts", "RemotePorts" }
322 { "SocketFile", "SocketGroup", "SocketPerms" },
338 for plugin in pairs(plugins) do
339 if plugin ~= "general" then