Debian

How to install and configure Nagios on Linux

Install Nagios Server on Linux

Nagios is a popular and open-source application that is used for continuous monitoring of systems, networks, services, and applications. It constantly monitors the status of machines and various services. In case any of any issue, it provides early warning so that administrator can take required actions. Nagios performs all checks on local and host machines using the external programs-known as plugins. It also provides you with a web interface that allows viewing the status of hosts and services, history, logs, and generating reports.

In this post, we will be explaining how to install and configure Nagios on Linux and monitor other Linux servers’ health. We will be using the two Debian 10 (Buster) servers; both on the same network. On the monitoring server, we will install the Nagios core, Nagios plugin, and check_nrpe plugin. While on the remote host, we will install the Nagios plugin and NRPE.

Terminologies

Let’s understand some terminologies which help you to understand the configurations:

Host: It is a device that needs to be monitored such as a physical machine, workstation, or network device.

Service: It is the service or resource being monitored on the host such as HTTP, SMTP, POP, DNS, number of logged-in users, memory usage, etc.

Nagios Plugin: These are scripts that check the status of a host or service and verify whether they are working correctly. There are some predefined plugins that can check basic resources such as processor load, disk usage, ping rates, etc. Plugins are usually located in /usr/local/nagios/libexec directory.

NRPE (Nagios Remote Plugin Executor) add-on: NRPE add-on executes the plugins on the remote Linux hosts which then allows Nagios to keep an eye on local resources (disk space, memory usage, etc). NRPE add-on comprises of:

  • NRPE daemon– It runs on remote Linux/Unix hosts which needs to be monitored
  • check_nrpe plugin– It is used by the Nagios monitoring server

 You must have sudo privileges on both the servers. The details of our machines are as follows:

Nagios Monitoring Server:

  • Hostname: nagios-server,
  • IP address: 192.168.72.158

Nagios Host:

  • Hostname: client
  • IP address: 192.168.72.159

Step 1 – Installing Nagios Core on Monitoring Server

On the Nagios monitoring server, follow the below steps to download and install the Nagios core.

1. Installing Prerequisites

First, we will need to install Prerequisites on the Nagios monitoring server. To do so, update the packages list using the below command in Terminal:

$ sudo apt update

Then install the required packages:

$ sudo apt install -y autoconf gcc libc6 make wget unzip apache2 php libapache2-mod-php7.4 libgd-dev openssl libssl-dev

2. Download the Nagios Source

Navigate to the /tmp directory:

$ cd /tmp

Then download the Nagios source code using the wget command. We will download the currently available latest version of Nagios that is 4.4.6. You can visit the Nagios Downloads to look for the current stable release.

$ wget -O nagios.tar.gz https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.4.6.tar.gz

The downloaded file will be saved as nagios.tar.gz in the /tmp directory.

3. Compile Source

Extract the downloaded archive nagios.tar.gz using the below command:

$ tar -xzf nagios.tar.gz

Move into the extracted folder using the cd command:

$ cd nagioscore-nagios-4.4.6/

Run the configure script and specifies the path to the Apache server configuration directory:

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

Once the configure script is executed, you will see the below summary at the end of the output:

Your configure script may fail in case of any pre-requisite missing. In that case, install the missing pre-requisites and again run the script.

Now compile Nagios using the below command:

$ make all

4. Create User and Group

Now create user and group for Nagios.

$ sudo make install-groups-users 

The above command will create a system user and group Nagios.

We will also need to add user “www-data” to the nagios group.

$ sudo usermod -a -G nagios www-data

5. Installing Binaries

Then to install binaries, use the below command:

$ sudo make install

6. Install Service / Daemon

To install service files, use the below command:

$ sudo make install-daemoninit

7. Install Command Mode

To install and configures the external command file, use the below command:

$ sudo make install-commandmode

8. Install Configuration Files

Then to install the sample Nagios configuration, use the below command:

$ sudo make install-config

9. Install Apache Config Files

This installs the Apache configuration files, use the below command:

$ sudo make install-webconf

Now enable the Apache modules using the below commands:

$ sudo a2enmod rewrite
sudo a2enmod cgi

Create Nagiosadmin User Account for the Web Interface:

Now we will create an Apache user account that can access the Nagios web interface.

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

Now set the password for the nagiosadmin user account. This password will be used to access the Nagios’ web interface.

Then restart Apache for the configuration changes to take effect:

$ sudo systemctl restart apache2

Now we have successfully installed the Nagios core engine.

Step 2 – Installing the Nagios Plugin on Monitoring Server

For the Nagios core to work properly, you will need to install the Nagios plugin.

Download Nagios Plugin

On the Nagios server, Navigate to the /tmp directory:

$ cd /tmp

Then download the Nagios plugin source code using the wget command. We will download the currently available latest version of the Nagios plugin that is 2.3.3. You can visit the Github page to find the recent stable release.

$ wget -O Nagios-plugins.tar.gz https://github.com/nagios-plugins/nagios-plugins/releases/download/release-2.3.3/nagios-plugins-2.3.3.tar.gz

The downloaded file will be saved as Nagios-plugins.tar.gz in the /tmp directory.

Compile

Extract the downloaded archive Nagios-plugins.tar.gz using the below command:

$ tar zxf nagios-plugins.tar.gz

Navigate to the extracted folder using the cd command:

$ cd nagios-plugins-2.3.3/

Run the configure script:

$ sudo ./configure

Now compile the Nagios plugin using the below command:

$ sudo make 
$ sudo make install

Step 3 – Installing the check_nrpe Plugin on Monitoring Server

Now we will install check_nrpe plugin on our Nagios server.

Download Plugin Source

On the Nagios server, Navigate to the /tmp directory:

$ cd /tmp

Then download the check_nrpe source code using the wget command. We will download the recent available version of check_nrpe that is 4.0.3. You can visit the Github page to find the recent stable release.

$ wget -O Nagios-nrpe.tar.gz https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-4.0.3/nrpe-4.0.3.tar.gz

The downloaded file will be saved as Nagios-nrpe.tar.gz in the /tmp directory.

Compiling the Source

Extract the downloaded archive nagios.tar.gz using the below command:

$ tar -xzf Nagios-nrpe.tar.gz

Navigate to the extracted folder using the cd command:

$ cd nrpe-4.0.3/

Run the configure script:

$ sudo ./configure

Now compile the check_nrpe using the below command:

$ sudo make check_nrpe

$ sudo make install-plugin

Step 4 – Configuring Nagios

Now, we will configure the Nagios main configuration file. Edi the Nagios configuration file nagios.cfg using the below command:

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

Now find the below line in the file and uncomment it by removing the # character from the beginning of the line.

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

The above line specifies the directory which contains a configuration file for all the hosts that the Nagios server will monitor. Now save and close the configuration file.

Now we will create the directory in the path defined above:

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

Configure E-mail Contacts

Now we will configure the contacts that should be notified in case of any event. Edit the Nagios contacts configuration using the below command:

In order to receive alerts from the Nagios monitoring server, you will need to configure the contacts. Open the contacts configuration file, and specify the email address you would like to receive the emails from Nagios:

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

In the email directive, replace [email protected]localhost with your own email address and then save and close the file.

Configure check_nrpe

Now in order to use check_nrpe plugin, we will add a new command in the command configuration file. Edit the command configuration file commands.cfg using the below command:

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

Append the following lines to the end of the commands.cfg file and then save and close it:

define command{

command_name check_nrpe

command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$

}

Now start Nagios service:

$ sudo systemctl start Nagios.service

Step 5 – Accessing the Nagios Web Interface

Now access the Nagios web interface by visiting the below address in any web browser:

http://nagios-server-ip/nagios

In our scenario, it would be:

http://192.168.72.158/nagios

In the Authentication Required dialog, type nagiosadmin as the username and password you have set earlier.

Once logged in to the web nterface, you will see the default Nagios web page. Go to the Hosts tab from the left sidebar to view the monitored hosts.

At present, you will see only one host “localhost” which is the Nagios server itself. In order to monitor other hosts, you will need to install the NRPE daemon on them.

Step 6 – Remote Host Setup

Now you will have to install the Nagios plugin and NRPE on the hosts that need to be monitored on the Nagios server.

1. Install Nagios Plugin on Remote Host

In the remote host (client) that needs to be monitored, follow the below steps to install Nagios plugin.

1. First, update the apt:

$ sudo apt update

2. Then issue the below command to install the required packages:

$ sudo apt install autoconf gcc libmcrypt-dev make libssl-dev dc build-essential gettext

3. Now, Navigate to /tmp and download the Nagios plugin.

$ cd /tmp
$ wget -O Nagios-plugins.tar.gz https://github.com/nagios-plugins/nagios-plugins/releases/download/release-2.3.3/nagios-plugins-2.3.3.tar.gz

4. Extract the downloaded archive:

$ tar -zxf nagios-plugins.tar.gz

5. Move into extracted folder and run the configure script:

$ cd nagios-plugins-2.3.3// 
$ sudo ./configure

6. Compile the plugin:

$ sudo make 
$ sudo make install

2. Install NRPE Daemon on Remote Host

Follow the below steps to install NRPE on the remote host.

1. First, update the apt:

$ sudo apt update

2. Issue the below command to install the pre-requisites:

$ sudo apt install -y autoconf automake gcc libc6 libmcrypt-dev make libssl-dev

3. Now Navigate to /tmp and download NRPE.

$ cd /tmp 
$ wget -O nrpe.tar.gz https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-4.0.3/nrpe-4.0.3.tar.gz

4. Extract the downloaded archive:

$ tar xzf nrpe.tar.gz

5.  Move into the extracted folder and run the configure script:

$ sudo ./configure --enable-command-args

6. Compile the NRPE with the following commands:

$ sudo make all
$ make install-groups-users

$ sudo make install

$ sudo make install-config

$ sudo make install-init

3. Configure Firewall

Now if a firewall is running on your remote host, you will need to open port 5666 (used by NRPE) through it.

$ sudo ufw allow 5666/tcp

4. Update NRPE Configuration

Now open the NRPE configuration file in the remote host:

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

You will need to find and modify the below entries:

Add client IP address:

server_address=192.168.72.159

Add Nagios server IP address:

allowed_hosts=127.0.0.1,192.168.72.158

Also, replace /dev/vda1 with your root file system:

command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1

Then start the NRPE service:

$ sudo systemctl start nrpe.service

Now check if the NRPE service is properly running:

$ sudo systemctl status nrpe.service

The active (running) in the below output shows that the service is successfully running without any issues. You can also see the NRPE is allowing connection from 192.168.72.158 which is the IP address of the Nagios server.

Now to check if the check_nrpe on the Nagios server can communicate with the NRPE on the remote host, issue this command on the Nagios server:

$ /usr/local/nagios/libexec/check_nrpe -H remote_host_ip

You should receive the following similar output (NRPE version number).

How to install Nagios on Linux

Step 7 – Add Host Configuration to the Nagios Server

Now in the Nagios monitoring server, you will need to create separate configuration files in the /usr/local/nagios/etc/servers/ for all remote hosts that you need to monitor.

For our host machine, we will create a new configuration file client.cfg:

$ sudo nano /usr/local/nagios/etc/servers/client.cfg

Replace client with the name of your host:

Now add host definition to the new configuration file replacing the value of host_name with the remote hostname, alias with a short description, and the address with IP address of the remote host.

This configuration will check if the host machine is up or down.

define host {

use linux-server

host_name client

alias client machine

address 192.168.72.159

max_check_attempts 5

}

Now add service definition to the configuration file for monitoring the remote host. Replace the value of host_name with the remote hostname.

Note: These service definitions will use the commands preconfigured in the /usr/local/nagios/etc/nrpe.cfg file on the remote host.

Add the following service block to monitor the CPU load on the remote host

define service {

use generic-service

host_name client

service_description CPU Load

check_command check_nrpe!check_load

}


c on the remote host:
define service{

use generic-service

host_name client

service_description Total Processes

check_command check_nrpe!check_total_procs

}

Add the following service block to monitor disk usage on the remote host:

define service { 
use generic-service     

host_name                       client
      service_description             Check Disk
      check_command                   check_nrpe!check_disk

}

Add the following service block to monitor the number of users currently logged in on the remote host:

define service { 
use generic-service     

host_name                       client
      service_description             Current Users
      check_command                   check_nrpe!check_users

}

Now save and close the configuration file.

How to install Nagios on Linux

Restart the Nagios service to apply the configuration changes:

$ sudo systemctl restart Nagios

Now access the web interface using the http://nagios-server-ip/nagios. Wait for a while and your remote host will be added to the Nagios web interface.

Go to Hosts from the left navigation bar and you will see your remote host listed there.

How to install Nagios on Linux

Then go to the Services tab, and you will see the status of all the services for a host you have configured for monitoring.

How to install Nagios on Linux

In this post, we have explained how to install and configure the Nagios monitoring solution on Linux. We have also explained how to setup remote hosts for monitoring. Now you can easily monitor the health of Linux hosts and the services running on them.

Click here if you are interested in monitoring your servers with Zabbix.

Similar Posts