Nagios Core Installation on Linux
Nagios Core is a powerful, open-source infrastructure monitoring system that allows organizations to identify and resolve IT infrastructure problems quickly. This guide covers compilation from source, plugin installation, host and service configuration, NRPE setup, and notification configuration on Linux systems.
Table of Contents
- Introduction
- System Requirements
- Building from Source
- Installing Nagios Plugins
- Web Interface Configuration
- Basic Configuration
- Adding Hosts and Services
- NRPE Installation
- Notifications Setup
- Performance Tuning
- Troubleshooting
- Conclusion
Introduction
Nagios Core monitors systems, networks, and infrastructure to ensure optimal performance. Its extensible architecture supports thousands of monitoring plugins and can scale to monitor large, complex environments. Unlike other monitoring solutions, Nagios requires explicit configuration, providing complete control and understanding of what's being monitored.
System Requirements
Verify system compatibility before beginning:
- Linux kernel 2.6 or later
- At least 1GB RAM
- 2GB disk space
- Apache 2.0+
- GCC compiler
- Perl 5.8+
- Optional: OpenSSL for NRPE encryption
Building from Source
Step 1: Install Dependencies
# Ubuntu/Debian
sudo apt-get update
sudo apt-get install -y build-essential wget unzip apache2 php libapache2-mod-php apache2-utils
sudo apt-get install -y libssl-dev libgd-dev libpng-dev autoconf libtool
# CentOS/RHEL
sudo yum groupinstall -y "Development Tools"
sudo yum install -y wget unzip httpd php openssl-devel gd-devel libpng-devel
Step 2: Create Nagios User
sudo useradd --no-create-home --shell /bin/false nagios
sudo groupadd nagcmd
sudo usermod -a -G nagcmd nagios
sudo usermod -a -G nagcmd apache
Step 3: Download and Compile Nagios
cd /tmp
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.14.tar.gz
tar -xzf nagios-4.4.14.tar.gz
cd nagios-4.4.14
# Configure
./configure \
--prefix=/usr/local/nagios \
--exec-prefix=/usr/local/nagios \
--localstatedir=/usr/local/nagios/var \
--sysconfdir=/usr/local/nagios/etc \
--with-cgiurl=/nagios/cgi-bin \
--with-htmurl=/nagios \
--with-nagios-user=nagios \
--with-nagios-group=nagios \
--with-command-group=nagcmd
# Compile and install
make all
sudo make install
sudo make install-init
sudo make install-daemoninit
sudo make install-commandmode
sudo make install-config
sudo make install-webconf
Step 4: Create Apache Config
sudo htpasswd -cb /usr/local/nagios/etc/htpasswd.users nagiosadmin admin_password
sudo tee /etc/apache2/sites-available/nagios.conf > /dev/null << 'EOF'
<VirtualHost *:80>
ServerName nagios.example.com
<Directory /usr/local/nagios/share>
Options None
AllowOverride None
<IfVersion >= 2.4>
Require all granted
</IfVersion>
</Directory>
ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
Alias /nagios /usr/local/nagios/share
<Directory /usr/local/nagios/sbin>
Options +ExecCGI
AllowOverride None
<IfVersion >= 2.4>
Require user @nagiosadmin
</IfVersion>
AuthName "Nagios"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
</Directory>
<Directory /usr/local/nagios/share>
<IfVersion >= 2.4>
Require all granted
</IfVersion>
Options +FollowSymLinks
</Directory>
</VirtualHost>
EOF
# Enable site and CGI
sudo a2ensite nagios
sudo a2enmod cgi
sudo systemctl restart apache2
Step 5: Enable Nagios Service
sudo systemctl enable nagios
sudo systemctl start nagios
sudo systemctl status nagios
Installing Nagios Plugins
Step 1: Install Plugin Dependencies
sudo apt-get install -y libmysqlclient-dev libpq-dev libsnmp-dev libdbi-dev
Step 2: Compile and Install Plugins
cd /tmp
wget https://nagios-plugins.org/download/nagios-plugins-2.4.10.tar.gz
tar -xzf nagios-plugins-2.4.10.tar.gz
cd nagios-plugins-2.4.10
./configure \
--prefix=/usr/local/nagios \
--with-nagios-user=nagios \
--with-nagios-group=nagios
make
sudo make install
Step 3: Verify Plugin Installation
ls -la /usr/local/nagios/libexec/ | head -20
# Test a plugin
/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
Step 4: Install Additional Plugins
# Custom plugin examples
sudo tee /usr/local/nagios/libexec/check_custom_metric > /dev/null << 'EOF'
#!/bin/bash
# Example: Check application status
APP_PID=$(pidof myapp)
if [ -z "$APP_PID" ]; then
echo "CRITICAL - Application not running"
exit 2
fi
# Check if process is consuming reasonable resources
if ps -p $APP_PID -o %cpu= | awk '{if ($1 > 90) exit 0; else exit 1}'; then
echo "WARNING - High CPU usage: $(ps -p $APP_PID -o %cpu=)"
exit 1
else
echo "OK - Application running normally"
exit 0
fi
EOF
chmod +x /usr/local/nagios/libexec/check_custom_metric
sudo chown nagios:nagios /usr/local/nagios/libexec/check_custom_metric
Web Interface Configuration
Access Nagios Web Interface
Navigate to http://your-server/nagios and log in with:
- Username: nagiosadmin
- Password: (set during installation)
Dashboard Overview
The main page shows:
- Network status summary
- Host status map
- Service status
- Recent alerts
- Performance statistics
Basic Configuration
Main Configuration File
Edit /usr/local/nagios/etc/nagios.cfg:
sudo tee /usr/local/nagios/etc/nagios.cfg > /dev/null << 'EOF'
log_file=/usr/local/nagios/var/nagios.log
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
cfg_dir=/usr/local/nagios/etc/objects/hosts
object_cache_file=/usr/local/nagios/var/objects.cache
precached_object_file=/usr/local/nagios/var/precached.objects
resource_file=/usr/local/nagios/etc/resource.cfg
status_file=/usr/local/nagios/var/status.dat
status_update_interval=10
nagios_user=nagios
nagios_group=nagios
check_external_commands=1
command_check_interval=-1
command_file=/usr/local/nagios/var/rw/nagios.cmd
external_command_buffer_slots=4096
check_for_updates=1
bare_update_check=0
[email protected]
[email protected]
interval_length=60
use_syslog=1
log_notifications=1
log_service_retries=1
log_host_retries=1
log_event_handlers=1
log_initial_states=0
log_external_commands=1
log_passive_checks=1
service_inter_check_delay_method=s
host_inter_check_delay_method=s
service_interleave_factor=s
max_concurrent_checks=20
max_service_check_spread=30
max_host_check_spread=30
service_check_timeout=60
host_check_timeout=30
event_handler_timeout=30
notification_timeout=30
ocsp_timeout=5
perfdata_timeout=5
obsess_over_services=0
service_perfdata_file=/usr/local/nagios/var/service-perfdata
service_perfdata_file_template=$LASTTIMESTAMP$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$\n
service_perfdata_file_mode=a
service_perfdata_file_processing_interval=0
service_perfdata_file_processing_command=process-service-perfdata
obsess_over_hosts=0
host_perfdata_file=/usr/local/nagios/var/host-perfdata
host_perfdata_file_template=$LASTTIMESTAMP$\t$HOSTNAME$\t$HOSTOUTPUT$\t$HOSTPERFDATA$\n
host_perfdata_file_mode=a
host_perfdata_file_processing_interval=0
host_perfdata_file_processing_command=process-host-perfdata
check_for_orphaned_services=1
check_for_orphaned_hosts=1
retain_state_information=1
state_retention_file=/usr/local/nagios/var/retention.dat
retention_update_interval=60
use_retained_program_state=1
use_retained_scheduling_info=1
retained_contact_host_attribute_mask=0
retained_contact_service_attribute_mask=0
retained_process_host_attribute_mask=0
retained_process_service_attribute_mask=0
retained_host_attribute_mask=0
retained_service_attribute_mask=0
retained_scheduling_info_attribute_mask=0
EOF
Configure Contacts
Edit /usr/local/nagios/etc/objects/contacts.cfg:
define contact {
contact_name nagiosadmin
use generic-contact
alias Nagios Admin
email [email protected]
pager +1-999-999-9999
}
define contact {
contact_name ops_team
use generic-contact
alias Operations Team
email [email protected]
}
define contactgroup {
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin
}
define contactgroup {
contactgroup_name ops
alias Operations
members ops_team
}
Adding Hosts and Services
Create Host Configuration
sudo tee /usr/local/nagios/etc/objects/hosts/web-servers.cfg > /dev/null << 'EOF'
define host {
use linux-server
host_name web-server-01
alias Web Server 1
address 192.168.1.10
hostgroups web-servers
contact_groups admins,ops
}
define host {
use linux-server
host_name web-server-02
alias Web Server 2
address 192.168.1.11
hostgroups web-servers
contact_groups admins,ops
}
define hostgroup {
hostgroup_name web-servers
alias Web Servers
members web-server-01,web-server-02
}
EOF
Create Service Configuration
sudo tee /usr/local/nagios/etc/objects/hosts/web-services.cfg > /dev/null << 'EOF'
define service {
use local-service
host_name web-server-01,web-server-02
service_description CPU Load
check_command check_local_load!5.0,4.0!10.0,6.0
contact_groups admins,ops
}
define service {
use local-service
host_name web-server-01,web-server-02
service_description Disk Space
check_command check_local_disk!20%!10%!/
contact_groups admins,ops
}
define service {
use local-service
host_name web-server-01,web-server-02
service_description HTTP
check_command check_http
contact_groups admins,ops
}
define service {
use local-service
host_name web-server-01,web-server-02
service_description Memory Usage
check_command check_local_swap!20!10
contact_groups admins,ops
}
EOF
NRPE Installation
On Monitoring Server
cd /tmp
wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-4.1.1/nrpe-4.1.1.tar.gz
tar -xzf nrpe-4.1.1.tar.gz
cd nrpe-4.1.1
./configure
make all
sudo make install-plugin
sudo make install-daemon
sudo make install-daemon-config
sudo make install-daemon-init
On Remote Hosts (Linux)
# Download and compile
cd /tmp
wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-4.1.1/nrpe-4.1.1.tar.gz
tar -xzf nrpe-4.1.1.tar.gz
cd nrpe-4.1.1
./configure
make all
sudo make install-daemon
sudo make install-daemon-config
# Edit NRPE configuration
sudo sed -i "s/^allowed_hosts=.*/allowed_hosts=127.0.0.1,::1,192.168.1.10/" /usr/local/nagios/etc/nrpe.cfg
# Start service
sudo systemctl enable nrpe
sudo systemctl start nrpe
Define NRPE Commands in Nagios
sudo tee /usr/local/nagios/etc/objects/commands.cfg >> /dev/null << 'EOF'
define command {
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
define command {
command_name check_nrpe_1arg
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -a $ARG2$
}
EOF
Notifications Setup
Email Notifications
Edit commands.cfg to add email commands:
define command {
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "Notification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n" | mail -s "Host $HOSTSTATE$ alert for $HOSTNAME$" $CONTACTEMAIL$
}
define command {
command_name notify-service-by-email
command_line /usr/bin/printf "%b" "Notification Type: $NOTIFICATIONTYPE$\nService: $SERVICEDESC$\nHost: $HOSTNAME$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\nInfo: $SERVICEOUTPUT$\n" | mail -s "Service $SERVICESTATE$ alert - $HOSTNAME$/$SERVICEDESC$" $CONTACTEMAIL$
}
Custom Notification Scripts
sudo mkdir -p /usr/local/nagios/libexec/alerts
sudo tee /usr/local/nagios/libexec/alerts/slack-notification.sh > /dev/null << 'EOF'
#!/bin/bash
SLACK_WEBHOOK="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
HOSTNAME="$1"
STATE="$2"
OUTPUT="$3"
curl -X POST -H 'Content-type: application/json' \
--data "{\"text\":\"Alert: $HOSTNAME is $STATE - $OUTPUT\"}" \
$SLACK_WEBHOOK
EOF
chmod +x /usr/local/nagios/libexec/alerts/slack-notification.sh
sudo chown nagios:nagios /usr/local/nagios/libexec/alerts/slack-notification.sh
Performance Tuning
Optimize Service Checks
# In nagios.cfg
check_result_reaper_frequency=10
max_concurrent_checks=20
service_inter_check_delay_method=smart
host_inter_check_delay_method=smart
Enable Service Performance Data
sudo tee -a /usr/local/nagios/etc/nagios.cfg > /dev/null << 'EOF'
process_performance_data=1
EOF
Archive Old Logs
# Create log rotation script
sudo tee /etc/logrotate.d/nagios > /dev/null << 'EOF'
/usr/local/nagios/var/nagios.log {
monthly
rotate 12
postrotate
/bin/kill -HUP `cat /usr/local/nagios/var/nagios.pid`
endscript
}
EOF
Troubleshooting
Verify Configuration Syntax
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Check Service Status
sudo systemctl status nagios
sudo journalctl -u nagios -f
Monitor Log Files
tail -f /usr/local/nagios/var/nagios.log
Debug NRPE Connection
# Test from monitoring server
/usr/local/nagios/libexec/check_nrpe -H 192.168.1.20 -c check_load
Conclusion
Nagios Core provides granular control over infrastructure monitoring through explicit configuration. By following this guide, you've built a scalable monitoring platform capable of handling complex environments. Focus on developing comprehensive service checks tailored to your applications, implementing effective notification strategies, and regularly reviewing and updating alert thresholds. The flexibility of Nagios allows growth alongside your infrastructure while maintaining visibility into system health.


