π 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
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
Step 4: Create Nagios User & Groups
sudo useradd nagios
sudo groupadd nagcmd
sudo usermod -aG nagcmd nagios
sudo usermod -aG nagcmd www-data
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
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
Step 7: Set Nagios Admin Login
sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
sudo systemctl restart apache2
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
Step 9: Start & Enable Nagios
sudo systemctl start nagios
sudo systemctl enable nagios
sudo systemctl status nagios
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
Open main Nagios config:
sudo nano /usr/local/nagios/etc/nagios.cfg
Add your config file:
cfg_file=/usr/local/nagios/etc/objects/nagios_demo.cfg
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
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
}
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
}
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%
}
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
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
Make executable:
sudo chmod +x /usr/local/nagios/libexec/check_disk_custom.sh
Step 13: Register Custom Command in commands.cfg
sudo nano /usr/local/nagios/etc/objects/commands.cfg
Add:
define command {
command_name check_disk_custom
command_line /usr/local/nagios/libexec/check_disk_custom.sh $ARG1$ $ARG2$
}
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%
}
Step 15: Validate Configuration
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
If no errors -> restart nagios
sudo systemctl restart nagios
π 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)