luci-app-statistics: rewrite stat-genconfig in ucode
Rewrite the collectd config generator script in ucode to remove the implicit dependency on the Lua runtime. Also move the stat-genconfig script into /usr/libexec as it isn't really a user facing executable. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
parent
287775351b
commit
984a9a4d69
4 changed files with 285 additions and 325 deletions
|
@ -9,7 +9,6 @@ include $(TOPDIR)/rules.mk
|
|||
LUCI_TITLE:=LuCI Statistics Application
|
||||
LUCI_DEPENDS:= \
|
||||
+luci-base \
|
||||
+luci-lib-jsonc \
|
||||
+collectd \
|
||||
+rrdtool1 \
|
||||
+collectd-mod-rrdtool \
|
||||
|
|
|
@ -17,7 +17,7 @@ start_service() {
|
|||
fi
|
||||
|
||||
### create config
|
||||
/usr/bin/stat-genconfig > /var/etc/collectd.conf
|
||||
/usr/libexec/stat-genconfig > /var/etc/collectd.conf
|
||||
|
||||
### workaround broken permissions on /tmp
|
||||
chmod 1777 /tmp
|
||||
|
|
|
@ -1,323 +0,0 @@
|
|||
#!/usr/bin/lua
|
||||
|
||||
--[[
|
||||
|
||||
Luci statistics - collectd configuration generator
|
||||
(c) 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
$Id$
|
||||
|
||||
]]--
|
||||
|
||||
|
||||
require("luci.model.uci")
|
||||
require("luci.util")
|
||||
require("luci.i18n")
|
||||
require("luci.jsonc")
|
||||
require("nixio.fs")
|
||||
|
||||
local uci = luci.model.uci.cursor()
|
||||
local sections = uci:get_all( "luci_statistics" )
|
||||
|
||||
|
||||
function print(...)
|
||||
nixio.stdout:write(...)
|
||||
nixio.stdout:write("\n")
|
||||
end
|
||||
|
||||
function section( plugin )
|
||||
|
||||
local config = sections[ "collectd_" .. plugin ] or sections["collectd"]
|
||||
|
||||
if type(config) == "table" and ( plugin == "collectd" or config.enable == "1" ) then
|
||||
|
||||
local params = ""
|
||||
|
||||
if type( plugins[plugin] ) == "function" then
|
||||
params = plugins[plugin]( config )
|
||||
else
|
||||
params = config_generic( config, plugins[plugin][1], plugins[plugin][2], plugins[plugin][3], plugin == "collectd" )
|
||||
end
|
||||
|
||||
|
||||
if plugin ~= "collectd" then
|
||||
print( "LoadPlugin " .. plugin )
|
||||
|
||||
if params:len() > 0 then
|
||||
print( "<Plugin " .. plugin .. ">\n" .. params .. "</Plugin>\n" )
|
||||
else
|
||||
print( "" )
|
||||
end
|
||||
else
|
||||
print( params .. "\n" )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function config_generic( c, singles, bools, lists, nopad )
|
||||
local str = ""
|
||||
|
||||
if type(c) == "table" then
|
||||
|
||||
if type(singles) == "table" then
|
||||
for i, key in ipairs( singles ) do
|
||||
if preprocess[key] then
|
||||
c[key] = preprocess[key](c[key])
|
||||
end
|
||||
|
||||
str = str .. _string( c[key], key, nopad )
|
||||
end
|
||||
end
|
||||
|
||||
if type(bools) == "table" then
|
||||
for i, key in ipairs( bools ) do
|
||||
if preprocess[key] then
|
||||
c[key] = preprocess[key](c[key])
|
||||
end
|
||||
|
||||
str = str .. _bool( c[key], key, nopad )
|
||||
end
|
||||
end
|
||||
|
||||
if type(lists) == "table" then
|
||||
str = str .. _list_expand( c, lists, nopad )
|
||||
end
|
||||
end
|
||||
|
||||
return str
|
||||
end
|
||||
|
||||
function config_exec( c )
|
||||
local str = ""
|
||||
|
||||
for s in pairs(sections) do
|
||||
for key, type in pairs({ Exec="collectd_exec_input", NotificationExec="collectd_exec_notify" }) do
|
||||
if sections[s][".type"] == type then
|
||||
|
||||
cmd = sections[s].cmdline
|
||||
|
||||
if cmd then
|
||||
cmd = cmd:gsub("^%s+", ""):gsub("%s+$", "")
|
||||
user = sections[s].cmduser or "nobody"
|
||||
group = sections[s].cmdgroup
|
||||
|
||||
str = str .. "\t" .. key .. ' "' ..
|
||||
user .. ( group and ":" .. group or "" ) .. '" "' ..
|
||||
cmd:gsub('%s+', '" "') .. '"\n'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return str
|
||||
end
|
||||
|
||||
function config_curl( c )
|
||||
local str = ""
|
||||
|
||||
for s in pairs(sections) do
|
||||
if sections[s][".type"] == "collectd_curl_page" then
|
||||
str = str .. "\t<Page \"" .. sections[s].name .. "\">\n" ..
|
||||
"\t\tURL \"" .. sections[s].url .. "\"\n" ..
|
||||
"\t\tMeasureResponseTime true\n" ..
|
||||
"\t</Page>\n"
|
||||
end
|
||||
end
|
||||
|
||||
return str
|
||||
end
|
||||
|
||||
function config_iptables( c )
|
||||
local str = ""
|
||||
|
||||
for id, s in pairs(sections) do
|
||||
if s[".type"] == "collectd_iptables_match" or s[".type"] == "collectd_iptables_match6" then
|
||||
local tname = s.table and tostring(s.table)
|
||||
local chain = s.chain and tostring(s.chain)
|
||||
|
||||
if tname and tname:match("^%S+$") and chain and chain:match("^%S+$") then
|
||||
local line = { #s[".type"] > 23 and "\tChain6" or "\tChain", tname, chain }
|
||||
local rule = s.rule and tostring(s.rule)
|
||||
|
||||
if rule and rule:match("^%S+$") then
|
||||
line[#line+1] = rule
|
||||
|
||||
local name = s.name and tostring(s.name)
|
||||
if name and name:match("^%S+$") then
|
||||
line[#line+1] = name
|
||||
end
|
||||
end
|
||||
|
||||
str = str .. table.concat(line, " ") .. "\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return str
|
||||
end
|
||||
|
||||
function config_network( c )
|
||||
local str = ""
|
||||
|
||||
for s in pairs(sections) do
|
||||
for key, type in pairs({ Listen="collectd_network_listen", Server="collectd_network_server" }) do
|
||||
if sections[s][".type"] == type then
|
||||
|
||||
host = sections[s].host
|
||||
port = sections[s].port
|
||||
|
||||
if host then
|
||||
if port then
|
||||
str = str .. "\t" .. key .. " \"" .. host .. "\" \"" .. port .. "\"\n"
|
||||
else
|
||||
str = str .. "\t" .. key .. " \"" .. host .. "\"\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return str ..
|
||||
_string(c["MaxPacketSize"], "MaxPacketSize") ..
|
||||
_string(c["TimeToLive"], "TimeToLive") ..
|
||||
_bool(c["Forward"], "Forward") ..
|
||||
_bool(c["ReportStats"], "ReportStats")
|
||||
end
|
||||
|
||||
|
||||
function _list_expand( c, l, nopad )
|
||||
local str = ""
|
||||
|
||||
for i, n in ipairs(l) do
|
||||
if c[n] then
|
||||
if preprocess[n] then
|
||||
c[n] = preprocess[n](c[n])
|
||||
end
|
||||
|
||||
if n:find("(%w+)ses") then
|
||||
k = n:gsub("(%w+)ses$", "%1s")
|
||||
else
|
||||
k = n:gsub("(%w+)s$", "%1")
|
||||
end
|
||||
|
||||
str = str .. _expand( c[n], k, nopad )
|
||||
end
|
||||
end
|
||||
|
||||
return str
|
||||
end
|
||||
|
||||
function _expand( s, n, nopad )
|
||||
local str = ""
|
||||
|
||||
if type(s) == "string" then
|
||||
for i, v in ipairs( luci.util.split( s, "%s+", nil, true ) ) do
|
||||
str = str .. _string( v, n, nopad )
|
||||
end
|
||||
elseif type(s) == "table" then
|
||||
for i, v in ipairs(s) do
|
||||
str = str .. _string( v, n, nopad )
|
||||
end
|
||||
end
|
||||
|
||||
return str
|
||||
end
|
||||
|
||||
function _bool( s, n, nopad )
|
||||
|
||||
local str = ""
|
||||
local pad = ""
|
||||
if not nopad then pad = "\t" end
|
||||
|
||||
if s == "1" then
|
||||
str = pad .. n .. " true\n"
|
||||
elseif s == "0" then
|
||||
str = pad .. n .. " false\n"
|
||||
end
|
||||
|
||||
return str
|
||||
end
|
||||
|
||||
function _string( s, n, nopad )
|
||||
|
||||
local str = ""
|
||||
local pad = ""
|
||||
if not nopad then pad = "\t" end
|
||||
|
||||
if s then
|
||||
if s:find("[^%d]") or n == "Port" or n == "Irq" then
|
||||
if not s:find("[^%w]") and n ~= "Port" and n ~= "Irq" then
|
||||
str = pad .. n .. " " .. luci.util.trim(s)
|
||||
else
|
||||
str = pad .. n .. ' "' .. luci.util.trim(s) .. '"'
|
||||
end
|
||||
else
|
||||
str = pad .. n .. " " .. luci.util.trim(s)
|
||||
end
|
||||
|
||||
str = str .. "\n"
|
||||
end
|
||||
|
||||
return str
|
||||
end
|
||||
|
||||
|
||||
plugins = {
|
||||
collectd = {
|
||||
{ "BaseDir", "Include", "PIDFile", "PluginDir", "TypesDB", "Interval", "ReadThreads", "Hostname" },
|
||||
{ },
|
||||
{ }
|
||||
},
|
||||
logfile = {
|
||||
{ "LogLevel", "File" },
|
||||
{ "Timestamp" },
|
||||
{ }
|
||||
},
|
||||
}
|
||||
|
||||
local plugin_dir = "/usr/share/luci/statistics/plugins/"
|
||||
for filename in nixio.fs.dir(plugin_dir) do
|
||||
local name = filename:gsub("%.json", "")
|
||||
if (name == "exec") then
|
||||
plugins[name] = config_exec
|
||||
elseif (name == "iptables") then
|
||||
plugins[name] = config_iptables
|
||||
elseif (name == "curl") then
|
||||
plugins[name] = config_curl
|
||||
elseif (name == "network") then
|
||||
plugins[name] = config_network
|
||||
else
|
||||
local plugin_def = luci.jsonc.parse(nixio.fs.readfile(plugin_dir .. filename))
|
||||
if type(plugin_def) == "table" then
|
||||
plugins[name] = plugin_def.legend
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
preprocess = {
|
||||
RRATimespans = function(val)
|
||||
local rv = { }
|
||||
for time in luci.util.imatch(val) do
|
||||
table.insert( rv, luci.util.parse_units(time) )
|
||||
end
|
||||
return table.concat(rv, " ")
|
||||
end
|
||||
}
|
||||
|
||||
|
||||
section("collectd")
|
||||
|
||||
section("logfile")
|
||||
|
||||
for plugin in pairs(plugins) do
|
||||
if (plugin ~= "collectd") and (plugin ~= "logfile") then
|
||||
section( plugin )
|
||||
end
|
||||
end
|
284
applications/luci-app-statistics/root/usr/libexec/stat-genconfig
Executable file
284
applications/luci-app-statistics/root/usr/libexec/stat-genconfig
Executable file
|
@ -0,0 +1,284 @@
|
|||
#!/usr/bin/env ucode
|
||||
/*
|
||||
Luci statistics - collectd configuration generator
|
||||
(c) 2008-2022 Jo-Philipp Wich <jo@mein.io>
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import { lsdir, open } from 'fs';
|
||||
import { cursor } from 'uci';
|
||||
|
||||
const uci = cursor();
|
||||
const sections = uci.get_all('luci_statistics');
|
||||
|
||||
const plugins = {
|
||||
collectd: [
|
||||
[ 'BaseDir', 'Include', 'PIDFile', 'PluginDir', 'TypesDB', 'Interval', 'ReadThreads', 'Hostname' ],
|
||||
[],
|
||||
[]
|
||||
],
|
||||
logfile: [
|
||||
[ 'LogLevel', 'File' ],
|
||||
[ 'Timestamp' ],
|
||||
[]
|
||||
],
|
||||
};
|
||||
|
||||
function parse_units(ustr) {
|
||||
let val = 0;
|
||||
|
||||
// unit map
|
||||
const map = {
|
||||
y : 60 * 60 * 24 * 366,
|
||||
m : 60 * 60 * 24 * 31,
|
||||
w : 60 * 60 * 24 * 7,
|
||||
d : 60 * 60 * 24,
|
||||
h : 60 * 60,
|
||||
min: 60
|
||||
};
|
||||
|
||||
// parse input string
|
||||
for (let spec in match(lc(ustr), /([0-9.]+)([a-z]*)/g)) {
|
||||
let num = +spec[1];
|
||||
let mul = map[spec[2]] ?? map[substr(spec[2], 0, 1)] ?? 1;
|
||||
|
||||
val += num * mul;
|
||||
}
|
||||
|
||||
return int(val);
|
||||
}
|
||||
|
||||
const preprocess = {
|
||||
RRATimespans: function(val) {
|
||||
return join(' ', map(split(val, /\s+/), parse_units));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
function _bool(s, n, nopad) {
|
||||
if (s == '1')
|
||||
return `${nopad ? '' : '\t'}${n} true\n`;
|
||||
|
||||
if (s == '0')
|
||||
return `${nopad ? '' : '\t'}${n} false\n`;
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
function _string(s, n, nopad) {
|
||||
if (s) {
|
||||
if (n == 'Port' || n == 'Irq' || match(s, /[^0-9]/)) {
|
||||
if (!match(s, /[^\w]/) && n != 'Port' && n != 'Irq')
|
||||
return `${nopad ? '' : '\t'}${n} ${trim(s)}\n`;
|
||||
else
|
||||
return `${nopad ? '' : '\t'}${n} "${trim(s)}"\n`;
|
||||
}
|
||||
else {
|
||||
return `${nopad ? '' : '\t'}${n} ${trim(s)}\n`;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
function _expand(s, n, nopad) {
|
||||
let str = "";
|
||||
|
||||
if (type(s) == 'string') {
|
||||
for (let v in split(s, /\s+/))
|
||||
str += _string(v, n, nopad);
|
||||
}
|
||||
else if (type(s) == 'array') {
|
||||
for (let v in s)
|
||||
str += _string(v, n, nopad);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
function _list_expand(c, l, nopad) {
|
||||
let str = '';
|
||||
|
||||
for (let n in l) {
|
||||
if (c[n]) {
|
||||
if (preprocess[n])
|
||||
c[n] = preprocess[n](c[n]);
|
||||
|
||||
let m = match(n, /^(\w+)ses$/);
|
||||
let k;
|
||||
|
||||
if (m)
|
||||
k = `${m[1]}s`;
|
||||
else
|
||||
k = replace(n, /^(\w+)s$/, '$1');
|
||||
|
||||
str += _expand(c[n], k, nopad);
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
function config_generic(c, singles, bools, lists, nopad) {
|
||||
let str = '';
|
||||
|
||||
if (c) {
|
||||
for (let key in singles) {
|
||||
if (preprocess[key])
|
||||
c[key] = preprocess[key](c[key]);
|
||||
|
||||
str += _string(c[key], key, nopad);
|
||||
}
|
||||
|
||||
for (let key in bools) {
|
||||
if (preprocess[key])
|
||||
c[key] = preprocess[key](c[key]);
|
||||
|
||||
str += _bool(c[key], key, nopad);
|
||||
}
|
||||
|
||||
if (lists)
|
||||
str += _list_expand(c, lists, nopad);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
function config_exec(c) {
|
||||
let str = "";
|
||||
|
||||
for (let k, s in sections) {
|
||||
for (let key, type in { Exec: 'collectd_exec_input', NotificationExec: 'collectd_exec_notify' }) {
|
||||
if (s['.type'] == type) {
|
||||
let cmd = replace(trim(s.cmdline), /\s+/g, '" "');
|
||||
let user = s.cmduser ?? 'nobody';
|
||||
let group = s.cmdgroup;
|
||||
|
||||
if (cmd)
|
||||
str += `\t${key} "${user}${group ? `:${group}` : ''}" "${cmd}"\n`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
function config_curl(c) {
|
||||
let str = "";
|
||||
|
||||
for (let k, s in sections) {
|
||||
if (s['.type'] == 'collectd_curl_page') {
|
||||
str += `\t<Page "${s.name}">\n`
|
||||
+ `\t\tURL "${s.url}"\n`
|
||||
+ `\t\tMeasureResponseTime true\n`
|
||||
+ `\t</Page>\n`;
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
function config_iptables(c) {
|
||||
let str = "";
|
||||
|
||||
for (let k, s in sections) {
|
||||
for (let type, verb in { collectd_iptables_match: 'Chain', collectd_iptables_match6: 'Chain6' }) {
|
||||
if (s['.type'] == type) {
|
||||
let tname = `${s.table}`;
|
||||
let chain = `${s.chain}`;
|
||||
|
||||
if (match(tname, /^\S+$/) && match(chain, /^\S+$/) && match(rule, /^\S+$/) && match(name, /^\S+$/)) {
|
||||
str += `\t${verb} "${tname}" "${chain}"`;
|
||||
|
||||
let rule = `${s.rule}`;
|
||||
|
||||
if (match(rule, /^\S+$/)) {
|
||||
str += ` "${rule}"`;
|
||||
|
||||
let name = `${s.name}`;
|
||||
|
||||
if (match(name, /^\S+$/))
|
||||
str += ` "${name}"`;
|
||||
}
|
||||
|
||||
str += '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
function config_network(c) {
|
||||
let str = '';
|
||||
|
||||
for (let k, s in sections) {
|
||||
for (let key, type in { Listen: 'collectd_network_listen', Server: 'collectd_network_server' }) {
|
||||
if (s['.type'] == type && s.host) {
|
||||
if (s.port)
|
||||
str += `\t${key} "${s.host}" "${s.port}"\n`;
|
||||
else
|
||||
str += `\t${key} "${s.host}"\n`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return str
|
||||
+ _string(c.MaxPacketSize, 'MaxPacketSize')
|
||||
+ _string(c.TimeToLive, 'TimeToLive')
|
||||
+ _bool(c.Forward, 'Forward')
|
||||
+ _bool(c.ReportStats, 'ReportStats')
|
||||
;
|
||||
}
|
||||
|
||||
function section(plugin) {
|
||||
let config = sections[`collectd_${plugin}`] ?? sections.collectd;
|
||||
|
||||
if (config && (plugin == 'collectd' || config.enable == '1')) {
|
||||
let params;
|
||||
|
||||
if (type(plugins[plugin]) == 'function')
|
||||
params = plugins[plugin](config);
|
||||
else
|
||||
params = config_generic(config, ...plugins[plugin], plugin == 'collectd');
|
||||
|
||||
if (plugin != 'collectd')
|
||||
print(`LoadPlugin ${plugin}\n${length(params) ? `<Plugin ${plugin}>\n${params}</Plugin>\n` : ''}\n`);
|
||||
else
|
||||
print(`${params ?? ''}\n`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let plugin_dir = '/usr/share/luci/statistics/plugins';
|
||||
|
||||
for (let filename in lsdir(plugin_dir)) {
|
||||
let name = replace(filename, /\.json$/, '');
|
||||
|
||||
switch (name) {
|
||||
case 'exec': plugins[name] = config_exec; break;
|
||||
case 'iptables': plugins[name] = config_iptables; break;
|
||||
case 'curl': plugins[name] = config_curl; break;
|
||||
case 'network': plugins[name] = config_network; break;
|
||||
default:
|
||||
plugins[name] = json(open(`${plugin_dir}/${filename}`))?.legend;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
section('collectd');
|
||||
section('logfile');
|
||||
|
||||
for (let plugin in plugins)
|
||||
if (plugin != 'collectd' && plugin != 'logfile')
|
||||
section(plugin);
|
Loading…
Reference in a new issue