summaryrefslogtreecommitdiffstats
path: root/applications/luci-app-statistics/htdocs/luci-static/resources/view/statistics/collectd.js
blob: 85dbbf4c249c515e70765167ca1d471c69744322 (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
'use strict';
'require view';
'require dom';
'require fs';
'require ui';
'require uci';
'require form';

return view.extend({
	load() {
		return Promise.all([
			fs.list('/usr/lib/collectd'),
			fs.list('/usr/share/luci/statistics/plugins'),
			uci.load('luci_statistics')
		]).then(function([installed, plugins]) {
			const tasks = [];

			for (let plugin of plugins) {
				tasks.push(fs.read_direct('/usr/share/luci/statistics/plugins/' + plugin.name, 'json').then(L.bind(function(name, spec) {
					return L.resolveDefault(L.require('view.statistics.plugins.' + name)).then(function(form) {
						if (!uci.get('luci_statistics', 'collectd_' + name))
							uci.add('luci_statistics', 'statistics', 'collectd_' + name);

						return {
							name: name,
							spec: spec,
							form: form,
							installed: installed.filter(function(e) { return e.name == name + '.so' }).length > 0
						};
					});
				}, this, plugin.name.replace(/\.json$/, ''))));
			}

			return Promise.all(tasks);
		});
	},

	render(plugins) {
		let m, s, o, enabled;

		for (let plugin of plugins)
			plugins[plugin.name] = plugin;

		m = new form.Map('luci_statistics', _('Collectd Settings'));
		m.tabbed = true;

		s = m.section(form.NamedSection, 'collectd', 'statistics', _('Collectd Settings'));

		o = s.option(form.Value, 'Hostname', _('Hostname'));
		o.load = function() {
			return fs.trimmed('/proc/sys/kernel/hostname').then(L.bind(function(name) {
				this.placeholder = name;
				return uci.get('luci_statistics', 'collectd', 'Hostname');
			}, this));
		};

		o = s.option(form.Value, 'BaseDir', _('Base Directory'));
		o.default = '/var/run/collectd';

		o = s.option(form.Value, 'Include', _('Directory for sub-configurations'));
		o.default = '/etc/collectd/conf.d/*.conf';

		o = s.option(form.Value, 'PluginDir', _('Directory for collectd plugins'));
		o.default = '/usr/lib/collectd/';

		o = s.option(form.Value, 'PIDFile', _('Used PID file'));
		o.default = '/var/run/collectd.pid';

		o = s.option(form.Value, 'TypesDB', _('Datasets definition file'));
		o.default = '/etc/collectd/types.db';

		o = s.option(form.Value, 'Interval', _('Data collection interval'), _('Seconds'));
		o.default = '60';

		o = s.option(form.Value, 'ReadThreads', _('Number of threads for data collection'));
		o.default = '5';

		o = s.option(form.Flag, 'FQDNLookup', _('Try to look up fully qualified hostname'));
		o.default = o.disabled;
		o.optional = true;
		o.depends('Hostname', '');

		let groupNames = [
			'general', _('General plugins'),
			'network', _('Network plugins'),
			'output', _('Output plugins')
		];

		for (let i = 0; i < groupNames.length; i += 2) {
			s = m.section(form.GridSection, 'statistics_' + groupNames[i], groupNames[i + 1]);

			s.cfgsections = L.bind(function(category) {
				return this.map.data.sections('luci_statistics', 'statistics')
					.map(function(s) { return s['.name'] })
					.filter(function(section_id) {
						const name = section_id.replace(/^collectd_/, '');
						const plugin = plugins[name];

						return (section_id.indexOf('collectd_') == 0 && plugin != null &&
						        plugin.installed && plugin.spec.category == category);
					});
			}, s, groupNames[i]);

			s.sectiontitle = function(section_id) {
				const name = section_id.replace(/^collectd_/, '');
				const plugin = plugins[name];

				return plugin ? plugin.spec.title : name
			};

			enabled = s.option(form.Flag, 'enable', _('Enabled'));
			enabled.editable = true;
			enabled.modalonly = false;
			enabled.renderWidget = function(section_id, option_index, cfgvalue) {
				const widget = form.Flag.prototype.renderWidget.apply(this, [section_id, option_index, cfgvalue]);

				widget.querySelector('input[type="checkbox"]').addEventListener('click', L.bind(function(section_id, plugin, ev) {
					if (ev.target.checked && plugin && plugin.form.addFormOptions)
						this.section.renderMoreOptionsModal(section_id);
				}, this, section_id, plugins[section_id.replace(/^collectd_/, '')]));

				return widget;
			};

			o = s.option(form.DummyValue, '_dummy', _('Status'));
			o.width = '50%';
			o.modalonly = false;
			o.textvalue = function(section_id) {
				const name = section_id.replace(/^collectd_/, '');
				const section = uci.get('luci_statistics', section_id);
				const plugin = plugins[name];

				if (section.enable != '1')
					return E('em', {}, [_('Plugin is disabled')]);

				const  summary = plugin ? plugin.form.configSummary(section) : null;
				return summary || E('em', _('none'));
			};

			s.modaltitle = function(section_id) {
				const name = section_id.replace(/^collectd_/, '');
				const plugin = plugins[name];

				return plugin ? plugin.form.title : null;
			};

			s.addModalOptions = function(s) {
				const name = s.section.replace(/^collectd_/, '');
				const plugin = plugins[name];

				if (!plugin)
					return;

				s.description = plugin.form.description;

				plugin.form.addFormOptions(s);

				const opt = s.children.filter(function(o) { return o.option == 'enable' })[0];
				if (opt)
					opt.cfgvalue = function(section_id, set_value) {
						if (arguments.length == 2)
							return form.Flag.prototype.cfgvalue.apply(this, [section_id, enabled.formvalue(section_id)]);
						else
							return form.Flag.prototype.cfgvalue.apply(this, [section_id]);
					};
			};

			s.renderRowActions = function(section_id) {
				const name = section_id.replace(/^collectd_/, '');
				const plugin = plugins[name];

				const trEl = this.super('renderRowActions', [ section_id, _('Configure…') ]);

				if (!plugin || !plugin.form.addFormOptions)
					dom.content(trEl, null);

				return trEl;
			};
		}

		return m.render();
	}
});