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")
26 local ipt = luci.sys.iptparser.IptParser()
27 local uci = luci.model.uci.cursor()
28 local sections = uci:get_all( "luci_statistics" )
32 nixio.stdout:write(...)
33 nixio.stdout:write("\n")
36 function section( plugin )
38 local config = sections[ "collectd_" .. plugin ] or sections["collectd"]
40 if type(config) == "table" and ( plugin == "collectd" or config.enable == "1" ) then
44 if type( plugins[plugin] ) == "function" then
45 params = plugins[plugin]( config )
47 params = config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3], plugin == "collectd" )
51 if plugin ~= "collectd" then
52 print( "LoadPlugin " .. plugin )
54 if params:len() > 0 then
55 print( "<Plugin " .. plugin .. ">\n" .. params .. "</Plugin>\n" )
60 print( params .. "\n" )
65 function config_generic( c, singles, bools, lists, nopad )
68 if type(c) == "table" then
70 if type(singles) == "table" then
71 for i, key in ipairs( singles ) do
72 if preprocess[key] then
73 c[key] = preprocess[key](c[key])
76 str = str .. _string( c[key], key, nopad )
80 if type(bools) == "table" then
81 for i, key in ipairs( bools ) do
82 if preprocess[key] then
83 c[key] = preprocess[key](c[key])
86 str = str .. _bool( c[key], key, nopad )
90 if type(lists) == "table" then
91 str = str .. _list_expand( c, lists, nopad )
98 function config_exec( c )
101 for s in pairs(sections) do
102 for key, type in pairs({ Exec="collectd_exec_input", NotificationExec="collectd_exec_notify" }) do
103 if sections[s][".type"] == type then
105 cmd = sections[s].cmdline
108 cmd = cmd:gsub("^%s+", ""):gsub("%s+$", "")
109 user = sections[s].cmduser or "nobody"
110 group = sections[s].cmdgroup
112 str = str .. "\t" .. key .. ' "' ..
113 user .. ( group and ":" .. group or "" ) .. '" "' ..
114 cmd:gsub('%s+', '" "') .. '"\n'
123 function config_curl( c )
126 for s in pairs(sections) do
127 if sections[s][".type"] == "collectd_curl_page" then
128 str = str .. "\t<Page \"" .. sections[s].name .. "\">\n" ..
129 "\t\tURL \"" .. sections[s].url .. "\"\n" ..
130 "\t\tMeasureResponseTime true\n" ..
138 function config_iptables( c )
141 for s in pairs(sections) do
142 if sections[s][".type"] == "collectd_iptables_match" then
146 for i, k in ipairs( {
147 "table", "chain", "target", "protocol", "source", "destination",
148 "inputif", "outputif", "options"
152 if type(v) == "string" then
153 if k == "options" then v = luci.util.split( v, "%s+", nil, true ) end
158 for i, rule in ipairs( ipt:find( search ) ) do
160 name = sections[s].name:gsub( "%s+", "_" )
161 if i > 1 then name = name .. "_(" .. i .. ")" end
163 str = str .. "\tChain " .. rule.table .. " " .. rule.chain .. " " .. rule.index .. ' "' .. name .. "\"\n"
171 function config_network( c )
174 for s in pairs(sections) do
175 for key, type in pairs({ Listen="collectd_network_listen", Server="collectd_network_server" }) do
176 if sections[s][".type"] == type then
178 host = sections[s].host
179 port = sections[s].port
183 str = str .. "\t" .. key .. " \"" .. host .. "\" \"" .. port .. "\"\n"
185 str = str .. "\t" .. key .. " \"" .. host .. "\"\n"
192 return str .. _string( c["TimeToLive"], "TimeToLive" )
193 .. _string( c["CacheFlush"], "CacheFlush" )
194 .. _bool( c["Forward"], "Forward" )
198 function _list_expand( c, l, nopad )
201 for i, n in ipairs(l) do
203 if preprocess[n] then
204 c[n] = preprocess[n](c[n])
207 if n:find("(%w+)ses") then
208 k = n:gsub("(%w+)ses$", "%1s")
210 k = n:gsub("(%w+)s$", "%1")
213 str = str .. _expand( c[n], k, nopad )
220 function _expand( s, n, nopad )
223 if type(s) == "string" then
224 for i, v in ipairs( luci.util.split( s, "%s+", nil, true ) ) do
225 str = str .. _string( v, n, nopad )
227 elseif type(s) == "table" then
228 for i, v in ipairs(s) do
229 str = str .. _string( v, n, nopad )
236 function _bool( s, n, nopad )
240 if not nopad then pad = "\t" end
242 if s and s == "1" then
243 str = pad .. n .. " true"
245 str = pad .. n .. " false"
251 function _string( s, n, nopad )
255 if not nopad then pad = "\t" end
258 if s:find("[^%d]") or n == "Port" then
259 if not s:find("[^%w]") and n ~= "Port" then
260 str = pad .. n .. " " .. luci.util.trim(s)
262 str = pad .. n .. ' "' .. luci.util.trim(s) .. '"'
265 str = pad .. n .. " " .. luci.util.trim(s)
277 { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads", "Hostname" },
282 { "LogLevel", "File" },
288 local plugin_dir = "/usr/share/luci/statistics/plugins/"
289 for filename in nixio.fs.dir(plugin_dir) do
290 local name = filename:gsub("%.json", "")
291 if (name == "exec") then
292 plugins[name] = config_exec
293 elseif (name == "iptables") then
294 plugins[name] = config_iptables
295 elseif (name == "curl") then
296 plugins[name] = config_curl
297 elseif (name == "network") then
298 plugins[name] = config_network
300 local plugin_def = luci.jsonc.parse(nixio.fs.readfile(plugin_dir .. filename))
301 if type(plugin_def) == "table" then
302 plugins[name] = plugin_def.legend
309 RRATimespans = function(val)
311 for time in val:gmatch("[^%s]+") do
312 table.insert( rv, luci.util.parse_units(time) )
314 return table.concat(rv, " ")
323 for plugin in pairs(plugins) do
324 if (plugin ~= "collectd") and (plugin ~= "logfile") then