summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-statistics/root/usr/libexec/stat-genconfig
blob: 130f8f3876149221bc094d194de372ef44eebeb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/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+$/)) {
					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);