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

Posted in Uncategorized.