Installing Nagios Core on Ubuntu: Monitoring Rock-Solid Infrastructure with NRPE and Custom Plugins

Monitoring tutorial - IT technology blog
Monitoring tutorial - IT technology blog

Why still use Nagios when Prometheus and Zabbix are taking over?

In the infrastructure I manage, Prometheus and Grafana handle real-time metrics for about 20 Docker Swarm clusters. However, when it comes to monitoring physical servers running legacy apps or requiring a simple binary “Up/Down” status, Nagios Core remains the top choice. It isn’t flashy, but it is incredibly resilient.

Many newcomers to the field often criticize Nagios for its dated interface and text-based configuration files which can be prone to errors. In reality, its stability is something modern tools sometimes struggle to match. Nagios runs extremely light, consuming less than 20MB of RAM and requiring no complex database to store state.

Three common monitoring approaches with Nagios

To retrieve data from remote servers, there are three primary methods:

  • Checking via public protocols (HTTP, PING, SMTP): This method only checks the surface. You know the web service is alive, but you don’t know if the server’s CPU is overloaded.
  • Checking via SNMP: This is the standard for network devices like switches or routers. However, configuring SNMP on Linux is quite cumbersome and poses security risks if using the old v2 version.
  • Checking via NRPE (Nagios Remote Plugin Executor): This is the optimal way for Linux servers. A small agent runs on the target server, executes local scripts, and returns the results to the central Nagios Server via port 5666.

Considering practical pros and cons

Strengths:
– Extremely low resource consumption, almost negligible.
– A massive repository of plugins contributed by the community over the last 20 years; there’s a script for everything.
– Infinite customization. As long as you can write Bash, Python, or Perl, you can monitor anything from server room temperature to the number of orders in a database.

Weaknesses:
– Managed entirely through .cfg files. Missing a single curly brace } can halt the entire service.
– The default interface looks like it’s from the 90s. But our goal is to receive timely alerts, not to admire pretty graphs.

Below, I will guide you through installing Nagios Core from source code for optimal performance and using NRPE for remote monitoring.

Step 1: Installing Nagios Core on Ubuntu 22.04

Start by installing the prerequisite packages to compile the source code. I’m using Ubuntu 22.04 LTS for stability.

sudo apt update
sudo apt install -y autoconf gcc libc6 make wget unzip apache2 php libapache2-mod-php libgd-dev
sudo apt install -y libmcrypt-dev libssl-dev bc gawk dc build-essential snmp libnet-snmp-perl gettext

Next, download the latest Nagios Core version to a temporary directory:

cd /tmp
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.9.tar.gz
tar xzf nagios-4.4.9.tar.gz
cd nagios-4.4.9

The build and installation process is as follows:

sudo ./configure --with-httpd-conf=/etc/apache2/sites-enabled
sudo make all

# Create system user and group
sudo make install-groups-users
sudo usermod -a -G nagios www-data

# Install binaries, daemon, and sample configuration files
sudo make install
sudo make install-daemoninit
sudo make install-commandmode
sudo make install-config
sudo make install-webconf

Finally, create a nagiosadmin account to log into the Dashboard. Don’t forget to save this password.

sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

Step 2: Installing Nagios Plugins

Nagios Core is like the brain, while Plugins are the hands and feet. Without plugins, Nagios won’t know how to check disk space or RAM status.

cd /tmp
wget https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz
tar xzf nagios-plugins-2.3.3.tar.gz
cd nagios-plugins-2.3.3

sudo ./configure
sudo make
sudo make install

Step 3: Configuring NRPE on the Client (Server to be monitored)

On the target servers (Clients), we don’t need to install the full Nagios suite. Just the NRPE agent is enough. To save time, I usually use apt instead of building from source.

sudo apt update
sudo apt install nagios-nrpe-server nagios-plugins

Open the file /etc/nagios/nrpe.cfg. Find the allowed_hosts line and add the Nagios Server’s IP to allow the connection:

allowed_hosts=127.0.0.1,10.0.0.50  # 10.0.0.50 is my Nagios Server IP

Restart the service to apply changes: sudo systemctl restart nagios-nrpe-server.

Step 4: Writing a Custom Plugin with Bash Script

This is the most interesting part. Suppose you want to alert if an application’s log folder exceeds 1GB. Create the file /usr/lib/nagios/plugins/check_log_size.sh on the Client:

#!/bin/bash
path="/var/log/myapp"
size=$(du -s $path | cut -f1)

if [ $size -gt 1048576 ]; then
    echo "CRITICAL - Directory $path occupies $(($size/1024)) MB"
    exit 2
elif [ $size -gt 524288 ]; then
    echo "WARNING - Directory $path occupies $(($size/1024)) MB"
    exit 1
else
    echo "OK - Log size is stable"
    exit 0
fi

Grant execution permissions using chmod +x. Then, simply declare this command in the nrpe.cfg file so the Nagios Server can call it remotely.

Step 5: Setting up Automatic Email Alerts

To receive emails immediately when an issue occurs, Nagios needs a Mail Transfer Agent (MTA). Postfix is the most common choice because it integrates easily with Gmail Relay or SendGrid.

sudo apt install postfix mailutils

Edit the file /usr/local/nagios/etc/objects/contacts.cfg and update the notification email:

define contact {
    contact_name            nagiosadmin
    use                     generic-contact
    alias                   Nagios Admin
    email                   [email protected]
}

Pro tip: Use SMTP services like Amazon SES. This helps prevent alert emails from ending up in the Spam folder when the system sends out dozens of emails a day.

Conclusion: When should you choose Nagios?

Nagios is like a dedicated “gatekeeper” for hybrid systems. If you manage physical servers, critical databases, or need complex business logic checks, Nagios remains extremely reliable. It’s not flashy, but it will shout loudly via Telegram or Email the moment something goes wrong. I hope this guide helps you master this classic monitoring tool.

Share: