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"
186 return str .. _string( c["TimeToLive"], "TimeToLive" )
187 .. _string( c["CacheFlush"], "CacheFlush" )
188 .. _bool( c["Forward"], "Forward" )
192 function _list_expand( c, l, nopad )
195 for i, n in ipairs(l) do
197 if preprocess[n] then
198 c[n] = preprocess[n](c[n])
201 if n:find("(%w+)ses") then
202 k = n:gsub("(%w+)ses$", "%1s")
204 k = n:gsub("(%w+)s$", "%1")
207 str = str .. _expand( c[n], k, nopad )
214 function _expand( s, n, nopad )
217 if type(s) == "string" then
218 for i, v in ipairs( luci.util.split( s, "%s+", nil, true ) ) do
219 str = str .. _string( v, n, nopad )
221 elseif type(s) == "table" then
222 for i, v in ipairs(s) do
223 str = str .. _string( v, n, nopad )
230 function _bool( s, n, nopad )
234 if not nopad then pad = "\t" end
236 if s and s == "1" then
237 str = pad .. n .. " true"
239 str = pad .. n .. " false"
245 function _string( s, n, nopad )
249 if not nopad then pad = "\t" end
252 if s:find("[^%d]") or n == "Port" then
253 if not s:find("[^%w]") and n ~= "Port" then
254 str = pad .. n .. " " .. luci.util.trim(s)
256 str = pad .. n .. ' "' .. luci.util.trim(s) .. '"'
259 str = pad .. n .. " " .. luci.util.trim(s)
271 { "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads", "Hostname" },
276 { "LogLevel", "File" },
282 local plugin_dir = "/usr/share/luci/statistics/plugins/"
283 for filename in nixio.fs.dir(plugin_dir) do
284 local name = filename:gsub("%.json", "")
285 if (name == "exec") then
286 plugins[name] = config_exec
287 elseif (name == "iptables") then
288 plugins[name] = config_iptables
289 elseif (name == "curl") then
290 plugins[name] = config_curl
291 elseif (name == "network") then
292 plugins[name] = config_network
294 local plugin_def = luci.jsonc.parse(nixio.fs.readfile(plugin_dir .. filename))
295 if type(plugin_def) == "table" then
296 plugins[name] = plugin_def.legend
303 RRATimespans = function(val)
305 for time in luci.util.imatch(val) do
306 table.insert( rv, luci.util.parse_units(time) )
308 return table.concat(rv, " ")
317 for plugin in pairs(plugins) do
318 if (plugin ~= "collectd") and (plugin ~= "logfile") then