Introducing Nagios customizations

The most exciting aspect of using Nagios is the ability to combine your programming skills with the powerful engine offered by the Nagios daemon. Your own pieces of code can be plugged into the Nagios daemon, or can communicate with it in various ways.

One of the best things about Nagios is that, in most cases, it does not force you to use a specific language. Whether the language of your choice is PHP, Perl, Tcl, Python, Ruby, or Java, you can easily use it with Nagios. This is a fundamental difference between Nagios and majority of monitoring applications. Usually, an application can only be extended in the same language it is written in. Across this chapter, we will use the language that is recently gaining huge popularity—JavaScript.

Our code can cooperate with Nagios in various ways—either by implementing commands or by sending information to the Nagios daemon. The first case means that we create a script or executable that will be run by Nagios, and its output and exit code is then processed by Nagios. Running external commands is used for performing active checks, sending notifications, and triggering event handlers. Using macro substitutions and variables available in the current context (refer to http://nagios.sourceforge.net/docs/nagioscore/4/en/macrolist.html), we’re able to pass down all of the information that’s needed for the command to do its job.

The alternative method of extending Nagios is to send information to it from other applications. The first option is that external applications (such as Web or typical user interface) allow the configuration and management of the Nagios system. This is done by sending control commands over Unix sockets to Nagios. Because this involves opening and writing to a Unix socket, which works just like a file, it can be done in any programming language that handles I/O.

Yet another option is that the other applications reporting to your application or a system scheduling mechanism, such as cron, are responsible for running the checks. A test needs to be carried out on its own and the application itself is responsible for sending results back to Nagios. Results of those checks can be sent directly via a Unix socket.

Your software can also get information related to Nagios easily. All that’s needed is to monitor Nagios’ status.dat file for changes, and read it as if it contains all object definitions along with the current soft and hard states. The format of the file is quite simple, and the task of writing a parser for it is quite trivial. The file format and how to parse its contents is described later in this chapter.

There are ready to use Nagios status file parsers for multiple languages such as nagios-status-parser for JavaScript (available at https://github.com/bahamas10/node-nagios-status-parse), Pynag for Python (available at http://pynag.org/), nagios_analyzed for Ruby (available at https://github.com/jbbarth/nagios_analyzer) and there are multiple ready to use PHP solutions for parsing statuses such as Naupy (available at http://sourceforge.net/projects/naupy/).

Assuming that you need to write a piece of code on your own, the first thing you should start with is choosing the programming language. If you already know a language that would fit this task, stick to it.

Over the course of this chapter we will use the JavaScript programming language (https://en.wikipedia.org/wiki/JavaScript). Even though many people may not know all the intricacies of that language, the code will only use the basic functionality so that it is understandable to nontechnical users.

JavaScript is a very popular object-oriented scripting language that has large variety of uses. It started as a marginalized addition to web browsers, but over the years gained popularity, and it was only a matter of time when more advanced applications would be possible. As any function inside the code is treated as the object, it allows the functional programming style to be used.

To run JavaScript code, you will need a runtime environment that is able to parse and execute it. In our examples, we use node.js (https://nodejs.org) that comes with its own, very popular packages distribution system Node Package Manager (npm, https://www.npmjs.com). Among these packages are libraries that will ease up most of tasks. Consult documentation on installing node.js and npm on your system. Every node.js application should come with the special file package.json, which specifies (in dependencies section) a list of libraries the application depends on, along with their versions. Observe the following example:

{ 
  "name": "mysql_check", 
  "version": "0.0.1", 
  "dependencies": { 
    "mysql": "^2.11.1", 
    "yargs": "^4.8.0" 
  } 
} 

The contents of this file are rather straightforward: they define the name and version of our plugin, and state all dependencies—in our case, two libraries along with their minimal required versions.

Thanks to npm, there is no need to care about libraries on your own. Once the dependencies list is done, simply run the npm i (or npm install) command to get them automatically downloaded and ready to use (an Internet connection will be required). Then the only step is to actually run your code with the node <file_name.js>.

Even though we’ll focus only on one language, keep in mind that almost any technology can be used. Nagios mainly uses basic functionality for interaction—exit codes, reading the program’s output, and passing commands via a pipe. Also, all of its interaction is in text mode and both the active check output and command pipe use a very basic format.

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 *

18 − fifteen =