cancel
Showing results for 
Search instead for 
Did you mean: 
John_Alam_45640
Historic F5 Account

Problem this snippet solves:

HTTP and HTTPS monitor using node.js. Beginning with 12.0 you can run node.js from the CLI just like you do Perl, Python, or TCL or Bash. This script uses the node.js engine to run a client. It also conforms to the standards used by external monitors. You can use the same monitor for both HTTP and HTTPS.

How to use this snippet:

1) Import into BigIP as an external monitor file under: System ››File Management >>External Monitor Program File List.

2) Create an external monitor using name entered in step 1. Under Local Traffic ›› Monitors

3) Add variables to monitor as follows, order is not important:

  • path: This is the path and page name which you use for testing. e.g. /myapp/default.aspx, or /, or /owa
  • recv: This is the receive string which will indicate proper operation.
  • timeout_seconds: This is the request time out after which node will stop waiting for a response.
  • secure: Any value for this variable will indicate that you want to use HTTPS. Omit this if you want HTTP.

    0151T000003d9DGQAY.PNG

4) Assign monitor to a pool

Code :

/**
 *  HTTP/HTTPS monitor for use with node.js which is built into Bigip CLI starting with version 12.0.
 *  Written by John Alam, nov-2016.
 *  
 *  no dependencies other than built-in http and https modules.
 *  use as a replacement for external HTTP monitors.  node.js scales very well.  In theory this can be used
 *  when deploying thousands of http or https monitors.
 *  
 *  To use this you must define the following variables in the monitor definition screen:
 *  path:  this is the path and page name which you use for testing.  e.g. /myapp/default.aspx, or simply /, or /owa
 *  recv:  This is the receive string which will indicate proper operation.
 *  timeout_seconds:  This is the request time out after which node will stop waiting for a response.
 *  secure:  Any value for this variable will indicate that you want to use https instead of http.
 *  
 *  The IP address and port number are passed along from tmm to the monitor.  These are picked up as arguments. 
 *  
 *  To use this file:
 *  1) import it as an external monitor under:  System  ››  File Management : External Monitor Program File List, and give it a name 
 *  2) create an external type monitor referencing the name in step 1)
 *  3) add variables as mentioned above.
 *  4) assign monitor to the pool.
 *  
 *  
 */
#!/usr/bin/node

if(typeof process.env.secure !== "undefined") {
    var http = require('https');
} else {
    var http = require('http');
} 


var recv = process.env.recv;
var timeout_seconds = process.env.timeout_seconds * 1000;
var path = process.env.path;


var ip_addr = process.argv[2].substr(7);
var port = process.argv[3];


var options = {
  host: ip_addr,
  port: port,
  path: path,
  headers: { 'User-Agent': 'Http monitor', 'acpet' : '*/*' }
};

callback = function(response) {
  var str = '';

  //another chunk of data has been recieved, so append it to `str`
  response.on('data', function (chunk) {
    str += chunk;
  });

  //the whole response has been recieved, so we just inspect
  response.on('end', function () {
    if ( str.indexOf(recv) > 0 ) {
        console.log('UP');
    } else {
        console.log('Not found');
    }
  });
}

var req = http.request(options, callback);
req.setTimeout(timeout_seconds, function() {
        this.abort();
});
req.on('error', function(err) {
        console.log(err);
        return;
});


req.end();

Tested this on version:

12.0
Comments
Algebraic_Mirro
Cirrostratus
Cirrostratus

Wouldn't this block of code cause the monitor to always be up?

 

req.on('error', function(err) { console.log(err); return; });

For an external monitor, isn't any output at all (even an error message) taken as an indicator of success? Wouldn't this error catching portion need to remain absolutely silent to get the desired result?

Version history
Last update:
‎17-Nov-2016 20:23
Updated by:
Contributors