]> arthur.barton.de Git - netdata.git/blob - plugins.d/node.d.plugin
added command line arguments in node.d.plugin to enabled selected modules; refactored...
[netdata.git] / plugins.d / node.d.plugin
1 #!/usr/bin/env node
2
3 'use strict';
4
5 // --------------------------------------------------------------------------------------------------------------------
6 // get NETDATA environment variables
7
8 var NETDATA_PLUGINS_DIR = process.env.NETDATA_PLUGINS_DIR || __dirname;
9 var NETDATA_CONFIG_DIR = process.env.NETDATA_CONFIG_DIR || '/etc/netdata';
10 var NETDATA_UPDATE_EVERY = process.env.NETDATA_UPDATE_EVERY || 1;
11 var NODE_D_DIR = NETDATA_PLUGINS_DIR + '/../node.d';
12 NETDATA_UPDATE_EVERY = NETDATA_UPDATE_EVERY * 1000;
13
14 // make sure the modules are found
15 process.mainModule.paths.unshift(NODE_D_DIR + '/node_modules');
16 process.mainModule.paths.unshift(NODE_D_DIR);
17
18
19 // --------------------------------------------------------------------------------------------------------------------
20 // load required modules
21
22 var fs = require('fs');
23 var url = require('url');
24 var http = require('http');
25 var path = require('path');
26 var extend = require('extend');
27 var netdata = require('netdata');
28
29
30 // --------------------------------------------------------------------------------------------------------------------
31 // configuration
32
33 function pluginConfig(filename) {
34         var f = path.basename(filename);
35
36         var m = f.match('.plugin' + '$');
37         if(m === null) m = f.match('.node.js' + '$');
38         if(m !== null)
39                 return netdata.options.paths.config + '/' + f.substring(0, m.index) + '.conf';
40
41         return netdata.options.paths.config + '/' + f + '.conf';
42 }
43
44 // internal defaults
45 extend(true, netdata.options, {
46         filename: path.basename(__filename),
47
48         update_every: NETDATA_UPDATE_EVERY,
49
50         exit_after_ms: 3600 * 4 * 1000,
51
52         paths: {
53                 plugins: NETDATA_PLUGINS_DIR,
54                 config: NETDATA_CONFIG_DIR,
55                 modules: [],
56         },
57
58         modules_enable_autodetect: true,
59         modules_enable_all: true,
60         modules: {},
61 });
62 netdata.options.config_filename = pluginConfig(__filename);
63
64 // load configuration file
65 try {
66         netdata.options_loaded = JSON.parse(fs.readFileSync(netdata.options.config_filename, 'utf8'));
67         extend(true, netdata.options, netdata.options_loaded);
68         console.error('merged netdata object:');
69         console.error(netdata);
70 }
71 catch(e) {
72         netdata.error('Cannot read configuration file ' + netdata.options.config_filename + ': ' + e.message + ', using internal defaults.');
73         netdata.options_loaded = undefined;
74         dumpError(e);
75 }
76
77
78 // apply module paths to node.js process
79 function applyModulePaths() {
80         var len = netdata.options.paths.modules.length;
81         while(len--)
82                 process.mainModule.paths.unshift(netdata.options.paths.modules[len]);
83 }
84 applyModulePaths();
85
86
87 // --------------------------------------------------------------------------------------------------------------------
88 // tracing
89
90 function dumpError(err) {
91         if (typeof err === 'object') {
92                 if (err.stack) {
93                         netdata.debug(err.stack);
94                 }
95         }
96 }
97
98 // --------------------------------------------------------------------------------------------------------------------
99 // get command line arguments
100 {
101         var found_myself = false;
102         var found_number = false;
103         var found_modules = false;
104         process.argv.forEach(function (val, index, array) {
105                 netdata.debug('PARAM: ' + val);
106
107                 if(!found_myself) {
108                         if(val === __filename)
109                                 found_myself = true;
110                 }
111                 else {
112                         switch(val) {
113                                 case 'debug':
114                                         netdata.options.DEBUG = true;
115                                         netdata.debug('DEBUG enabled');
116                                         break;
117
118                                 default:
119                                         if(found_number === true) {
120                                                 if(found_modules === false) {
121                                                         for(var i in netdata.options.modules)
122                                                                 netdata.options.modules[i].enabled = false;
123                                                 }
124
125                                                 if(typeof netdata.options.modules[val] === 'undefined')
126                                                         netdata.options.modules[val] = {};
127
128                                                 netdata.options.modules[val].enabled = true;
129                                                 netdata.options.modules_enable_all = false;
130                                                 netdata.debug('enabled module ' + val);
131                                         }
132                                         else {
133                                                 try {
134                                                         var x = parseInt(val);
135                                                         if(x > 0) {
136                                                                 netdata.options.update_every = x * 1000;
137                                                                 if(netdata.options.update_every < NETDATA_UPDATE_EVERY) {
138                                                                         netdata.options.update_every = NETDATA_UPDATE_EVERY;
139                                                                         netdata.debug('Update frequency ' + x + 's is too low');
140                                                                 }
141
142                                                                 found_number = true;
143                                                                 netdata.debug('Update frequency set to ' + netdata.options.update_every + ' ms');
144                                                         }
145                                                         else netdata.error('Ignoring parameter: ' + val);
146                                                 }
147                                                 catch(e) {
148                                                         netdata.error('Cannot get value of parameter: ' + val);
149                                                         dumpError(e);
150                                                 }
151                                         }
152                                         break;
153                         }
154                 }
155         });
156 }
157
158 if(netdata.options.update_every < 1000) {
159         netdata.debug('Adjusting update frequency to 1 second');
160         netdata.options.update_every = 1000;
161 }
162
163 // --------------------------------------------------------------------------------------------------------------------
164 // find modules
165
166 function findModules() {
167         var found = 0;
168
169         var files = fs.readdirSync(NODE_D_DIR);
170         var len = files.length;
171         while(len--) {
172                 var m = files[len].match('.node.js' + '$');
173                 if(m !== null) {
174                         var n = files[len].substring(0, m.index);
175
176                         if(typeof(netdata.options.modules[n]) === 'undefined')
177                                 netdata.options.modules[n] = { enabled: netdata.options.modules_enable_all };
178
179                         if(netdata.options.modules[n].enabled === true) {
180                                 netdata.options.modules[n].name = n;
181                                 netdata.options.modules[n].filename = NODE_D_DIR + '/' + files[len];
182                                 netdata.options.modules[n].loaded = false;
183
184                                 if(typeof(netdata.options.modules[n].config_filename) !== 'string')
185                                         netdata.options.modules[n].config_filename = pluginConfig(files[len]);
186
187                                 // load the module
188                                 try {
189                                         netdata.debug('loading module ' + netdata.options.modules[n].filename);
190                                         netdata.options.modules[n].module = require(netdata.options.modules[n].filename);
191                                         netdata.options.modules[n].module.name = n;
192                                         netdata.debug('loaded module ' + netdata.options.modules[n].name + ' from ' + netdata.options.modules[n].filename);
193                                 }
194                                 catch(e) {
195                                         netdata.options.modules[n].enabled = false;
196                                         netdata.error('Cannot load module: ' + netdata.options.modules[n].filename + ' exception: ' + e);
197                                         dumpError(e);
198                                         continue;
199                                 }
200
201                                 // load its configuration
202                                 var c = {
203                                         enable_autodetect: netdata.options.modules_enable_autodetect,
204                                         update_every: Math.round(netdata.options.update_every / 1000)
205                                 };
206                                 try {
207                                         netdata.debug('loading module\'s ' + netdata.options.modules[n].name + ' config ' + netdata.options.modules[n].config_filename);
208                                         var c2 = JSON.parse(fs.readFileSync(netdata.options.modules[n].config_filename, 'utf8'));
209                                         extend(true, c, c2);
210                                         netdata.debug('loaded module\'s ' + netdata.options.modules[n].name + ' config ' + netdata.options.modules[n].config_filename);
211                                 }
212                                 catch(e) {
213                                         netdata.error('Cannot load module\'s ' + netdata.options.modules[n].name + ' config from ' + netdata.options.modules[n].config_filename + ' exception: ' + e + ', using internal defaults.');
214                                         dumpError(e);
215                                 }
216
217                                 // call module auto-detection / configuration
218                                 try {
219                                         netdata.modules_configuring++;
220                                         netdata.debug('Configuring module ' + netdata.options.modules[n].name);
221                                         var serv = netdata.configure(netdata.options.modules[n].module, c, function() {
222                                                 netdata.debug('Configured module ' + netdata.options.modules[n].name);
223                                                 netdata.modules_configuring--;
224                                         });
225
226                                         netdata.debug('Configuring module ' + netdata.options.modules[n].name + ' reports ' + serv + ' eligible services.');
227                                 }
228                                 catch(e) {
229                                         netdata.modules_configuring--;
230                                         netdata.options.modules[n].enabled = false;
231                                         netdata.error('Failed module auto-detection: ' + netdata.options.modules[n].name + ' exception: ' + e + ', disabling module.');
232                                         dumpError(e);
233                                         continue;
234                                 }
235
236                                 netdata.options.modules[n].loaded = true;
237                                 found++;
238                         }
239                 }
240         }
241
242         return found;
243 }
244
245 if(findModules() === 0) {
246         netdata.error('Cannot load any .node.js module from: ' + NODE_D_DIR);
247         netdata.disableNodePlugin();
248         process.exit(1);
249 }
250
251
252 // --------------------------------------------------------------------------------------------------------------------
253 // start
254
255 function start_when_configuring_ends() {
256         if(netdata.modules_configuring > 0) {
257                 netdata.debug('Waiting modules configuration, still running ' + netdata.modules_configuring);
258                 setTimeout(start_when_configuring_ends, 500);
259                 return;
260         }
261
262         netdata.modules_configuring = 0;
263         netdata.start();
264 }
265 start_when_configuring_ends();
266
267 //netdata.debug('netdata object:')
268 //netdata.debug(netdata);