summaryrefslogtreecommitdiffstats
path: root/plugins.d/node.d.plugin
blob: b16203912db4c07068cb1539656f55a17fd3e9fb (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
#!/usr/bin/env bash
':' //; exec "$(command -v nodejs || command -v node || command -v js || echo "ERROR node.js IS NOT AVAILABLE IN THIS SYSTEM")" "$0" "$@"

// shebang hack from:
// http://unix.stackexchange.com/questions/65235/universal-node-js-shebang

// Initially this is run as a shell script.
// Then, the second line, finds nodejs or node or js in the system path
// and executes it with the shell parameters.

// netdata
// real-time performance and health monitoring, done right!
// (C) 2017 Costa Tsaousis <costa@tsaousis.gr>
// GPL v3+

// --------------------------------------------------------------------------------------------------------------------

'use strict';

// --------------------------------------------------------------------------------------------------------------------
// get NETDATA environment variables

var NETDATA_PLUGINS_DIR = process.env.NETDATA_PLUGINS_DIR || __dirname;
var NETDATA_CONFIG_DIR = process.env.NETDATA_CONFIG_DIR || __dirname + '/../../../../etc/netdata';
var NETDATA_UPDATE_EVERY = process.env.NETDATA_UPDATE_EVERY || 1;
var NODE_D_DIR = NETDATA_PLUGINS_DIR + '/../node.d';

// make sure the modules are found
process.mainModule.paths.unshift(NODE_D_DIR + '/node_modules');
process.mainModule.paths.unshift(NODE_D_DIR);


// --------------------------------------------------------------------------------------------------------------------
// load required modules

var fs = require('fs');
var url = require('url');
var util = require('util');
var http = require('http');
var path = require('path');
var extend = require('extend');
var netdata = require('netdata');


// --------------------------------------------------------------------------------------------------------------------
// configuration

function pluginConfig(filename) {
    var f = path.basename(filename);

    // node.d.plugin configuration
    var m = f.match('.plugin' + '$');
    if(m !== null)
        return netdata.options.paths.config + '/' + f.substring(0, m.index) + '.conf';

    // node.d modules configuration
    m = f.match('.node.js' + '$');
    if(m !== null)
        return netdata.options.paths.config + '/node.d/' + f.substring(0, m.index) + '.conf';

    return netdata.options.paths.config + '/node.d/' + f + '.conf';
}

// internal defaults
extend(true, netdata.options, {
    filename: path.basename(__filename),

    update_every: NETDATA_UPDATE_EVERY,

    paths: {
        plugins: NETDATA_PLUGINS_DIR,
        config: NETDATA_CONFIG_DIR,
        modules: [],
    },

    modules_enable_autodetect: true,
    modules_enable_all: true,
    modules: {},
});
netdata.options.config_filename = pluginConfig(__filename);

// load configuration file
try {
    netdata.options_loaded = JSON.parse(fs.readFileSync(netdata.options.config_filename, 'utf8'));
    extend(true, netdata.options, netdata.options_loaded);

    if(!netdata.options.paths.plugins)
        netdata.options.paths.plugins = NETDATA_PLUGINS_DIR;

    if(!netdata.options.paths.config)
        netdata.options.paths.config = NETDATA_CONFIG_DIR;

    // console.error('merged netdata object:');
    // console.error(util.inspect(netdata, {depth: 10}));
}
catch(e) {
    netdata.error('Cannot read configuration file ' + netdata.options.config_filename + ': ' + e.message + ', using internal defaults.');
    netdata.options_loaded = undefined;
    dumpError(e);
}


// apply module paths to node.js process
function applyModulePaths() {
    var len = netdata.options.paths.modules.length;
    while(len--)
        process.mainModule.paths.unshift(netdata.options.paths.modules[len]);
}
applyModulePaths();


// --------------------------------------------------------------------------------------------------------------------
// tracing

function dumpError(err) {
    if (typeof err === 'object') {
        if (err.stack) {
            netdata.debug(err.stack);
        }
    }
}

// --------------------------------------------------------------------------------------------------------------------
// get command line arguments
{
    var found_myself = false;
    var found_number = false;
    var found_modules = false;
    process.argv.forEach(function (val, index, array) {
        netdata.debug('PARAM: ' + val);

        if(!found_myself) {
            if(val === __filename)
                found_myself = true;
        }
        else {
            switch(val) {
                case 'debug':
                    netdata.options.DEBUG = true;
                    netdata.debug('DEBUG enabled');
                    break;

                default:
                    if(found_number === true) {
                        if(found_modules === false) {
                            for(var i in netdata.options.modules)
                                netdata.options.modules[i].enabled = false;
                        }

                        if(typeof netdata.options.modules[val] === 'undefined')
                            netdata.options.modules[val] = {};

                        netdata.options.modules[val].enabled = true;
                        netdata.options.modules_enable_all = false;
                        netdata.debug('enabled module ' + val);
                    }
                    else {
                        try {
                            var x = parseInt(val);
                            if(x > 0) {
                                netdata.options.update_every = x;
                                if(netdata.options.update_every < NETDATA_UPDATE_EVERY) {
                                    netdata.options.update_every = NETDATA_UPDATE_EVERY;
                                    netdata.debug('Update frequency ' + x + 's is too low');
                                }

                                found_number = true;
                                netdata.debug('Update frequency set to ' + netdata.options.update_every + ' seconds');
                            }
                            else netdata.error('Ignoring parameter: ' + val);
                        }
                        catch(e) {
                            netdata.error('Cannot get value of parameter: ' + val);
                            dumpError(e);
                        }
                    }
                    break;
            }
        }
    });
}

if(netdata.options.update_every < 1) {
    netdata.debug('Adjusting update frequency to 1 second');
    netdata.options.update_every = 1;
}

// --------------------------------------------------------------------------------------------------------------------
// find modules

function findModules() {
    var found = 0;

    var files = fs.readdirSync(NODE_D_DIR);
    var len = files.length;
    while(len--) {
        var m = files[len].match('.node.js' + '$');
        if(m !== null) {
            var n = files[len].substring(0, m.index);

            if(typeof(netdata.options.modules[n]) === 'undefined')
                netdata.options.modules[n] = { name: n, enabled: netdata.options.modules_enable_all };

            if(netdata.options.modules[n].enabled === true) {
                netdata.options.modules[n].name = n;
                netdata.options.modules[n].filename = NODE_D_DIR + '/' + files[len];
                netdata.options.modules[n].loaded = false;

                if(typeof(netdata.options.modules[n].config_filename) !== 'string')
                    netdata.options.modules[n].config_filename = pluginConfig(files[len]);

                // load the module
                try {
                    netdata.debug('loading module ' + netdata.options.modules[n].filename);
                    netdata.options.modules[n].module = require(netdata.options.modules[n].filename);
                    netdata.options.modules[n].module.name = n;
                    netdata.debug('loaded module ' + netdata.options.modules[n].name + ' from ' + netdata.options.modules[n].filename);
                }
                catch(e) {
                    netdata.options.modules[n].enabled = false;
                    netdata.error('Cannot load module: ' + netdata.options.modules[n].filename + ' exception: ' + e);
                    dumpError(e);
                    continue;
                }

                // load its configuration
                var c = {
                    enable_autodetect: netdata.options.modules_enable_autodetect,
                    update_every: netdata.options.update_every
                };
                try {
                    netdata.debug('loading module\'s ' + netdata.options.modules[n].name + ' config ' + netdata.options.modules[n].config_filename);
                    var c2 = JSON.parse(fs.readFileSync(netdata.options.modules[n].config_filename, 'utf8'));
                    extend(true, c, c2);
                    netdata.debug('loaded module\'s ' + netdata.options.modules[n].name + ' config ' + netdata.options.modules[n].config_filename);
                }
                catch(e) {
                    netdata.error('Cannot load module\'s ' + netdata.options.modules[n].name + ' config from ' + netdata.options.modules[n].config_filename + ' exception: ' + e + ', using internal defaults.');
                    dumpError(e);
                }

                // call module auto-detection / configuration
                try {
                    netdata.modules_configuring++;
                    netdata.debug('Configuring module ' + netdata.options.modules[n].name);
                    var serv = netdata.configure(netdata.options.modules[n].module, c, function() {
                        netdata.debug('Configured module ' + netdata.options.modules[n].name);
                        netdata.modules_configuring--;
                    });

                    netdata.debug('Configuring module ' + netdata.options.modules[n].name + ' reports ' + serv + ' eligible services.');
                }
                catch(e) {
                    netdata.modules_configuring--;
                    netdata.options.modules[n].enabled = false;
                    netdata.error('Failed module auto-detection: ' + netdata.options.modules[n].name + ' exception: ' + e + ', disabling module.');
                    dumpError(e);
                    continue;
                }

                netdata.options.modules[n].loaded = true;
                found++;
            }
        }
    }

    // netdata.debug(netdata.options.modules);
    return found;
}

if(findModules() === 0) {
    netdata.error('Cannot load any .node.js module from: ' + NODE_D_DIR);
    netdata.disableNodePlugin();
    process.exit(1);
}


// --------------------------------------------------------------------------------------------------------------------
// start

function start_when_configuring_ends() {
    if(netdata.modules_configuring > 0) {
        netdata.debug('Waiting modules configuration, still running ' + netdata.modules_configuring);
        setTimeout(start_when_configuring_ends, 500);
        return;
    }

    netdata.modules_configuring = 0;
    netdata.start();
}
start_when_configuring_ends();

//netdata.debug('netdata object:')
//netdata.debug(netdata);