Managing Nagios

Your application might also want to have some control over Nagios. You might want to expose an interface for users to take control of your monitoring system, for example, a web interface or a client-server system. You might also want to handle custom authorization and access the control list. This is something that is beyond the functionality offered by the web interface that Nagios comes with.

In such cases, it is best to create your own system for reading the current status as well as for sending commands directly over the external command pipe. In both cases, this is very easy to do from any programming language.

The first thing that we can do is to show Nagios’ current status. This requires reading the status.dat file, parsing it to any data format, and then manipulating it. The format of the file is relatively simple—each object is enclosed in a section. Each section contains one or more name=value directives. For example, the following is a definition of information about the status.dat file:

info { 
   created=1388002190 
   version=4.0.1 
} 

All hosts, services, and other objects are defined in the same way as the preceding definition. There can be multiple instances of a specified object type; for example, each hoststatus object definition specifies a single host along with its current status.

Sending commands to Nagios also seems trivial. The details of the most commonly used commands were given in Chapter 8, Notifications and Events. Sending commands simply involves opening a pipe for writing, sending commands, and closing the pipe again.

Controlling Nagios from an external application written in JavaScript is easy. Thanks to the existing nagios-status-parser library (https://github.com/bahamas10/node-nagios-status-parser) that is capable of parsing the Nagios status.dat file into a JavaScript object. Consider the following sample usage:

var fs = require('fs'); 
var parse = require('nagios-status-parser'); 
var status = parse(fs.readFileSync('https://510848-1853064-raikfcquaxqncofqfm.stackpathdns.com/path/to/status.dat', 'utf8')); 
 
console.log(status.info); 

The output is as follows:

root@ubuntu:# node parse.js
[ { created: 1468269833,
version: '4.1.1',
last_update_check: 1468261385,
update_available: 0,
last_version: '4.1.1',
new_version: '4.1.1' } ]

Having such a powerful tool, it’s also relatively easy to write a function that allows you to search for objects by their type so that they match the specified criteria, for example, all the services associated with a host. A sample code to do this is as follows:

var fs = require('fs'); 
var parse = require('nagios-status-parser'); 
 
var status = parse(fs.readFileSync('https://510848-1853064-raikfcquaxqncofqfm.stackpathdns.com/path/to/status.dat', 'utf8')); 
 
var findObject = function (status, type, filter) { 
  var array = status[type]; 
  if (array) { 
    return array.filter(function(entry) { 
   return Object.keys(filter).every(function(key) { 
     return filter[key] === entry[key]; 
   }); 
    }); 
  } 
}; 
 
var type = 'servicestatus'; 
var filter = { 
  last_hard_state: 2, 
  host_name: 'localhost' 
}; 
findObject(status, type, filter).forEach(function (object) { 
  console.log(object.service_description, '-> Last state change:',        
    new Date(object.last_state_change * 1000) 
   .toLocaleDateString("en-US")); 
});  

The findObject function takes all objects of the said type and checks if all of the fields and expected values are matched to specified filtering criteria. In this case, we find all of the services on the localhost machine that have critical statuses, and display their descriptions along with the last state change date:

root@ubuntu:# node find_status.js
HTTP -> Last state change: Monday, July 04, 2016
Total Processes -> Last state change: Monday, July 04, 2016 

This approach can be used to perform complex searches and show the status depending on many configuration options.

Sending commands to Nagios from JavaScript is also a very simple thing to do. The following is a class that offers internal functions for sending commands, as well as two sample commands that cause Nagios to schedule the next host or service check on the specified date. If the date is omitted, then the check is run immediately:

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.scheduleHostCheck = function (host, when, callback) { 
    if (when === undefined) { 
      when = Math.round(Date.now() / 1000); 
    } 
    writeCommand('SCHEDULE_FORCED_HOST_CHECK;' + host + ';' + when, callback); 
  }; 
  this.scheduleServiceCheck = function (host, svc, when, callback) { 
    if (when === undefined) { 
      when = Math.round(Date.now() / 1000); 
    } 
    writeCommand('SCHEDULE_FORCED_SVC_CHECK;' + host + ';' + svc + ';' + when, callback); 
  }; 
}; 

A small section of code to test the functionality is as follows:

var nagios = new Nagios(); 
nagios.scheduleHostCheck('linux1'); 
var when = new Date(); 
when.setDate(when.getDate() + 1); // tomorrow 
nagios.scheduleServiceCheck('localhost', 'APT', Math.round(when.getTime() / 1000)); 

This initializes an instance of the Nagios class, and then schedules a host check for the linux1 machine immediately. Next, it schedules the APT service check on the localhost machine to occur one day from now.

Implementing additional commands should be as simple as specifying new functions that send commands (http://www.nagios.org/developerinfo/externalcommands) to Nagios over the external command pipe. Usually, the functionality base grows as a project grows, hence, we should not define unused functions on a just-in-case basis.

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 *

12 + 20 =