You can setup nagios to execute commands via NRPE whenever a host or service state change occurs, these are called event handlers. Luckily they’re fairly easy to implement and the example I provide uses NRPE to issue remote event handlers.
On the nagios server:
In your command.cfg file, where I keep all of my command definitions, I am going to setup a new command called my_eventhandler. This will be the command that services use to call the remote event handler. Notice that we’re using the check_nrpe command to actually execute the remote method.
There are several things that you need to make sure are done to allow this type of usage:
- NRPE daemon was compiled with support for command arguments. There is an option for this in the configure script for NRPE when you compile it on the remote machine.
- The dont_blame_nrpe directive in this remote server’s configuration file config file is set to ’1′
define command{ command_name my_eventhandler command_line $USER1$/check_nrpe -n -H $HOSTADDRESS$ -c myhandler -a $ARG1$ }
Lets assume you already have a host definition for your site set up on the nagios server, mine is gnucom.cc. When defining a service definitions for this host, lets use check_load as an example, we’re going to include an event_handler directive like the one shown below.
define service{ use local-service host_name gnucom.cc service_description LOAD check_command check_nrpe!check_load event_handler my_eventhandler!$SERVICESTATE$ $STATETYPE$ $SERVICEATTEMPT$ notifications_enabled 1 }
This event handler will be called on every state change, so if you’re building your own custom event handler you’ll want to check to make sure what state you’re in before taking any actions.
On the remote host:
On the remote host we need to create a command definition for the command that the nagios server is issuing. We do this in the nrpe.cfg file, unless you’re defining the commands in some other external file.
command[myhandler]=/usr/local/nagios/libexec/myhandler.pl
An example script for my myhandler.pl file is listed below. It simply logs the arguments that the main server passed to us.
#!/usr/local/bin/perl use strict; open(FILE, ">>", "/usr/local/nagios/myeventhandler.log") or die "$!"; print FILE "myhandler: $ARGV[0] : $ARGV[1] : $ARGV[2]\n"; close FILE; print "DONE!";
Remember that the event handler is going to be run as user nagios (or whatever other user you decided to use). If you need to run root commands like kill or service then you’re going to have to add user nagios to the /etc/sudoers file.
Troubleshooting
- While testing you should try running commands from the nagios server to the remote host via the command line.
- If you’re getting a “NRPE: Unable to read output” don’t fret. Its because you’re script or handler isn’t producing any TEXT data when it returns. Try doing what I did above with the print statement that prints “DONE!” to standard output – that should be enough for nagios eat up.
- Need help running commands that require root privileges? Feel free to read this guide here Configuring nagios to run privileged or root commands with NRPE.
