Support Documentation Plugin Development
development_120Plugin Development

Plugins for monitor are small programs, that does the actual monitoring of host and services. They may be written in any programming language, although C and Perl are most common. They also need to follow a few rules, in order for monitor to interpret their results.

  1. The exit-code of the program denotes the status of the check according to this table:
    Exit code Status (as shown in GUI)
    0 OK
    1 Warning
    2 Critical
    3 Unknown
  2. The first line of output by the program will be shown in "Status Information" field in the GUI.

Arguments are passed from monitor to the plugin in the standard way through command-line switches. There are a few arguments that most well-written plugins support:

Switch Description
-h/--help Print usage information.
-t/--timeout Timeout in seconds.
-w/--warning Threshold for warning.
-c/--critical Threshold for critical.


The purpose of the --timeout switch is to not let the plugin hang nagios, by specifying a timeout that the plugin may not exceed.
The --warning and --critical parameters are of course not applicable in all plugins. They are used for comparison when a plugin checks a numerical value.

Timeout

In order to accomplish a bounded run-time on the plugin the alarm() function is used to send a SIGALRM signal to the process.
In Perl you can use the following code:

use lib "/opt/plugins"; # Import predefined status-codes and timeout variable use utils qw($TIMEOUT %ERRORS); $TIMEOUT = 15; # use 15 seconds as timeout # Timeout handler used $SIG{'ALRM'} = sub { print "CRITICAL: Plugin timed out after $TIMEOUT seconds\n"; exit($ERRORS{'CRITICAL'}); }; # Install timeout handler, so that the plugin does not hang indefinitely. alarm($TIMEOUT);


In C the following code can be used. At the start of the file:

#include "common.h" #include "utils.h"

this includes the timeout_alarm_handler() function. In order to make it fire after some time:

signal (SIGALRM, timeout_alarm_handler); alarm (timeout_interval);

Examples

Since many people find examples an easy way of learning, the following links provides an example of a really simple plugin written in both Perl and C. The plugin takes one argument, a filename, and returns OK if the file exists and CRITICAL if it does not. They also include timeout-handling.

In order to use the perl example you only need the copy the file into the /opt/plugins directory of the monitor-server. In order to compile the C-version, you need to rebuild the plugins-package from source. The source can be found at the Nagiosplugins-website (latest sources are listed at sourceforge).

Perl example C example

Links

Source for plugins.
Nagios plugin developer guidelines.

Bookmark and Share