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 ]
32 if type(config) == "table" and config.enable == "1" then
34 print( "<Plugin " .. plugin .. ">" )
36 if type( plugins[plugin] ) == "function" then
37 plugins[plugin]( config )
39 config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3] )
42 print( "</Plugin>\n" )
47 function config_generic( c, singles, bools, lists )
49 if type(c) == "table" then
51 if type(singles) == "table" then
52 for i, key in ipairs( singles ) do
53 _string( c[key], key )
57 if type(bools) == "table" then
58 for i, key in ipairs( bools ) do
63 if type(lists) == "table" then
64 _list_expand( c, lists )
71 function config_exec( c )
73 for s in pairs(sections) do
74 for key, type in pairs({ Exec="collectd_exec_input", NotificationExec="collectd_exec_notify" }) do
75 if sections[s][".type"] == type then
77 cmd = sections[s].cmdline
78 user = sections[s].cmduser or "root"
79 group = sections[s].cmdgroup or "root"
81 print( "\t" .. key .. " " .. user .. ":" .. group .. ' "' .. cmd .. '"' )
87 function config_iptables( c )
89 for s in pairs(sections) do
90 if sections[s][".type"] == "collectd_iptables_match" then
95 "table", "chain", "target", "protocol", "source", "destination",
96 "inputif", "outputif", "options"
100 if type(v) == "string" then
101 if k == "options" then v = ffluci.util.split( v, "%s+", nil, true ) end
106 for i, rule in ipairs( ipt:find( search ) ) do
108 name = sections[s].name
109 if i > 1 then name = name .. " (" .. i .. ")" end
111 print( "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. '"' )
118 function config_network( c )
120 for s in pairs(sections) do
121 for key, type in pairs({ Listen="collectd_network_listen", Server="collectd_network_server" }) do
122 if sections[s][".type"] == type then
124 host = sections[s].host
125 port = sections[s].port
129 print( "\t" .. key .. " " .. host .. " " .. port )
131 print( "\t" .. key .. " " .. host )
138 _string( c["TimeToLive"], "TimeToLive" )
139 _string( c["CacheFlush"], "CacheFlush" )
140 _bool( c["Forward"], "Forward" )
144 function _list_expand( c, l )
145 for i, n in ipairs(l) do
147 _expand( c[n], n:gsub( "(%w+)s", "%1" ) )
152 function _expand( s, n )
153 if type(s) == "string" then
154 for i, v in ipairs( ffluci.util.split( s, "%s+", nil, true ) ) do
160 function _bool( s, n )
161 if s and s == "1" then
162 print( "\t" .. n .. " true" )
164 print( "\t" .. n .. " false" )
168 function _string( s, n )
170 if not s:find("%d") then
171 if not s:find("%s") then
172 print( "\t" .. n .. " " .. s )
174 print( "\t" .. n .. ' "' .. s '"' )
177 print( "\t" .. n .. " " .. s )
192 { "IgnoreSelected" },
193 { "Devices", "MountPoints", "FSTypes" }
198 { "IgnoreSelected" },
205 { "Interfaces", "IgnoreSources" }
209 { "SocketFile", "SocketUser", "SocketPerms", "MaxConns" },
218 { "IgnoreSelected" },
222 iptables = config_iptables,
226 { "IgnoreSelected" },
231 { "LogLevel", "File" },
238 { "IgnoreSelected" },
239 { "Interfaces", "VerboseInterfaces", "QDiscs", "Classs", "Filters" }
242 network = config_network,
247 for plugin in pairs(plugins) do