Configure Nagios to use Embedded Perl and write plugins with Perl

After following the Nagios monitoring system quick setup guide, you can use the existing plugins provided by the nagios-plugins package to monitor your hosts and services. However sometimes there is no existing plugin that meets your need, then you need to write it by yourself. It is not that hard. Following we use an example to describe how to do this.

First the interface between Nagios daemon and a service check plugin is very simple. By default Nagios daemon forks a process to run the plugin and in the simplest case, the plugin outputs one line of descriptive text and then exit with 0 (OK), 1 (WARNING), 2 (CRITICAL) or 3 (UNKNOWN). That's all. Simple, isn't it? :)

You can develop your plugins in whatever programming languages you want. However, most of the time with Perl is probably the easiest. Because it is a bit costly to fork a Perl executable to run Perl based script, Nagios can also be configured to use embedded Perl interpreter so running each plugin is just a library call. When writing your Perl script, please follow the following best practices:

To configure Nagios to use embedded Perl interpreter, you just need to add the two options --enable-embedded-perl --with-perlcache. If you follow our Nagios monitoring system quick setup guide, you just need to reconfigure Nagios and install the new executables with the following commands.

./configure --with-nagios-user=joe --with-nagios-group=users --prefix=/opt/dev/nagios \
  --enable-embedded-perl --with-perlcache
# Builds Nagios daemon and CGI programs
gmake all
# Reinstall Nagios daemon and CGI programs
gmake install

Please note that you shouldn't reinstall the sample configuration files, otherwise your local changes will be gone. By default, the $NAGIOS/etc/nagios.cfg file has the following settings that enable the embedded Perl interpreter already so you don't need to change anything.

enable_embedded_perl=1
use_embedded_perl_implicitly=1

Now you can start writing service check plugin with Perl. As an example, suppose we want to check how many players are currently online at the Lords of War and Money, a free browser based game. If the number is below a minimum one say 200, then we issue a warning, otherwise it is OK.

The forum on Lords of War and Money does not require you to login and it shows the number of players on the top right of the screen. You can use WWW::Mechanize, the Perl based web automation tool to do this check very easily. For your convenience, you can download the sample Perl script to check number of Lords of War and Money online players and adapt it to your local environment. Then you can copy it to the $NAGIOS/libexec directory which is the default place for all Nagios service check plugins.

Now we are ready to define the Nagios command and service for checking number of LordsWM players online. Open the $NAGIOS/etc/objects/commands.cfg file and add the following lines to define the check command:

define command{
  command_name check_lordswm
  command_line $USER1$/check_online_number.pl $ARG1$
}

Here the $ARGS1 is the optional argument accepted by the plugin. It specifies the minimum number of players online for the plugin to return OK status.

Now modify the $NAGIOS/etc/objects/localhost.cfg file to add the LordWM online players service check.

define service{
  use local-service ; Name of service template to use
  host_name localhost
  service_description Check LordsWM users
  check_command check_lordswm!500
  normal_check_interval 30 ; Check the service every 30 minutes under normal conditions
  retry_check_interval 30 ; Re-check the service every 30 minutes until a hard state can be determined
  notifications_enabled 0
}

Here you can see that we can modify the normal and retry check intervals because we don't want to storm the server. You can also note that even though the plugin checks a remote server, it can still be put into the localhost.

References

Back to articles on development