]> arthur.barton.de Git - netdata.git/blobdiff - plugins.d/node.d.plugin
updated shebang for compatibility
[netdata.git] / plugins.d / node.d.plugin
index 304b7aeb3317f0e726f79cd1bba5be115265d979..270351e4e5582cada1876edd54eb71d481d0c71b 100755 (executable)
@@ -1,4 +1,14 @@
-#!/usr/bin/env node
+#!/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 (#!/bin/sh).
+// Then, the second line, finds nodejs or node or js in the system path
+// and executes it with the shell parameters.
+
+// --------------------------------------------------------------------------------------------------------------------
 
 'use strict';
 
@@ -9,7 +19,6 @@ var NETDATA_PLUGINS_DIR = process.env.NETDATA_PLUGINS_DIR || __dirname;
 var NETDATA_CONFIG_DIR = process.env.NETDATA_CONFIG_DIR || '/etc/netdata';
 var NETDATA_UPDATE_EVERY = process.env.NETDATA_UPDATE_EVERY || 1;
 var NODE_D_DIR = NETDATA_PLUGINS_DIR + '/../node.d';
-NETDATA_UPDATE_EVERY = NETDATA_UPDATE_EVERY * 1000;
 
 // make sure the modules are found
 process.mainModule.paths.unshift(NODE_D_DIR + '/node_modules');
@@ -33,12 +42,17 @@ var netdata = require('netdata');
 function pluginConfig(filename) {
        var f = path.basename(filename);
 
+       // node.d.plugin configuration
        var m = f.match('.plugin' + '$');
-       if(m === null) m = f.match('.node.js' + '$');
        if(m !== null)
                return netdata.options.paths.config + '/' + f.substring(0, m.index) + '.conf';
 
-       return netdata.options.paths.config + '/' + f + '.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
@@ -97,47 +111,67 @@ function dumpError(err) {
 
 // --------------------------------------------------------------------------------------------------------------------
 // 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;
+                                               }
 
-var found_myself = false;
-process.argv.forEach(function (val, index, array) {
-       netdata.debug('PARAM: ' + val);
+                                               if(typeof netdata.options.modules[val] === 'undefined')
+                                                       netdata.options.modules[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:
-                               try {
-                                       var x = parseInt(val);
-                                       if(x > 0) {
-                                               netdata.options.update_every = x * 1000;
-                                               if(netdata.options.update_every < NETDATA_UPDATE_EVERY) {
-                                                       netdata.options.update_every = NETDATA_UPDATE_EVERY;
-                                                       netdata.debug('Update frequency ' + x + 's is too low');
+                                               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);
                                                }
-
-                                               netdata.debug('Update frequency set to ' + netdata.options.update_every + ' ms');
                                        }
-                                       else netdata.error('Ignoring parameter: ' + val);
-                               }
-                               catch(e) {
-                                       netdata.error('Cannot get value of parameter: ' + val);
-                                       dumpError(e);
-                               }
+                                       break;
+                       }
                }
-       }
-});
+       });
+}
 
-if(netdata.options.update_every < 1000) {
+if(netdata.options.update_every < 1) {
        netdata.debug('Adjusting update frequency to 1 second');
-       netdata.options.update_every = 1000;
+       netdata.options.update_every = 1;
 }
 
 // --------------------------------------------------------------------------------------------------------------------
@@ -154,7 +188,7 @@ function findModules() {
                        var n = files[len].substring(0, m.index);
 
                        if(typeof(netdata.options.modules[n]) === 'undefined')
-                               netdata.options.modules[n] = { enabled: netdata.options.modules_enable_all };
+                               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;
@@ -181,7 +215,7 @@ function findModules() {
                                // load its configuration
                                var c = {
                                        enable_autodetect: netdata.options.modules_enable_autodetect,
-                                       update_every: Math.round(netdata.options.update_every / 1000)
+                                       update_every: netdata.options.update_every
                                };
                                try {
                                        netdata.debug('loading module\'s ' + netdata.options.modules[n].name + ' config ' + netdata.options.modules[n].config_filename);
@@ -219,6 +253,7 @@ function findModules() {
                }
        }
 
+       // netdata.debug(netdata.options.modules);
        return found;
 }