Reading Time: 10 mins

Overview

Nagios, free and open-source software is used for continuous monitoring of system metrics, applications, network elements, server resources and custom services. It works by providing two major services, namely; monitoring service and alerting service. It is available in two different interfaces such as Nagios Core (which is open-source) and Nagios XI (which is a freemium as hosting (nodes) is limited to seven). 
The Nagios XI is further available in two different editions called Standard Edition and Enterprise Edition. The NRPE (Nagios Remote Plugin Executor) is used as an intermediate agent to connect with the remote hosts by Nagios.

Pre-conditions

  • Web Server: Apache 2.x
  • Programming Language: PHP 7.2
  • Requires firewall configuration tool, ufw (since we use Ubuntu).
  • Presuming that you are logged in as a non-root user (in which sudo privileges is necessary).
Note:
  1. Not installed Apache yet, go to, Installing and Setting Virtual Hosts for Apache on Ubuntu 18.04
  2. In case you didn’t install PHP on your Ubuntu, visit, Installing PHP On Ubuntu 18.04

Here, we use two servers. One will be used as a Nagios’ server and the other one will be used as our web server. It is also to be noted that it is best if you install Nagios on a separate server as it is troublesome to monitor several remote hosts in a single server. Only in the case of monitoring a single website, it is recommended to go with the same server. 
Table of Contents:

  1. Overview
  2. Installing Nagios Core
  3. Installing Nagios Plug-ins
  4. Installation of check_nrpe Plug-in
  5. Configuring Nagios
  6. Testing Nagios
  7. Installation of NRPE daemon with its Prerequisites
  8. Installation of Nagios Plug-ins
  9. Installation of NRPE daemon
  10. Monitoring Configuration with Nagios Server
  11. Conclusion

Installing Nagios Core

Step 1: Updating the Packages

Log in to your Nagios server and do make sure that you have installed all the required packages. Though there are numerous ways possible to install Nagios, let us stick with the trusted way of installing Nagios, i.e from its official website as it comes with updated versions and security patches. 
Before start installing Nagios Core, you need to install some of the required libraries.
To update the default packages,

sudo apt-get update

To install libraries,

sudo apt install autoconf gcc make unzip libgd-dev libmcrypt-dev libssl-dev dc snmp libnet-snmp-perl gettext

Step 2: Installing Nagios

Install Nagios using the curl command and make sure you are in a home directory  (as it is easy to identify the required packages when it is configured in the home directory). 

curl -L -O https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.4.5.tar.gz

Note: To confirm that you have installed the latest version of Nagios, go to the Nagios official website, https://www.nagios.org/downloads/nagios-core/thanks/?skip=1/

Step 3: Configuring Nagios with Apache

Extract the Nagios file which we downloaded in the previous step.

tar zxf nagios-4.4.5.tar.gz

Now, enter the extracted directory with the below command,

cd nagioscore-nagios-4.4.5

Next, to configure Nagios, enable Apache configuration with the below command: 

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

Post which, you will see an output like the below one,

Output
*** Configuration summary for nagios 4.4.5 2019-07-29 ***:
 General Options:
 -------------------------
        Nagios executable:  nagios
        Nagios user/group:  nagios,nagios
       Command user/group:  nagios,nagios
             Event Broker:  yes
        Install ${prefix}:  /usr/local/nagios
    Install ${includedir}:  /usr/local/nagios/include/nagios
                Lock file:  /run/nagios.lock
   Check result directory:  /usr/local/nagios/var/spool/checkresults
           Init directory:  /lib/systemd/system
  Apache conf.d directory:  /etc/apache2/sites-enabled
             Mail program:  /bin/mail
                  Host OS:  linux-gnu
          IOBroker Method:  epoll
 Web Interface Options:
 ------------------------
                 HTML URL:  http://localhost/nagios/
                  CGI URL:  http://localhost/nagios/cgi-bin/
 Traceroute (used by WAP):

Compile the configured Nagios using the below command:

sudo make all

For running the Nagios process, you need nagios user and nagios group.

sudo make install-groups-users

Next, you need to install the Nagios binary log files, service files and sample configuration files.

sudo make install
sudo make install-daemoninit
sudo make install-commandmode
sudo make install-config

Install and configure the apache configuration files as you need Nagios web interface for monitoring of hosts.

sudo make install-webconf

Enable the Apache rewrite and cgi modules.

sudo a2enmod rewrite
sudo a2enmod cgi

To enable third-party access (or external commands), include a separate user to the nagios group.

sudo usermod -a -G nagios www-data

Create a user, nagiosadmin in order to login in to your Nagios web interface using the htpasswd,

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

Note down the password which you enter to secure the nagiosadmin account as you’ll need it later to log into the Nagios interface. 

Note: In a case, if you are creating a user by not using the nagiosadmin name, you need to edit the file, /usr/local/nagios/etc/cgi.cfg and replace all the nagiosadmin references with the one you newly create.  
Now, restart Apache for the changes to take into effect, using the command:

sudo systemctl restart apache2

You have finished installing Nagios Core, let us now move to install Nagios plug-ins. 

Installing Nagios Plug-ins

Step 1: Downloading Plug-ins

Nagios Core works by its dependency plug-ins and therefore, it is a must to install all those plug-ins for Nagios to run properly. Some of the elements monitored by these plug-ins include,

  • Current load
  • Current users
  • Http
  • Ping status
  • SSH
  • Total processes and so forth.

Download the latest Nagios plug-ins using the curl command. 

curl -L -O https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz

To install the latest stable version of the plug-ins, go to, https://nagios-plugins.org/

Step 2: Configuring Plug-ins

Extract the Nagios plug-in which you downloaded in the previous step.

tar zxf nagios-plugins-2.2.1.tar.gz

Enter the extracted directory with the below command:

cd nagios-plugins-2.2.1

Now, configure the Nagios plug-in.

./configure

Next, it’s time to install and build the plug-ins.

sudo make
sudo make install

You have done installing the required plug-ins, yet in order to monitor the remote servers, you need to install one more plug-in called check_nrpe.

Installation of check_nrpe Plug-in

Step 1: Installing check_nrpe

Generally, Nagios uses an NRPE plug-in for monitoring of the remote hosts. This NRPE serves via two different parts.
check_nrpe – this is used by the Nagios server
NRPE daemon – this is used by a remote server or web server
Download the latest version of check_nrpe plug-in using the curl command and make sure you are in the Nagios server. 
To download the latest stable release of check_nrpe, go to the GitHub page of Nagios https://github.com/NagiosEnterprises/nrpe/releases

curl -L -O https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.2.1/nrpe-3.2.1.tar.gz

Step 2: Configuring check_nrpe

Extract the check_nrpe file which we downloaded in the previous step.

tar zxf nrpe-3.2.1.tar.gz

Next, enter the extracted directory with the command:

cd nrpe-3.2.1

Now, configure the check_nrpe plug-in.

./configure

Post the configuration step, it’s time to install and build the check_nrpe plug-in.

sudo make check_nrpe
sudo make install-plugin
Now, that you have installed check_nrpe and all its required plug-ins, you can now get going with the configuration of Nagios with check_nrpe plug-in.

Configuring Nagios

Step 1: Creating Directory

Log into your Nagios server to carry out some of the basic configurations. Open the Nagios configuration file using the below command,

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

After you open the nagios.cfg file, find the below line to make changes,

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

Uncomment the line as done in the following command,

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

If you want to maintain multiple servers, create the number of files (as needed) using the below directory,  

 /usr/local/nagios/etc/servers

Step 2: Configuring Nagios Contacts

To configure the nagios contacts use the below command,

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

Replace the email directive’s value with your email address:

define contact{
        contact_name                    nagiosadmin ; Short name of user
        use                             generic-contact ; Inherit default values from generic-contact template (defined above)
        alias                           Nagios Admin ; Full name of user
        email                           your_email@your_domain.com   ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******

Step 3: Defining check_nrpe Command

You need to define the check_nrpe command in the nagios configuration file.
Open the file called /usr/local/nagios/etc/objects/commands.cfg. using the command,

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

At the end of the file, include the following commands,

...
define command{
        command_name check_nrpe
        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

Note: It is to be noted that here, we have defined the check_nrpe command in the Nagios configuration file.
Now, start Nagios for the changes to take into effect and to restart the Nagios on system start-ups. 

sudo systemctl start nagios

To start the Nagios service, use the below command

sudo service nagios start

It’s time to test the working status of Nagios Core as we have successfully installed Nagios and check_nrpe with it’s required plug-ins.

Testing Nagios

Step 1: Accessing Nagios’ Interface

Since you have already configured and created nagiosadmin user id, you can start accessing the Nagios interface by visiting, http://nagios_server_public_ip/nagios
Enter the login credentials of Nagios web interface which you created while configuring the Nagios Core with Apache during the installation process. 
After the successful authentication, you will be able to see the Nagios web interface as shown in the below image. 
 

nagios-interface-nagios-installation

Step 2: Verifying Nagios (Monitoring) Service

After accessing the Nagios web interface, you need to check whether the Nagios monitors as expected.  
Click the ‘Hosts’ option present at the left-side panel of the Nagios homepage to view the hosts and services which Nagios monitors.

nagios-monitoring-hosts-nagios-installation
From the above image, it is confirmed that Nagios monitors the ‘localhost’.