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