Setting Up NRPE for Nagios on Ubuntu Servers

Step 1: Install Required Packages

sudo apt update
sudo apt install -y xinetd monitoring-plugins nagios-nrpe-server

Step 2: Configure NRPE

Edit the NRPE configuration file to allow connections from the Nagios server:

sudo vi /etc/nagios/nrpe.cfg

# Add your Nagios server's IP address to allowed_hosts
allowed_hosts=127.0.0.1, <NAGIOS_SERVER_IP>

Step 3: Restart NRPE Service

sudo systemctl restart nrpe
sudo systemctl enable nrpe

Step 4: Verify NRPE is Listening

sudo ss -tulnp | grep 5666

Step 5: Verify NRPE Commands

Use the following command from the Nagios server to verify NRPE checks:

/usr/local/nagios/libexec/check_nrpe -H <REMOTE_SERVER_IP> -c check_disk

Step 6: Add Custom Check (Optional)

If you want to add a custom memory check, create a script:

sudo vi /usr/lib/nagios/plugins/check_mem.sh

# Paste the following content into the file
#!/bin/bash
used=$(free | awk '/Mem:/ { printf("%.2f", $3/$2 * 100) }')
echo "MEMORY OK - $used% used | mem=$used%;80;90;0;100"
exit 0

# Make the script executable
sudo chmod +x /usr/lib/nagios/plugins/check_mem.sh

Step 7: Define the Command in NRPE

Add the following line to /etc/nagios/nrpe.cfg:

command[check_mem]=/usr/lib/nagios/plugins/check_mem.sh

Step 8: Final Restart

Restart the NRPE service one final time:

sudo systemctl restart nrpe

test

  • How Do I Install Nagios on an Ubuntu Server?

    To install Nagios, follow these steps:

    1. Update your system:
      bash
      sudo apt update && sudo apt upgrade -y

      <button class=”copy-code-button”>Copy Code</button>

    2. Install required dependencies:
      bash
      sudo apt install -y build-essential libgd-dev openssl libssl-dev apache2 php

      <button class=”copy-code-button”>Copy Code</button>

    3. Download and compile Nagios Core:
      bash
      wget https://github.com/NagiosEnterprises/nagioscore/releases/download/nagios-4.5.9/nagios-4.5.9.tar.gz
      tar -zxvf nagios-4.5.9.tar.gz
      cd nagios-4.5.9
      ./configure
      make all
      sudo make install

      <button class=”copy-code-button”>Copy Code</button>


add nrpe for all ubuntu servers

1. Install Required NRPE Packages

bash
sudo apt update
sudo apt install -y xinetd monitoring-plugins nagios-nrpe-server

2. Verify NRPE Installation

bash
which nrpe

3. Update /etc/nagios/nrpe.cfg Configuration
Add or update the following lines:

bash
allowed_hosts=127.0.0.1,<Nagios_Server_IP>
dont_blame_nrpe=1
command[check_disk]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /
command[check_load]=/usr/lib/nagios/plugins/check_load -w 1.0,0.75,0.5 -c 2.0,1.5,1.0
command[check_procs]=/usr/lib/nagios/plugins/check_procs -w 150 -c 200
command[check_users]=/usr/lib/nagios/plugins/check_users -w 5 -c 10
command[check_mem]=/usr/lib/nagios/plugins/check_mem.sh 80 90

4. Create and Set Permissions for check_mem.sh Script

bash
sudo vi /usr/lib/nagios/plugins/check_mem.sh

Paste the following code into the file:

bash
#!/bin/bash
USED_MEM=$(free | awk '/Mem:/ {print $3/$2 * 100.0}')
USED_MEM_INT=${USED_MEM%.*}
echo "MEMORY OK - $USED_MEM% used | mem=$USED_MEM%;$1;$2;0;100"
exit 0

Set the correct permissions:

bash
sudo chown nagios:nagios /usr/lib/nagios/plugins/check_mem.sh
sudo chmod 755 /usr/lib/nagios/plugins/check_mem.sh

5. Restart the NRPE Service

bash
sudo systemctl restart nrpe
sudo systemctl status nrpe

6. Test NRPE Commands Locally and from the Nagios Server

  • Local Test
    bash
    /usr/lib/nagios/plugins/check_nrpe -H localhost -c check_disk
    /usr/lib/nagios/plugins/check_nrpe -H localhost -c check_mem
  • Remote Test (Run from Nagios Server)
    bash
    /usr/local/nagios/libexec/check_nrpe -H <Remote_Server_IP> -c check_disk
    /usr/local/nagios/libexec/check_nrpe -H <Remote_Server_IP> -c check_mem