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")
25 local uci = luci.model.uci.cursor()
26 local sections = uci:get_all( "luci_statistics" )
30 nixio.stdout:write(...)
31 nixio.stdout:write("\n")
34 function section( plugin )
36 local config = sections[ "collectd_" .. plugin ] or sections["collectd"]
38 if type(config) == "table" and ( plugin == "collectd" or config.enable == "1" ) then
42 if type( plugins[plugin] ) == "function" then
43 params = plugins[plugin]( config )
45 params = config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3], plugin == "collectd" )
49 if plugin ~= "collectd" then
50 print( "LoadPlugin " .. plugin )
52 if params:len() > 0 then
53 print( "<Plugin " .. plugin .. ">\n" .. params .. "</Plugin>\n" )
58 print( params .. "\n" )
63 function config_generic( c, singles, bools, lists, nopad )
66 if type(c) == "table" then
68 if type(singles) == "table" then
69 for i, key in ipairs( singles ) do
70 if preprocess[key] then
71 c[key] = preprocess[key](c[key])
74 str = str .. _string( c[key], key, nopad )
78 if type(bools) == "table" then
79 for i, key in ipairs( bools ) do
80 if preprocess[key] then
81 c[key] = preprocess[key](c[key])
84 str = str .. _bool( c[key], key, nopad )
88 if type(lists) == "table" then
89 str = str .. _list_expand( c, lists, nopad )
96 function config_exec( c )
99 for s in pairs(sections) do
100 for key, type in pairs({ Exec="collectd_exec_input", NotificationExec="collectd_exec_notify" }) do
101 if sections[s][".type"] == type then
103 cmd = sections[s].cmdline
106 cmd = cmd:gsub("^%s+", ""):gsub("%s+$", "")
107 user = sections[s].cmduser or "nobody"
108 group = sections[s].cmdgroup
110 str = str .. "\t" .. key .. ' "' ..
111 user .. ( group and ":" .. group or "" ) .. '" "' ..
112 cmd:gsub('%s+', '" "') .. '"\n'
121 function config_curl( c )
124 for s in pairs(sections) do
125 if sections[s][".type"] == "collectd_curl_page" then
126 str = str .. "\t<Page \"" .. sections[s].name .. "\">\n" ..
127 "\t\tURL \"" .. sections[s].url .. "\"\n" ..
128 "\t\tMeasureResponseTime true\n" ..
136 function config_iptables( c )
139 for id, s in pairs(sections) do
140 if s[".type"] == "collectd_iptables_match" or s[".type"] == "collectd_iptables_match6" then
141 local tname = s.table and tostring(s.table)
142 local chain = s.chain and tostring(s.chain)
144 if tname and tname:match("^%S+$") and chain and chain:match("^%S+$") then
145 local line = { #s[".type"] > 23 and "\tChain6" or "\tChain", tname, chain }
146 local rule = s.rule and tostring(s.rule)
148 if rule and rule:match("^%S+$") then
151 local name = s.name and tostring(s.name)
152 if name and name:match("^%S+$") then
157 str = str .. table.concat(line, " ") .. "\n"
165 function config_network( c )
168 for s in pairs(sections) do
169 for key, type in pairs({ Listen="collectd_network_listen", Server="collectd_network_server" }) do
170 if sections[s][".type"] == type then
172 host = sections[s].host
173 port = sections[s].port
177 str = str .. "\t" .. key .. " \"" .. host .. "\" \"" .. port .. "\"\n"
179 str = str .. "\t" .. key .. " \"" .. host .. "\"\n"
187 _string(c["MaxPacketSize"], "MaxPacketSize") ..
188 _string(c["TimeToLive"], "TimeToLive") ..
189 _bool(c["Forward"], "Forward") ..
190 _bool(c["ReportStats"], "ReportStats")
194 function _list_expand( c, l, nopad )
197 for i, n in ipairs(l) do
199 if preprocess[n] then
200 c[n] = preprocess[n](c[n])
203 if n:find("(%w+)ses") then
204 k = n:gsub("(%w+)ses$", "%1s")
206 k = n:gsub("(%w+)s$", "%1")
209 str = str .. _expand( c[n], k, nopad )
216 function _expand( s, n, nopad )
219 if type(s) == "string" then
220 for i, v in ipairs( luci.util.split( s, "%s+", nil, true ) ) do
221 str = str .. _string( v, n, nopad )
223 elseif type(s) == "table" then
224 for i, v in ipairs(s) do
225 str = str .. _string( v, n, nopad )
232 function _bool( s, n, nopad )
236 if not nopad then pad = "\t" end
238 if s and s == "1" then
239 str = pad .. n .. " true"
241 str = pad .. n .. " false"
247 function _string( s, n, nopad )
251 if not nopad then pad = "\t" end
254 if s:find("[^%d]") or n == "Port" then
255 if not s:find("[^%w]") and n ~= "Port" then
256 str = pad .. n .. " " .. luci.util.trim(s)
258 str = pad .. n .. ' "' .. luci.util.trim(s) .. '"'
261 str = pad .. n .. " " .. luci.util.trim(s)
273 { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads", "Hostname" },
278 { "LogLevel", "File" },
284 local plugin_dir = "/usr/share/luci/statistics/plugins/"
285 for filename in nixio.fs.dir(plugin_dir) do
286 local name = filename:gsub("%.json", "")
287 if (name == "exec") then
288 plugins[name] = config_exec
289 elseif (name == "iptables") then
290 plugins[name] = config_iptables
291 elseif (name == "curl") then
292 plugins[name] = config_curl
293 elseif (name == "network") then
294 plugins[name] = config_network
296 local plugin_def = luci.jsonc.parse(nixio.fs.readfile(plugin_dir .. filename))
297 if type(plugin_def) == "table" then
298 plugins[name] = plugin_def.legend
305 RRATimespans = function(val)
307 for time in luci.util.imatch(val) do
308 table.insert( rv, luci.util.parse_units(time) )
310 return table.concat(rv, " ")
319 for plugin in pairs(plugins) do
320 if (plugin ~= "collectd") and (plugin ~= "logfile") then