5 Luci statistics - collectd configuration generator
6 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
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" )
29 nixio.stdout:write(...)
30 nixio.stdout:write("\n")
33 function section( plugin )
35 local config = sections[ "collectd_" .. plugin ] or sections["collectd"]
37 if type(config) == "table" and ( plugin == "collectd" or config.enable == "1" ) then
41 if type( plugins[plugin] ) == "function" then
42 params = plugins[plugin]( config )
44 params = config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3], plugin == "collectd" )
48 if plugin ~= "collectd" then
49 print( "LoadPlugin " .. plugin )
51 if params:len() > 0 then
52 print( "<Plugin " .. plugin .. ">\n" .. params .. "</Plugin>\n" )
57 print( params .. "\n" )
62 function config_generic( c, singles, bools, lists, nopad )
65 if type(c) == "table" then
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])
73 str = str .. _string( c[key], key, nopad )
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])
83 str = str .. _bool( c[key], key, nopad )
87 if type(lists) == "table" then
88 str = str .. _list_expand( c, lists, nopad )
95 function config_exec( c )
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
102 cmd = sections[s].cmdline
105 cmd = cmd:gsub("^%s+", ""):gsub("%s+$", "")
106 user = sections[s].cmduser or "nobody"
107 group = sections[s].cmdgroup
109 str = str .. "\t" .. key .. ' "' ..
110 user .. ( group and ":" .. group or "" ) .. '" "' ..
111 cmd:gsub('%s+', '" "') .. '"\n'
120 function config_iptables( c )
123 for s in pairs(sections) do
124 if sections[s][".type"] == "collectd_iptables_match" then
128 for i, k in ipairs( {
129 "table", "chain", "target", "protocol", "source", "destination",
130 "inputif", "outputif", "options"
134 if type(v) == "string" then
135 if k == "options" then v = luci.util.split( v, "%s+", nil, true ) end
140 for i, rule in ipairs( ipt:find( search ) ) do
142 name = sections[s].name:gsub( "%s+", "_" )
143 if i > 1 then name = name .. "_(" .. i .. ")" end
145 str = str .. "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. "\"\n"
153 function config_network( c )
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
160 host = sections[s].host
161 port = sections[s].port
165 str = str .. "\t" .. key .. " \"" .. host .. "\" \"" .. port .. "\"\n"
167 str = str .. "\t" .. key .. " \"" .. host .. "\"\n"
174 return str .. _string( c["TimeToLive"], "TimeToLive" )
175 .. _string( c["CacheFlush"], "CacheFlush" )
176 .. _bool( c["Forward"], "Forward" )
180 function _list_expand( c, l, nopad )
183 for i, n in ipairs(l) do
185 if preprocess[n] then
186 c[n] = preprocess[n](c[n])
189 if n:find("(%w+)ses") then
190 k = n:gsub("(%w+)ses", "%1s")
192 k = n:gsub("(%w+)s", "%1")
195 str = str .. _expand( c[n], k, nopad )
202 function _expand( s, n, nopad )
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 )
209 elseif type(s) == "table" then
210 for i, v in ipairs(s) do
211 str = str .. _string( v, n, nopad )
218 function _bool( s, n, nopad )
222 if not nopad then pad = "\t" end
224 if s and s == "1" then
225 str = pad .. n .. " true"
227 str = pad .. n .. " false"
233 function _string( s, n, nopad )
237 if not nopad then pad = "\t" end
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)
244 str = pad .. n .. ' "' .. luci.util.trim(s) .. '"'
247 str = pad .. n .. " " .. luci.util.trim(s)
259 { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads", "Hostname" },
284 { "IgnoreSelected" },
285 { "Devices", "MountPoints", "FSTypes" }
290 { "IgnoreSelected" },
297 { "Interfaces", "IgnoreSources" }
301 { "SocketFile", "SocketGroup", "SocketPerms", "MaxConns" },
316 { "IgnoreSelected" },
320 iptables = config_iptables,
324 { "IgnoreSelected" },
330 { "IgnoreSelected" },
341 { "LogLevel", "File" },
349 { "Interfaces", "WatchAdds" }
360 { "IgnoreSelected" },
361 { "Interfaces", "VerboseInterfaces", "QDiscs", "Classes", "Filters" }
364 network = config_network,
373 { "Host", "Port", "CollectLinks","CollectRoutes","CollectTopology"},
379 { "TTL", "Interval" },
391 { "DataDir", "StepSize", "HeartBeat", "RRARows", "XFF", "CacheFlush", "CacheTimeout" },
404 { "ListeningPorts" },
405 { "LocalPorts", "RemotePorts" }
409 { "SocketFile", "SocketGroup", "SocketPerms" },
428 RRATimespans = function(val)
430 for time in val:gmatch("[^%s]+") do
431 table.insert( rv, luci.util.parse_units(time) )
433 return table.concat(rv, " ")
440 for plugin in pairs(plugins) do
441 if plugin ~= "collectd" then