DEV Community

Cover image for How I Installed Nagios on EC2 and Created My Own Disk Monitoring Plugin
Krisha Arya
Krisha Arya

Posted on

How I Installed Nagios on EC2 and Created My Own Disk Monitoring Plugin

🌟 What is Nagios?

Nagios is a powerful open-source monitoring tool used to track:
βœ” Server health
βœ” Services (SSH, HTTP, CPU, disk, memory)
βœ” Network devices
βœ” Custom application metrics

It alerts you instantly when a service goes down.
Nagios is widely used in DevOps and IT operations for real-time monitoring.

In this guide, you’ll learn:

  • How to install Nagios Core on AWS EC2
  • How to configure hosts & services
  • How to create your own custom Nagios plugin (disk monitoring)
  • How to validate & restart Nagios configuration

Step 1: Launch an AWS EC2 Ubuntu Instance

Use Ubuntu 22.04 with port 80 open in security groups (for web UI).

Step 2: SSH into Your EC2 Instance

(Open your window powershell)

ssh -i yourkey.pem ubuntu@EC2_PUBLIC_IP
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Required Packages

sudo apt update && sudo apt upgrade -y
sudo apt install -y build-essential libgd-dev openssl libssl-dev unzip apache2 php libapache2-mod-php
Enter fullscreen mode Exit fullscreen mode

Step 4: Create Nagios User & Groups

sudo useradd nagios
sudo groupadd nagcmd
sudo usermod -aG nagcmd nagios
sudo usermod -aG nagcmd www-data
Enter fullscreen mode Exit fullscreen mode

Step 5: Download & Extract Nagios Core

cd /tmp
wget https://github.com/NagiosEnterprises/nagioscore/releases/download/nagios-4.4.14/nagios-4.4.14.tar.gz
tar -xzvf nagios-4.4.14.tar.gz
cd nagios-4.4.14
Enter fullscreen mode Exit fullscreen mode

Step 6: Configure & Compile Nagios

./configure --with-command-group=nagcmd
make all
sudo make install
sudo make install
sudo make install-init
sudo make install-commandmode
sudo make install-config
sudo make install-webconf
sudo a2enmod cgi
sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Step 7: Set Nagios Admin Login

sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Step 8: Install Nagios Plugins

cd /tmp
wget https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz
tar -xzvf nagios-plugins-2.3.3.tar.gz
cd nagios-plugins-2.3.3
./configure --with-nagios-user=nagios --with-nagios-group=nagcmd
make
sudo make install
Enter fullscreen mode Exit fullscreen mode

Step 9: Start & Enable Nagios

sudo systemctl start nagios
sudo systemctl enable nagios
sudo systemctl status nagios
Enter fullscreen mode Exit fullscreen mode

Nagios UI will be available at:

πŸ‘‰ http://EC2_PUBLIC_IP/nagios
Login: nagiosadmin

Step 10: Create Custom Configuration File

sudo nano /usr/local/nagios/etc/objects/nagios_demo.cfg
Enter fullscreen mode Exit fullscreen mode

Open main Nagios config:

sudo nano /usr/local/nagios/etc/nagios.cfg
Enter fullscreen mode Exit fullscreen mode

Add your config file:

cfg_file=/usr/local/nagios/etc/objects/nagios_demo.cfg
Enter fullscreen mode Exit fullscreen mode

Step 11: Define Hosts & Services in nagios_demo.cfg

Note: We can define more than one host and service in same nagios_demo.cfg file.

sudo nano /usr/local/nagios/etc/objects/nagios_demo.cfg
Enter fullscreen mode Exit fullscreen mode

Define two hosts:

define host{
    use                    linux-server
    host_name              nagios-prac
    address                172.31.34.62
    max_check_attempts     5
}

define host{
    use                    linux-server
    host_name              nagios-prac1
    address                172.31.34.62
    max_check_attempts     5
}
Enter fullscreen mode Exit fullscreen mode

Define SSH service:

define service{
    use                    generic-service
    host_name              nagios-prac
    service_description    SSH
    check_command          check_ssh
}
define service{
    use                    generic-service
    host_name              nagios-prac1
    service_description    SSH
    check_command          check_ssh
}
Enter fullscreen mode Exit fullscreen mode

Define HTTP (Ping) service:

define service{
    use                    generic-service
    host_name              nagios-prac
    service_description    HTTP
    check_command          check_ping!100.0,20%!500.0,60%
}

Enter fullscreen mode Exit fullscreen mode

Note: We are defining all these in same file i.e nagios_demo.cfg

Step 12: Create Custom Disk Check Plugin

Nagios allows custom plugins, so let’s create one for disk usage.

sudo nano /usr/local/nagios/libexec/check_disk_custom.sh
Enter fullscreen mode Exit fullscreen mode

Add:

#!/bin/bash

DISK_USAGE=$(df / | grep / | awk '{print $5}' | sed 's/%//')
WARN=$1
CRIT=$2

if [ "$DISK_USAGE" -ge "$CRIT" ]; then
    echo "CRITICAL - Disk usage ${DISK_USAGE}% | usage=${DISK_USAGE}"
    exit 2
elif [ "$DISK_USAGE" -ge "$WARN" ]; then
    echo "WARNING - Disk usage ${DISK_USAGE}% | usage=${DISK_USAGE}"
    exit 1
else
    echo "OK - Disk usage ${DISK_USAGE}% | usage=${DISK_USAGE}"
    exit 0
fi
Enter fullscreen mode Exit fullscreen mode

Make executable:

sudo chmod +x /usr/local/nagios/libexec/check_disk_custom.sh
Enter fullscreen mode Exit fullscreen mode

Step 13: Register Custom Command in commands.cfg

sudo nano /usr/local/nagios/etc/objects/commands.cfg
Enter fullscreen mode Exit fullscreen mode

Add:

define command {
    command_name    check_disk_custom
    command_line    /usr/local/nagios/libexec/check_disk_custom.sh $ARG1$ $ARG2$
}
Enter fullscreen mode Exit fullscreen mode

Step 14: Create Service Entry for Disk Monitoring

Make any service and use this command inside nagios_demo.cfg

define service {
    use                     generic-service
    host_name               nagios-prac
    service_description     Disk Load
    check_command           check_disk_custom!-w 20% -c 10%
}
Enter fullscreen mode Exit fullscreen mode

Step 15: Validate Configuration

sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Enter fullscreen mode Exit fullscreen mode

If no errors -> restart nagios

sudo systemctl restart nagios
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ Final Output

Your Nagios dashboard will now show:

βœ” Host status
βœ” SSH monitoring
βœ” Ping/HTTP monitoring
βœ” Disk monitoring using your custom plugin
βœ” Alerts for Warning/Critical states

Login URL:
πŸ‘‰ http://EC2_PUBLIC_IP/nagios

Top comments (0)