123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- #!/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(type(val) == 'array' ? val : 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+$/)) {
- 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 config_mqtt(c) {
- let str = "";
- for (let k, s in sections) {
- if (s['.type'] === 'collectd_mqtt_block') {
- const isPublish = s['blocktype'] === 'Publish';
- str += isPublish ? `\t<Publish "${s.name}">\n` : `\t<Subscribe "${s.name}">\n`;
- str += `\t\tHost "${s.Host}"\n`;
- str += s['Port'] ? `\t\tPort ${s.Port}\n` : '';
- str += s['User'] ? `\t\tUser "${s.User}"\n` : '';
- str += s['Password'] ? `\t\tPassword "${s.Password}"\n` : '';
- str += s['Qos'] ? `\t\tQos ${s.Qos}\n` : '';
- str += s['Prefix'] ? `\t\tPrefix ${s.Prefix}\n` : '';
- str += s['Retain'] ? `\t\tRetain ${s.Retain}\n` : '';
- str += s['StoreRates'] ? `\t\tRetain ${s.StoreRates}\n` : '';
- str += s['CleanSession'] ? `\t\tRetain ${s.CleanSession}\n` : '';
- str += s['Topic'] ? `\t\tTopic "${s.Topic}"\n` : '';
- str += isPublish ? `\t</Publish>\n` : `\t</Subscribe>\n`;
- }
- }
- return str;
- }
- 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;
- case 'mqtt': plugins[name] = config_mqtt; 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);
|