d92693525a1b421e6fc709a28fcc16754f23a08f
[project/luci.git] / applications / luci-app-statistics / htdocs / luci-static / resources / view / statistics / collectd.js
1 'use strict';
2 'require fs';
3 'require ui';
4 'require uci';
5 'require form';
6
7 return L.view.extend({
8 load: function() {
9 return Promise.all([
10 fs.list('/usr/lib/collectd'),
11 fs.list('/usr/share/luci/statistics/plugins'),
12 uci.load('luci_statistics')
13 ]).then(function(data) {
14 var installed = data[0],
15 plugins = data[1],
16 tasks = [];
17
18 for (var i = 0; i < plugins.length; i++) {
19 tasks.push(fs.read_direct('/usr/share/luci/statistics/plugins/' + plugins[i].name, 'json').then(L.bind(function(name, spec) {
20 return L.resolveDefault(L.require('view.statistics.plugins.' + name)).then(function(form) {
21 if (!uci.get('luci_statistics', 'collectd_' + name))
22 uci.add('luci_statistics', 'statistics', 'collectd_' + name);
23
24 return {
25 name: name,
26 spec: spec,
27 form: form,
28 installed: installed.filter(function(e) { return e.name == name + '.so' }).length > 0
29 };
30 });
31 }, this, plugins[i].name.replace(/\.json$/, ''))));
32 }
33
34 return Promise.all(tasks);
35 });
36 },
37
38 render: function(plugins) {
39 var m, s, o, enabled;
40
41 for (var i = 0; i < plugins.length; i++)
42 plugins[plugins[i].name] = plugins[i];
43
44 m = new form.Map('luci_statistics', _('Collectd Settings'));
45 m.tabbed = true;
46
47 s = m.section(form.NamedSection, 'collectd', 'statistics', _('Collectd Settings'));
48
49 o = s.option(form.Value, 'Hostname', _('Hostname'));
50 o.load = function() {
51 return fs.trimmed('/proc/sys/kernel/hostname').then(L.bind(function(name) {
52 this.placeholder = name;
53 return uci.get('collectd', 'statistics', 'hostname');
54 }, this));
55 };
56
57 o = s.option(form.Value, 'BaseDir', _('Base Directory'));
58 o.default = '/var/run/collectd';
59
60 o = s.option(form.Value, 'Include', _('Directory for sub-configurations'));
61 o.default = '/etc/collectd/conf.d/*.conf';
62
63 o = s.option(form.Value, 'PluginDir', _('Directory for collectd plugins'));
64 o.default = '/usr/lib/collectd/';
65
66 o = s.option(form.Value, 'PIDFile', _('Used PID file'));
67 o.default = '/var/run/collectd.pid';
68
69 o = s.option(form.Value, 'TypesDB', _('Datasets definition file'));
70 o.default = '/etc/collectd/types.db';
71
72 o = s.option(form.Value, 'Interval', _('Data collection interval'), _('Seconds'));
73 o.default = '60';
74
75 o = s.option(form.Value, 'ReadThreads', _('Number of threads for data collection'));
76 o.default = '5';
77
78 o = s.option(form.Flag, 'FQDNLookup', _('Try to lookup fully qualified hostname'));
79 o.default = o.disabled;
80 o.optional = true;
81 o.depends('Hostname', '');
82
83 var groupNames = [
84 'general', _('General plugins'),
85 'network', _('Network plugins'),
86 'output', _('Output plugins')
87 ];
88
89 for (var i = 0; i < groupNames.length; i += 2) {
90 s = m.section(form.GridSection, 'statistics_' + groupNames[i], groupNames[i + 1]);
91
92 s.cfgsections = L.bind(function(category) {
93 return this.map.data.sections('luci_statistics', 'statistics')
94 .map(function(s) { return s['.name'] })
95 .filter(function(section_id) {
96 var name = section_id.replace(/^collectd_/, ''),
97 plugin = plugins[name];
98
99 return (section_id.indexOf('collectd_') == 0 && plugin != null &&
100 plugin.installed && plugin.spec.category == category);
101 });
102 }, s, groupNames[i]);
103
104 s.sectiontitle = function(section_id) {
105 var name = section_id.replace(/^collectd_/, ''),
106 plugin = plugins[name];
107
108 return plugin ? plugin.spec.title : name
109 };
110
111 enabled = s.option(form.Flag, 'enable', _('Enabled'));
112 enabled.editable = true;
113 enabled.modalonly = false;
114 enabled.renderWidget = function(section_id, option_index, cfgvalue) {
115 var widget = form.Flag.prototype.renderWidget.apply(this, [section_id, option_index, cfgvalue]);
116
117 widget.querySelector('input[type="checkbox"]').addEventListener('click', L.bind(function(section_id, plugin, ev) {
118 if (ev.target.checked && plugin && plugin.form.addFormOptions)
119 this.section.renderMoreOptionsModal(section_id);
120 }, this, section_id, plugins[section_id.replace(/^collectd_/, '')]));
121
122 return widget;
123 };
124
125 o = s.option(form.DummyValue, '_dummy', _('Status'));
126 o.width = '50%';
127 o.modalonly = false;
128 o.textvalue = function(section_id) {
129 var name = section_id.replace(/^collectd_/, ''),
130 section = uci.get('luci_statistics', section_id),
131 plugin = plugins[name];
132
133 if (section.enable != '1')
134 return E('em', {}, [_('Plugin is disabled')]);
135
136 var summary = plugin ? plugin.form.configSummary(section) : null;
137 return summary || E('em', _('none'));
138 };
139
140 s.modaltitle = function(section_id) {
141 var name = section_id.replace(/^collectd_/, ''),
142 plugin = plugins[name];
143
144 return plugin ? plugin.form.title : null;
145 };
146
147 s.addModalOptions = function(s) {
148 var name = s.section.replace(/^collectd_/, ''),
149 plugin = plugins[name];
150
151 if (!plugin)
152 return;
153
154 s.description = plugin.form.description;
155
156 plugin.form.addFormOptions(s);
157
158 var opt = s.children.filter(function(o) { return o.option == 'enable' })[0];
159 if (opt)
160 opt.cfgvalue = function(section_id, set_value) {
161 if (arguments.length == 2)
162 return form.Flag.prototype.cfgvalue.apply(this, [section_id, enabled.formvalue(section_id)]);
163 else
164 return form.Flag.prototype.cfgvalue.apply(this, [section_id]);
165 };
166 };
167
168 s.renderRowActions = function(section_id) {
169 var name = section_id.replace(/^collectd_/, ''),
170 plugin = plugins[name];
171
172 var trEl = this.super('renderRowActions', [ section_id, _('Configureā€¦') ]);
173
174 if (!plugin || !plugin.form.addFormOptions)
175 L.dom.content(trEl, null);
176
177 return trEl;
178 };
179 }
180
181 return m.render();
182 }
183 });