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("luci.model.uci")
20 require("luci.sys.iptparser")
23 local ipt = luci.sys.iptparser.IptParser()
24 local uci = luci.model.uci
25 local sections = uci.get_all( "luci_statistics" )
28 function section( plugin )
30 local config = sections[ "collectd_" .. plugin ] or sections["collectd"]
32 if type(config) == "table" and ( plugin == "collectd" 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 == "collectd" )
43 if plugin ~= "collectd" 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 if preprocess[key] then
65 c[key] = preprocess[key](c[key])
68 str = str .. _string( c[key], key, nopad )
72 if type(bools) == "table" then
73 for i, key in ipairs( bools ) do
74 if preprocess[key] then
75 c[key] = preprocess[key](c[key])
78 str = str .. _bool( c[key], key, nopad )
82 if type(lists) == "table" then
83 str = str .. _list_expand( c, lists, nopad )
90 function config_exec( c )
93 for s in pairs(sections) do
94 for key, type in pairs({ Exec="collectd_exec_input", NotificationExec="collectd_exec_notify" }) do
95 if sections[s][".type"] == type then
97 cmd = sections[s].cmdline
98 user = sections[s].cmduser or "nobody"
99 group = sections[s].cmdgroup or "nogroup"
101 str = str .. "\t" .. key .. " " .. user .. ":" .. group .. ' "' .. cmd .. "\"\n"
109 function config_iptables( c )
112 for s in pairs(sections) do
113 if sections[s][".type"] == "collectd_iptables_match" then
117 for i, k in ipairs( {
118 "table", "chain", "target", "protocol", "source", "destination",
119 "inputif", "outputif", "options"
123 if type(v) == "string" then
124 if k == "options" then v = luci.util.split( v, "%s+", nil, true ) end
129 for i, rule in ipairs( ipt:find( search ) ) do
131 name = sections[s].name:gsub( "%s+", "_" )
132 if i > 1 then name = name .. "_(" .. i .. ")" end
134 str = str .. "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. "\"\n"
142 function config_network( c )
145 for s in pairs(sections) do
146 for key, type in pairs({ Listen="collectd_network_listen", Server="collectd_network_server" }) do
147 if sections[s][".type"] == type then
149 host = sections[s].host
150 port = sections[s].port
154 str = str .. "\t" .. key .. " " .. host .. " " .. port .. "\n"
156 str = str .. "\t" .. key .. " " .. host .. "\n"
163 return str .. _string( c["TimeToLive"], "TimeToLive" )
164 .. _string( c["CacheFlush"], "CacheFlush" )
165 .. _bool( c["Forward"], "Forward" )
169 function _list_expand( c, l, nopad )
172 for i, n in ipairs(l) do
174 if preprocess[n] then
175 c[n] = preprocess[n](c[n])
178 if n:find("(%w+)ses") then
179 k = n:gsub("(%w+)ses", "%1s")
181 k = n:gsub("(%w+)s", "%1")
184 str = str .. _expand( c[n], k, nopad )
191 function _expand( s, n, nopad )
192 if type(s) == "string" then
195 for i, v in ipairs( luci.util.split( s, "%s+", nil, true ) ) do
196 str = str .. _string( v, n, nopad )
203 function _bool( s, n, nopad )
207 if not nopad then pad = "\t" end
209 if s and s == "1" then
210 str = pad .. n .. " true"
212 str = pad .. n .. " false"
218 function _string( s, n, nopad )
222 if not nopad then pad = "\t" end
225 if s:find("[^%d]") then
226 if not s:find("[^%w]") then
227 str = pad .. n .. " " .. s
229 str = pad .. n .. ' "' .. s .. '"'
232 str = pad .. n .. " " .. s
244 { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads", "Hostname" },
263 { "IgnoreSelected" },
264 { "Devices", "MountPoints", "FSTypes" }
269 { "IgnoreSelected" },
276 { "Interfaces", "IgnoreSources" }
280 { "SocketFile", "SocketGroup", "SocketPerms", "MaxConns" },
289 { "IgnoreSelected" },
293 iptables = config_iptables,
297 { "IgnoreSelected" },
302 { "LogLevel", "File" },
309 { "IgnoreSelected" },
310 { "Interfaces", "VerboseInterfaces", "QDiscs", "Classes", "Filters" }
313 network = config_network,
328 { "DataDir", "StepSize", "HeartBeat", "RRARows", "XFF", "CacheFlush", "CacheTimeout" },
335 { "ListeningPorts" },
336 { "LocalPorts", "RemotePorts" }
340 { "SocketFile", "SocketGroup", "SocketPerms" },
353 RRATimespans = function(val)
355 for time in val:gmatch("[^%s]+") do
356 table.insert( rv, luci.util.parse_units(time) )
358 return table.concat(rv, " ")
365 for plugin in pairs(plugins) do
366 if plugin ~= "collectd" then