Using passive checks in Nagios

Nagios offers a very powerful mechanism for scheduling tests. However, there are many situations where you might want to perform tests on your own and just tell Nagios what the result is. One of the typical scenarios for using passive tests can be when performing the actual test takes very little time but the startup overhead is large. Refer to Chapter 9Passive Checks and NRDP for details.

The next code print contains an example of an application that periodically performs tests and sends their results to Nagios over the external command pipe. This code consists of a method to supply information to Nagios, and a main loop that performs tests every five minutes. It does not contain the actual test that should be performed as this might vary depending on your needs:

var fs = require('fs'); 
var os = require('os'); 
 
var Nagios = function () { 
  var pipeFileName = 'https://510848-1853064-raikfcquaxqncofqfm.stackpathdns.com/var/nagios/rw/nagios.cmd'; 
  var writeCommand = function (command, callback) { 
    fs.appendFile(pipeFileName, command + os.EOL, callback); 
  }; 
 
  this.writeStatus = function (host, svc, code, output, callback) { 
    var time = Math.round(Date.now() / 1000); 
    writeCommand('[' + time + '] PROCESS_SERVICE_CHECK_RESULT;' + host + ';' + svc + ';' + code + ';' + output, callback); 
  }; 
}; 
 
var nagios = new Nagios(); 
 
var performTest = function () { 
  //to be implemented 
  return { 
    code: 0, 
    output: 'check_service: OK' 
  } 
}; 
 
setInterval(function () { 
  var result = performTest(); 
  nagios.writeStatus('hostname', 'service', result.code, result.output, function (err) { 
    console.log(err); 
  }); 
}, 300 * 1000); //every 5 minutes 

Note that the actual implementation of the performTest method should perform real tests.

Very often, you will need to create or extend applications to perform checks on remote machines. In this case, NRDP is used to send the check results to the Nagios server.

The following is a JavaScript code for sending service check results over NRDP. It uses the request and xmlbuilder libraries (http://github.com/request/request and http://github.com/oozcitak/xmlbuilder-js) for building an XML payload and sending it to Nagios.

var request = require('request'); 
var builder = require('xmlbuilder'); 
 
var token = 'cu8Eiquasoomeiphahpa'; 
var url = 'http://your.nagios.server/nrdp/'; 
 
 
var xml = builder.create('root').ele('checkresults').ele('checkresult', {'type': 'service'}); 
xml.ele('hostname', 'localhost'); 
xml.ele('servicename', 'HTTP'); 
xml.ele('state', '1'); 
xml.ele('output', 'check result output here'); 
 
xml = xml.end({ pretty: true}); 
 
request.post( 
    url, 
    { 
      form: { 
        cmd: 'submitcheck', 
        token: token, 
        XMLDATA: xml 
      } 
    }, 
    function (error, response, body) { 
      console.log(response.statusCode); 
      console.log(body); 
    } 
); 

In order to test it, first we need to define package.json,as follows:

{ 
  "name": "nrdp_post", 
  "version": "0.0.1", 
  "dependencies": { 
    "request": "^2.73.0", 
    "xmlbuilder": "^8.2.2" 
  } 
} 

Next, install the dependencies with npm i, and finally run it:

root@ubuntu:# node index.js
200
<?xml version="1.0" encoding="utf-8"?>
<result>
  <status>0</status>
  <message>OK</message>
  <meta>
    <output>1 checks processed.</output>
  </meta>
</result>

This will prepare the XML representation of an HTTP service check notification on the localhost machine, submit it to the NRDP endpoint, and then print out the response.

Related Articles

How to add swap space on Ubuntu 21.04 Operating System

How to add swap space on Ubuntu 21.04 Operating System

The swap space is a unique space on the disk that is used by the system when Physical RAM is full. When a Linux machine runout the RAM it use swap space to move inactive pages from RAM. Swap space can be created into Linux system in two ways, one we can create a...

read more

Lorem ipsum dolor sit amet consectetur

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

one × four =