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.cursor()
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
100 cmd = cmd:gsub("^%s+", ""):gsub("%s+$", "")
101 user = sections[s].cmduser or "nobody"
102 group = sections[s].cmdgroup
104 str = str .. "\t" .. key .. ' "' ..
105 user .. ( group and ":" .. group or "" ) .. '" "' ..
106 cmd:gsub('%s+', '" "') .. '"\n'
115 function config_iptables( c )
118 for s in pairs(sections) do
119 if sections[s][".type"] == "collectd_iptables_match" then
123 for i, k in ipairs( {
124 "table", "chain", "target", "protocol", "source", "destination",
125 "inputif", "outputif", "options"
129 if type(v) == "string" then
130 if k == "options" then v = luci.util.split( v, "%s+", nil, true ) end
135 for i, rule in ipairs( ipt:find( search ) ) do
137 name = sections[s].name:gsub( "%s+", "_" )
138 if i > 1 then name = name .. "_(" .. i .. ")" end
140 str = str .. "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. "\"\n"
148 function config_network( c )
151 for s in pairs(sections) do
152 for key, type in pairs({ Listen="collectd_network_listen", Server="collectd_network_server" }) do
153 if sections[s][".type"] == type then
155 host = sections[s].host
156 port = sections[s].port
160 str = str .. "\t" .. key .. " " .. host .. " " .. port .. "\n"
162 str = str .. "\t" .. key .. " " .. host .. "\n"
169 return str .. _string( c["TimeToLive"], "TimeToLive" )
170 .. _string( c["CacheFlush"], "CacheFlush" )
171 .. _bool( c["Forward"], "Forward" )
175 function _list_expand( c, l, nopad )
178 for i, n in ipairs(l) do
180 if preprocess[n] then
181 c[n] = preprocess[n](c[n])
184 if n:find("(%w+)ses") then
185 k = n:gsub("(%w+)ses", "%1s")
187 k = n:gsub("(%w+)s", "%1")
190 str = str .. _expand( c[n], k, nopad )
197 function _expand( s, n, nopad )
198 if type(s) == "string" then
201 for i, v in ipairs( luci.util.split( s, "%s+", nil, true ) ) do
202 str = str .. _string( v, n, nopad )
209 function _bool( s, n, nopad )
213 if not nopad then pad = "\t" end
215 if s and s == "1" then
216 str = pad .. n .. " true"
218 str = pad .. n .. " false"
224 function _string( s, n, nopad )
228 if not nopad then pad = "\t" end
231 if s:find("[^%d]") then
232 if not s:find("[^%w]") then
233 str = pad .. n .. " " .. luci.util.trim(s)
235 str = pad .. n .. ' "' .. luci.util.trim(s) .. '"'
238 str = pad .. n .. " " .. luci.util.trim(s)
250 { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads", "Hostname" },
269 { "IgnoreSelected" },
270 { "Devices", "MountPoints", "FSTypes" }
275 { "IgnoreSelected" },
282 { "Interfaces", "IgnoreSources" }
286 { "SocketFile", "SocketGroup", "SocketPerms", "MaxConns" },
295 { "IgnoreSelected" },
299 iptables = config_iptables,
303 { "IgnoreSelected" },
314 { "LogLevel", "File" },
321 { "IgnoreSelected" },
322 { "Interfaces", "VerboseInterfaces", "QDiscs", "Classes", "Filters" }
325 network = config_network,
340 { "DataDir", "StepSize", "HeartBeat", "RRARows", "XFF", "CacheFlush", "CacheTimeout" },
347 { "ListeningPorts" },
348 { "LocalPorts", "RemotePorts" }
352 { "SocketFile", "SocketGroup", "SocketPerms" },
365 RRATimespans = function(val)
367 for time in val:gmatch("[^%s]+") do
368 table.insert( rv, luci.util.parse_units(time) )
370 return table.concat(rv, " ")
377 for plugin in pairs(plugins) do
378 if plugin ~= "collectd" then