Configure Nagios Core server with NRPE agents for distributed monitoring across multiple servers. Set up remote host checks, service monitoring, and centralized alerting for comprehensive infrastructure oversight.
Prerequisites
- Root or sudo access
- Multiple Linux servers to monitor
- Basic knowledge of systemctl and Apache
- Email server or SMTP relay for notifications
What this solves
Nagios Core provides centralized monitoring for servers, services, and network devices across your infrastructure. This setup uses NRPE (Nagios Remote Plugin Executor) to run checks on remote hosts, giving you visibility into CPU usage, disk space, memory consumption, and custom services from a single dashboard.
Step-by-step installation
Update system packages
Start by updating your package manager on both the Nagios server and remote hosts.
sudo apt update && sudo apt upgrade -yInstall build dependencies on Nagios server
Install the packages needed to compile Nagios Core from source.
sudo apt install -y wget build-essential apache2 php libapache2-mod-php7.4 php-gd libgd-dev unzipCreate Nagios user and group
Create a dedicated system user for running Nagios services.
sudo groupadd nagios
sudo useradd -g nagios -d /home/nagios -s /bin/bash nagios
sudo usermod -a -G nagios www-dataDownload and compile Nagios Core 4.5
Download the latest Nagios Core source code and compile it.
cd /tmp
wget https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.5.0.tar.gz
tar xzf nagios-4.5.0.tar.gz
cd nagioscore-nagios-4.5.0Configure and compile the source code.
sudo ./configure --with-httpd-conf=/etc/apache2/sites-enabled
sudo make all
sudo make install
sudo make install-init
sudo make install-config
sudo make install-webconfDownload and install Nagios plugins
Install the official Nagios monitoring plugins for basic system checks.
cd /tmp
wget https://github.com/nagios-plugins/nagios-plugins/archive/release-2.4.6.tar.gz
tar xzf release-2.4.6.tar.gz
cd nagios-plugins-release-2.4.6
sudo ./tools/setup
sudo ./configure
sudo make
sudo make installCreate Nagios web interface user
Set up authentication for the Nagios web interface.
sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadminEnter a strong password when prompted. This user will have full access to the Nagios web interface.
Configure Apache web server
Enable required Apache modules and start the web server.
sudo a2enmod rewrite
sudo a2enmod cgi
sudo systemctl enable apache2
sudo systemctl start apache2Start and enable Nagios service
Configure Nagios to start automatically and verify the configuration.
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfgIf the configuration check passes, start the service.
sudo systemctl enable nagios
sudo systemctl start nagiosConfigure NRPE on remote hosts
Install NRPE daemon on remote hosts
Install NRPE and monitoring plugins on each server you want to monitor.
sudo apt install -y nagios-nrpe-server nagios-plugins-basicConfigure NRPE daemon
Edit the NRPE configuration to allow connections from your Nagios server.
allowed_hosts=127.0.0.1,203.0.113.10
command[check_users]=/usr/lib/nagios/plugins/check_users -w 5 -c 10
command[check_load]=/usr/lib/nagios/plugins/check_load -r -w .15,.10,.05 -c .30,.25,.20
command[check_hda1]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/lib/nagios/plugins/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/lib/nagios/plugins/check_procs -w 150 -c 200
command[check_disk_root]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /
command[check_memory]=/usr/lib/nagios/plugins/check_mem -w 80 -c 90Replace 203.0.113.10 with your Nagios server's IP address.
Configure firewall for NRPE
Open port 5666 for NRPE communication on remote hosts.
sudo ufw allow from 203.0.113.10 to any port 5666Start NRPE service on remote hosts
Enable and start the NRPE daemon on each remote host.
sudo systemctl enable nrpe
sudo systemctl start nrpeConfigure distributed monitoring on Nagios server
Install check_nrpe plugin
Install the NRPE plugin on your Nagios server to communicate with remote hosts.
sudo apt install -y nagios-nrpe-pluginDefine NRPE command
Add the check_nrpe command definition to your Nagios configuration.
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}Create host configuration for remote servers
Create a configuration file for your remote hosts.
define host{
use linux-server
host_name web-server-01
alias Web Server 01
address 203.0.113.20
max_check_attempts 5
check_period 24x7
notification_interval 30
notification_period 24x7
}
define hostgroup{
hostgroup_name remote-servers
alias Remote Linux Servers
members web-server-01
}Define service checks for remote hosts
Configure specific service checks that will run via NRPE.
define service{
use generic-service
host_name web-server-01
service_description CPU Load
check_command check_nrpe!check_load
}
define service{
use generic-service
host_name web-server-01
service_description Current Users
check_command check_nrpe!check_users
}
define service{
use generic-service
host_name web-server-01
service_description Root Disk Usage
check_command check_nrpe!check_disk_root
}
define service{
use generic-service
host_name web-server-01
service_description Total Processes
check_command check_nrpe!check_total_procs
}Include remote server configuration
Add your remote server configuration to the main Nagios config file.
# Add this line to include remote server configurations
cfg_file=/usr/local/nagios/etc/objects/remote-servers.cfgConfigure contact groups and notifications
Set up email notifications for monitoring alerts.
define contact{
contact_name nagiosadmin
use generic-contact
alias Nagios Admin
email admin@example.com
}
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin
}Configure alerting and notifications
Install mail transport agent
Install a mail server to send notification emails.
sudo apt install -y postfix mailutilsConfigure Postfix as a satellite system when prompted during installation.
Test email notifications
Verify that email notifications work properly.
echo "Test email from Nagios server" | mail -s "Nagios Test" admin@example.comConfigure notification commands
Ensure notification commands are properly defined in your commands configuration.
define command{
command_name notify-host-by-email
command_line /usr/bin/printf "%b" " Nagios \n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mail -s " $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ " $CONTACTEMAIL$
}
define command{
command_name notify-service-by-email
command_line /usr/bin/printf "%b" " Nagios \n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/mail -s " $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ " $CONTACTEMAIL$
}Restart Nagios and verify configuration
Check the configuration and restart all services.
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfgIf no errors are reported, restart the services.
sudo systemctl restart nagios
sudo systemctl restart apache2Set up monitoring dashboards
Configure firewall for web interface
Allow HTTP traffic to access the Nagios web interface.
sudo ufw allow 'Apache Full'Access the web interface
Open your web browser and navigate to the Nagios web interface.
http://your-nagios-server-ip/nagiosLog in with the username nagiosadmin and the password you set earlier. You should see the main Nagios dashboard with your hosts and services.
Create custom service groups
Organize your services into logical groups for better dashboard organization.
define servicegroup{
servicegroup_name system-resources
alias System Resources
members web-server-01,CPU Load,web-server-01,Root Disk Usage
}
define servicegroup{
servicegroup_name processes
alias Process Monitoring
members web-server-01,Total Processes,web-server-01,Current Users
}Verify your setup
Test the complete monitoring setup with these verification commands.
# Check Nagios service status
sudo systemctl status nagios
# Verify NRPE connectivity from Nagios server
/usr/lib/nagios/plugins/check_nrpe -H 203.0.113.20
# Test specific NRPE commands
/usr/lib/nagios/plugins/check_nrpe -H 203.0.113.20 -c check_load
/usr/lib/nagios/plugins/check_nrpe -H 203.0.113.20 -c check_disk_root
# Check configuration syntax
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfgAll checks should return OK status. If you encounter issues, check the centralized logging setup to help troubleshoot problems across your infrastructure.
Common issues
| Symptom | Cause | Fix |
|---|---|---|
| NRPE connection refused | Firewall blocking port 5666 | Open port 5666 on remote host firewall |
| CHECK_NRPE: Socket timeout | NRPE daemon not running | sudo systemctl start nrpe on remote host |
| NRPE: Command not defined | Command missing in nrpe.cfg | Add command definition in /etc/nagios/nrpe.cfg |
| Nagios web interface 403 error | Apache configuration issue | Check /etc/apache2/sites-enabled/nagios.conf permissions |
| Email notifications not working | Postfix not configured | Configure Postfix relay host and test with mail command |
| Service checks return critical | Incorrect plugin path | Verify plugin paths in /etc/nagios/nrpe.cfg |
Next steps
- Integrate Nagios with Grafana dashboards for advanced monitoring visualization
- Configure Nagios SNMP monitoring for network devices with automated discovery and templates
- Set up Nagios high availability clustering for redundant monitoring
- Configure Nagios custom plugins for application-specific monitoring
- Integrate Nagios with Prometheus Alertmanager for unified alerting
Running this in production?
Automated install script
Run this to automate the entire setup
#!/usr/bin/env bash
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Default values
NAGIOS_VERSION="4.5.0"
PLUGINS_VERSION="2.4.6"
NAGIOS_SERVER_IP=""
INSTALL_TYPE=""
usage() {
echo "Usage: $0 <server|remote> [nagios_server_ip]"
echo " server: Install Nagios Core server"
echo " remote: Install NRPE client (requires nagios_server_ip)"
exit 1
}
cleanup() {
echo -e "${RED}Installation failed. Cleaning up...${NC}"
systemctl stop nagios 2>/dev/null || true
systemctl stop nrpe 2>/dev/null || true
rm -rf /tmp/nagios* /tmp/release-* 2>/dev/null || true
}
trap cleanup ERR
log() {
echo -e "${GREEN}$1${NC}"
}
warn() {
echo -e "${YELLOW}$1${NC}"
}
error() {
echo -e "${RED}$1${NC}"
}
# Detect OS and set package manager
if [ -f /etc/os-release ]; then
. /etc/os-release
case "$ID" in
ubuntu|debian)
PKG_MGR="apt"
PKG_INSTALL="apt install -y"
PKG_UPDATE="apt update && apt upgrade -y"
WEB_USER="www-data"
WEB_SERVICE="apache2"
APACHE_CONF_DIR="/etc/apache2/sites-enabled"
FIREWALL_CMD="ufw"
;;
almalinux|rocky|centos|rhel|ol|fedora)
PKG_MGR="dnf"
PKG_INSTALL="dnf install -y"
PKG_UPDATE="dnf update -y"
WEB_USER="apache"
WEB_SERVICE="httpd"
APACHE_CONF_DIR="/etc/httpd/conf.d"
FIREWALL_CMD="firewall-cmd"
;;
amzn)
PKG_MGR="yum"
PKG_INSTALL="yum install -y"
PKG_UPDATE="yum update -y"
WEB_USER="apache"
WEB_SERVICE="httpd"
APACHE_CONF_DIR="/etc/httpd/conf.d"
FIREWALL_CMD="firewall-cmd"
;;
*)
error "Unsupported distribution: $ID"
exit 1
;;
esac
else
error "Cannot detect OS distribution"
exit 1
fi
check_root() {
if [[ $EUID -ne 0 ]]; then
error "This script must be run as root or with sudo"
exit 1
fi
}
install_server() {
log "[1/10] Updating system packages..."
$PKG_UPDATE
log "[2/10] Installing build dependencies..."
if [[ "$PKG_MGR" == "apt" ]]; then
$PKG_INSTALL wget build-essential apache2 php libapache2-mod-php php-gd libgd-dev unzip openssl libssl-dev
else
$PKG_INSTALL epel-release
dnf groupinstall -y "Development Tools"
$PKG_INSTALL httpd php php-cli gcc glibc glibc-common wget unzip gd gd-devel openssl-devel
fi
log "[3/10] Creating Nagios user and group..."
groupadd -f nagios
useradd -g nagios -d /home/nagios -s /bin/bash nagios 2>/dev/null || true
usermod -a -G nagios $WEB_USER
log "[4/10] Downloading and compiling Nagios Core..."
cd /tmp
wget -O nagios-${NAGIOS_VERSION}.tar.gz https://github.com/NagiosEnterprises/nagioscore/archive/nagios-${NAGIOS_VERSION}.tar.gz
tar xzf nagios-${NAGIOS_VERSION}.tar.gz
cd nagioscore-nagios-${NAGIOS_VERSION}
./configure --with-httpd-conf=$APACHE_CONF_DIR
make all
make install
make install-init
make install-config
make install-webconf
log "[5/10] Downloading and installing Nagios plugins..."
cd /tmp
wget -O nagios-plugins-${PLUGINS_VERSION}.tar.gz https://github.com/nagios-plugins/nagios-plugins/archive/release-${PLUGINS_VERSION}.tar.gz
tar xzf nagios-plugins-${PLUGINS_VERSION}.tar.gz
cd nagios-plugins-release-${PLUGINS_VERSION}
./tools/setup
./configure
make
make install
log "[6/10] Installing NRPE..."
cd /tmp
wget -O nrpe-4.1.0.tar.gz https://github.com/NagiosEnterprises/nrpe/archive/nrpe-4.1.0.tar.gz
tar xzf nrpe-4.1.0.tar.gz
cd nrpe-nrpe-4.1.0
./configure
make check_nrpe
make install-plugin
log "[7/10] Setting permissions..."
chown -R nagios:nagios /usr/local/nagios
chmod 755 /usr/local/nagios
chmod 755 /usr/local/nagios/etc
chmod 644 /usr/local/nagios/etc/*
log "[8/10] Configuring web interface..."
echo -e "nagiosadmin\nnagiosadmin" | htpasswd -c -i /usr/local/nagios/etc/htpasswd.users nagiosadmin
warn "Default password set to 'nagiosadmin' - please change this!"
log "[9/10] Configuring web server..."
if [[ "$PKG_MGR" == "apt" ]]; then
a2enmod rewrite
a2enmod cgi
fi
systemctl enable $WEB_SERVICE
systemctl start $WEB_SERVICE
log "[10/10] Starting Nagios service..."
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
systemctl enable nagios
systemctl start nagios
log "Nagios Core installation completed!"
log "Web interface: http://$(hostname -I | awk '{print $1}')/nagios"
log "Username: nagiosadmin"
warn "Password: nagiosadmin (CHANGE THIS!)"
}
install_remote() {
if [[ -z "$NAGIOS_SERVER_IP" ]]; then
error "Nagios server IP required for remote installation"
usage
fi
log "[1/5] Updating system packages..."
$PKG_UPDATE
log "[2/5] Installing NRPE and plugins..."
if [[ "$PKG_MGR" == "apt" ]]; then
$PKG_INSTALL nagios-nrpe-server nagios-plugins-basic nagios-plugins-standard
NRPE_CONFIG="/etc/nagios/nrpe.cfg"
PLUGINS_DIR="/usr/lib/nagios/plugins"
else
$PKG_INSTALL epel-release
$PKG_INSTALL nrpe nagios-plugins-all
NRPE_CONFIG="/etc/nagios/nrpe.cfg"
PLUGINS_DIR="/usr/lib64/nagios/plugins"
fi
log "[3/5] Configuring NRPE..."
cp $NRPE_CONFIG $NRPE_CONFIG.bak
sed -i "s/allowed_hosts=127.0.0.1/allowed_hosts=127.0.0.1,$NAGIOS_SERVER_IP/" $NRPE_CONFIG
cat >> $NRPE_CONFIG << EOF
# Custom commands
command[check_users]=$PLUGINS_DIR/check_users -w 5 -c 10
command[check_load]=$PLUGINS_DIR/check_load -r -w .15,.10,.05 -c .30,.25,.20
command[check_disk_root]=$PLUGINS_DIR/check_disk -w 20% -c 10% -p /
command[check_zombie_procs]=$PLUGINS_DIR/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=$PLUGINS_DIR/check_procs -w 150 -c 200
EOF
chmod 644 $NRPE_CONFIG
chown root:root $NRPE_CONFIG
log "[4/5] Configuring firewall..."
if [[ "$FIREWALL_CMD" == "ufw" ]]; then
ufw --force enable
ufw allow from $NAGIOS_SERVER_IP to any port 5666
else
systemctl enable firewalld
systemctl start firewalld
firewall-cmd --permanent --add-rich-rule="rule family=\"ipv4\" source address=\"$NAGIOS_SERVER_IP\" port protocol=\"tcp\" port=\"5666\" accept"
firewall-cmd --reload
fi
log "[5/5] Starting NRPE service..."
systemctl enable nrpe
systemctl start nrpe
log "NRPE installation completed!"
log "Service listening on port 5666 for connections from $NAGIOS_SERVER_IP"
}
verify_installation() {
if [[ "$INSTALL_TYPE" == "server" ]]; then
if systemctl is-active --quiet nagios && systemctl is-active --quiet $WEB_SERVICE; then
log "✓ Nagios Core server is running"
else
error "✗ Nagios services are not running properly"
fi
else
if systemctl is-active --quiet nrpe; then
log "✓ NRPE service is running"
else
error "✗ NRPE service is not running properly"
fi
fi
}
# Main execution
check_root
if [[ $# -eq 0 ]]; then
usage
fi
INSTALL_TYPE="$1"
case "$INSTALL_TYPE" in
server)
install_server
;;
remote)
if [[ $# -lt 2 ]]; then
error "Nagios server IP required for remote installation"
usage
fi
NAGIOS_SERVER_IP="$2"
install_remote
;;
*)
usage
;;
esac
verify_installation
log "Installation completed successfully!"
Review the script before running. Execute with: bash install.sh