Linux System Monitoring with Nagios

Nagios is a powerful open-source monitoring system that enables organizations to identify and resolve IT infrastructure problems before they affect critical business processes. This tutorial covers setting up Nagios Core to monitor Linux systems.

Prerequisites

  • Ubuntu Server 20.04 LTS or newer
  • Minimum 2GB RAM (4GB recommended)
  • 20GB+ of disk space
  • Root or sudo privileges
  • Static IP address configured
  • Basic knowledge of Linux command line

1 System Preparation

Update your system and install required dependencies:

sudo apt update && sudo apt upgrade -y
sudo apt install -y build-essential autoconf automake gcc libc6 make wget unzip \
apache2 php libapache2-mod-php libgd-dev libssl-dev snmp libsnmp-dev snmpd

Create a nagios user and group:

sudo useradd nagios
sudo groupadd nagcmd
sudo usermod -a -G nagcmd nagios
sudo usermod -a -G nagcmd www-data

2 Download and Install Nagios

Download the latest Nagios Core source code:

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

Compile and install Nagios:

./configure --with-httpd-conf=/etc/apache2/sites-enabled
make all
sudo make install
sudo make install-commandmode
sudo make install-init
sudo make install-config
sudo make install-webconf

Enable Apache modules and restart the service:

sudo a2enmod rewrite cgi
sudo systemctl restart apache2

3 Install Nagios Plugins

Download and install Nagios plugins:

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

Compile and install the plugins:

./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
sudo make install

4 Configure Nagios

Create a directory for additional configurations:

sudo mkdir /usr/local/nagios/etc/servers

Edit the main Nagios configuration file:

sudo nano /usr/local/nagios/etc/nagios.cfg

Uncomment or add the following line to include server configurations:

cfg_dir=/usr/local/nagios/etc/servers

Configure contacts for notifications:

sudo nano /usr/local/nagios/etc/objects/contacts.cfg

Update the email address for notifications:

define contact{
    contact_name                    nagiosadmin
    use                             generic-contact
    alias                           Nagios Admin
    email                           admin@yourdomain.com
}

5 Set Up Web Interface

Create a password for the nagiosadmin user:

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

Restart Apache to apply changes:

sudo systemctl restart apache2

Start Nagios and enable it to start on boot:

sudo systemctl start nagios
sudo systemctl enable nagios

Check the status of Nagios:

sudo systemctl status nagios

6 Configure Monitoring for Local System

Create a configuration file for the local server:

sudo nano /usr/local/nagios/etc/servers/localhost.cfg

Add the following configuration:

define host{
    use                     linux-server
    host_name               localhost
    alias                   Local Linux Server
    address                 127.0.0.1
    max_check_attempts      5
    check_period            24x7
    notification_interval   30
    notification_period     24x7
}

define service{
    use                     generic-service
    host_name               localhost
    service_description     PING
    check_command           check_ping!100.0,20%!500.0,60%
}

define service{
    use                     generic-service
    host_name               localhost
    service_description     SSH
    check_command           check_ssh
    notifications_enabled   0
}

define service{
    use                     generic-service
    host_name               localhost
    service_description     Disk Usage
    check_command           check_disk!20%!10%!/
}

define service{
    use                     generic-service
    host_name               localhost
    service_description     Current Users
    check_command           check_users!20!50
}

define service{
    use                     generic-service
    host_name               localhost
    service_description     Total Processes
    check_command           check_procs!250!400
}

define service{
    use                     generic-service
    host_name               localhost
    service_description     CPU Load
    check_command           check_load!5.0!4.0!3.0!10.0!6.0!4.0
}

7 Verify Configuration and Restart Nagios

Check the Nagios configuration for errors:

sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

If there are no errors, restart Nagios:

sudo systemctl restart nagios

8 Access Nagios Web Interface

Open your web browser and navigate to:

http://your_server_ip/nagios

Log in with the username nagiosadmin and the password you created.

Navigate to the "Hosts" or "Services" section to view monitoring status.

9 Monitor Remote Linux Hosts

On remote Linux hosts, install NRPE (Nagios Remote Plugin Executor):

# On the remote host
sudo apt update
sudo apt install -y nagios-nrpe-server nagios-plugins

Edit the NRPE configuration on the remote host:

sudo nano /etc/nagios/nrpe.cfg

Add your Nagios server IP to the allowed hosts:

allowed_hosts=127.0.0.1,your_nagios_server_ip

Restart NRPE on the remote host:

sudo systemctl restart nagios-nrpe-server

On the Nagios server, create a configuration file for the remote host:

sudo nano /usr/local/nagios/etc/servers/remote-host.cfg

Add configuration similar to the localhost but with the remote host's IP address.

10 Configure Email Notifications

Install and configure Postfix for email notifications:

sudo apt install -y postfix mailutils

During installation, select "Internet Site" and enter your domain name.

Test email functionality:

echo "Test email from Nagios" | mail -s "Test Email" your@email.com

Configure Nagios to send email notifications by ensuring the email command is properly defined in the commands configuration.

11 Advanced Configuration

Create host groups for better organization:

sudo nano /usr/local/nagios/etc/objects/hostgroups.cfg

Add host group definitions:

define hostgroup {
    hostgroup_name  linux-servers
    alias           Linux Servers
    members         localhost,remote-host
}

define hostgroup {
    hostgroup_name  web-servers
    alias           Web Servers
    members         localhost
}

Create service groups:

define servicegroup {
    servicegroup_name       disk-services
    alias                   Disk Services
    members                 localhost,Disk Usage,remote-host,Disk Usage
}

12 Troubleshooting Common Issues

Check the Nagios log for errors:

tail -f /usr/local/nagios/var/nagios.log

Common issues and solutions:

Issue Solution
NRPE connection refused Check firewall settings and NRPE configuration
Services showing as pending Check Nagios scheduling and service definitions
Email notifications not working Verify Postfix configuration and check mail logs
Web interface not loading Check Apache error logs and file permissions
Command copied to clipboard!