← All posts

Running Remote Commands for Nagios with NRPE

Nagios and NRPE logos
Image source

NRPE stands for Nagios Remote Plugin Executor. It lets you run commands on remote hosts. NRPE consists of two parts:

  • On the monitoring server running Nagios (Monitoring Host), you need to install the nrpe plugin.
  • On the remote hosts (Remote Host), you install the “nrpe-server” agent.

Diagram

We will use SSL for connections, so NRPE needs to be built with SSL support everywhere.

Installing the Agent on FreeBSD

I have already covered how to install Nagios on the Monitoring Host running FreeBSD.

Log in to the Remote Host and install the agent:

cd /usr/ports/
portsnap fetch update
cd /usr/ports/net-mgmt/nrpe2
make config

Select:

[*] SSL   Enable SSL support (disables plain-text server)

Continue the installation:

make install clean
echo 'nrpe2_enable="YES"' >> /etc/rc.conf

Log in to the Monitoring Host and install the same port, since this port provides both the agent and the plugin. Just do not put nrpe2_enable in /etc/rc.conf.

Installing the Agent on Debian

I have already covered how to install Nagios on the Monitoring Host running Debian.

Log in to the Remote Host and install the agent:

apt-get install nagios-nrpe-server

Log in to the Monitoring Host and install the plugin:

apt-get install nagios-nrpe-plugin

Configuring the Agent on the Remote Host

After installation, a configuration file will be available on the Remote Host:

  • on FreeBSD: /usr/local/etc/nrpe.cfg
  • on Debian: /etc/nagios/nrpe.cfg

Open it for editing. You need to allow the Monitoring Host to connect to the Remote Host:

server_address=203.0.113.42
allowed_hosts=127.0.0.1,203.0.113.41

Here, the IP addresses are:

  • “203.0.113.42” — the external address of the current server, i.e. the Remote Host;
  • “203.0.113.41” — the address of the Monitoring Host server.

In the same file, you can define commands that can be called from the Monitoring Host:

$ grep -E "^command\[check_" nrpe.cfg
command[check_users]=/usr/local/libexec/nagios/check_users -w 5 -c 10
command[check_load]=/usr/local/libexec/nagios/check_load -w 15,10,5 -c 30,25,20
command[check_root]=/usr/local/libexec/nagios/check_disk -w 20% -c 10% -p /
command[check_var]=/usr/local/libexec/nagios/check_disk -w 20% -c 10% -p /var
command[check_tmp]=/usr/local/libexec/nagios/check_disk -w 20% -c 10% -p /tmp
command[check_usr]=/usr/local/libexec/nagios/check_disk -w 20% -c 10% -p /usr
command[check_zombie_procs]=/usr/local/libexec/nagios/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/libexec/nagios/check_procs -w 150 -c 200

You can also specify two directives that allow you to include configuration files. This makes it better to move the commands into separate files:

# Присоединить определенный конфигурационный файл
include=/path/to/my_commands.cfg
# Или целый каталог
include_dir=/path/to/nrpe.d/

Now you can start the agent:

# Во FreeBSD
/usr/local/etc/rc.d/nrpe2 start

# На Debian
/etc/init.d/nagios-nrpe-server start

Configuring the Plugin on the Monitoring Host for FreeBSD

Log in to the Monitoring Host, open the file /usr/local/etc/nagios/objects/commands.cfg for editing, and add the following command:

# NRPE
define command{
    command_name    check_remote_command
    command_line    $USER1$/check_nrpe2 -H $HOSTADDRESS$ -c $ARG1$
    }

Now open the services file, for example /usr/local/etc/nagios/objects/remotehosts.cfg (which we created earlier), and add the external services:

define service{
    use                             remote-service
    hostgroups                      remote-servers
    service_description             Current Users
    check_command                   check_remote_command!check_users
    }
define service{
    use                             remote-service
    hostgroups                      remote-servers
    service_description             Zombie Processes
    check_command                   check_remote_command!check_zombie_procs
    }
define service{
    use                             remote-service
    hostgroups                      remote-servers
    service_description             Total Processes
    check_command                   check_remote_command!check_total_procs
    }

and so on.

Then restart Nagios for the changes to take effect:

/usr/local/etc/rc.d/nagios restart

Configuring the Plugin on the Monitoring Host for Debian

Log in to the Monitoring Host, go to the /etc/nagios-plugins/config directory, and open the file check_nrpe.cfg. You should see something like this:

# this command runs a program $ARG1$ with arguments $ARG2$
define command {
    command_name    check_nrpe
    command_line    /usr/lib/nagios/plugins/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -a $ARG2$
}
# this command runs a program $ARG1$ with no arguments
define command {
    command_name    check_nrpe_1arg
    command_line    /usr/lib/nagios/plugins/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

Now open the services file, for example /etc/nagios3/conf.d/remotehosts.cfg (which we created earlier here and here), and add the external services:

define service{
    use                             remote-service
    hostgroups                      remote-servers
    service_description             Current Users
    check_command                   check_nrpe_1arg!check_users
    }
define service{
    use                             remote-service
    hostgroups                      remote-servers
    service_description             Zombie Processes
    check_command                   check_nrpe_1arg!check_zombie_procs
    }
define service{
    use                             remote-service
    hostgroups                      remote-servers
    service_description             Total Processes
    check_command                   check_nrpe_1arg!check_total_procs
    }

Then restart Nagios for the changes to take effect:

service nagios3 restart

I also recommend reading my post on common problems with NRPE.

Comments are closed. Read the original discussion in Russian →