There is a new version of this tutorial available for Debian 12 (Bookworm).

How to Install Nagios 4.3.x Monitoring Tool on Debian 9

This tutorial exists for these OS versions

On this page

  1. Requirements
  2. Initial Configuration
  3. Install Nagios Core
  4. Install Nagios Plugins

This tutorial will show you how to install and configure the latest version of Nagios Core, currently 4.3.4, from sources in Debian 9.1, codename Stretch.

Nagios, also known as Nagios Core, is a free Open Source network monitoring tool actively maintained by its developers, designed to monitor an entire network infrastructure. Nagios can monitor almost every network device, as long as the device has a network connection configured, such as routers, switches, servers, desktops, firewalls appliances, IoT devices. In addition to monitoring devices in order to determine their network state, Nagios can also monitor network services, applications or other specific tasks related to operating systems and can be configured to send alerts via mail or SMS in order to inform network administrators for possible devices, outages or service failures.

Requirements

  • Debian 9.1 installed on a bare-metal machine or on a virtual private server. Preferably, the installation must be performed with minimal software requirements.
  • The network interface card configured with a static IP address.
  • Access to root account or a user with root account privileges via sudo.
  • A domain name, private or public, with the proper DNS A records configured. In case you don’t have a DNS server configured at your premises, you can access Nagios via server IP address.

Initial Configuration

Before we start to install Nagios from sources, make sure the system meets all the software requirements for compiling and installing Nagios. In the first step, update your system repositories and software packages by issuing the below command.

apt update
apt upgrade

Install Debian updates

In the next step, fire-up a new command in order to install some necessary utilities that will be used to further manage your system from the command line.

apt install wget unzip zip bash-completion

 Next, set up the name for your system by executing the following command:

hostnamectl set-hostname nagios.server.lan

Verify machine hostname and hosts file by issuing the below commands.

hostnamectl
cat /etc/hostname
cat /etc/hosts

Finally, reboot the system in order to apply the new hostname.

init 6

Nagios is a web-based monitoring application with some parts written in PHP server-side programming language and other CGI programs. In order to run Nagios PHP file scripts, a web server, such as Apache HTTP server, and a PHP processing gateway must be installed and operational in the system.  In order to install Apache web server and the PHP interpreter alongside with all required PHP modules needed by Nagios 4 to run properly, issue the following command in your server console.

apt install apache2 libapache2-mod-php7.0 php7.0

After Apache and PHP have been installed, test if the web server is up and running and listening for network connections on port 80 by issuing the following command with root privileges.

netstat –tlpn

In case netstat network utility is not installed by default in your Debian 9 system, execute the below command to install it.

apt install net-tools

Check apache installation with netstat command

By inspecting the netstat command output you can see that apache web server is listening for incoming network connections on port 80.

In case you have a firewall enabled on your system, such as UFW firewall application, you should add a new rule to allow HTTP traffic to pass through the firewall by issuing the following command.

ufw allow WWW

or

ufw allow 80/tcp

In case you want to use iptables raw rules to allow port 80 inbound traffic on the firewall so that visitors can browse the Nagios Core web interface, add the following rule.

apt-get install -y iptables-persistent
iptables -I INPUT -p tcp --destination-port 80 -j ACCEPT
systemctl iptables-persistent save
systemctl iptables-persistent reload

Next, enable and apply the following Apache modules required by Nagios web application to run properly, by issuing the below command.

a2enmod rewrite headers cgi
systemctl restart apache2

Enable apache modules

Finally, test if Apache web server default web page can be displayed in your client's browser by visiting your Debian machine IP address or domain name via HTTP protocol, as shown in the below image. If you don’t know your machine IP address, execute ifconfig or ip a commands.

http://192.168.1.14

Apache default web page

In the next step, we need to make some further changes to PHP default configuration file in order to assure that the PHP timezone setting is correctly configured and matches your system physical location.  Open /etc/php/7.0/apache2/php.ini file for editing and assure that the following lines are setup as follows.

date.timezone = Europe/London

Replace the timezone variable accordingly to your physical time by consulting the list of timezones provided by PHP docs at the following link http://php.net/manual/en/timezones.php.

Restart apache daemon to apply the changes.

systemctl restart apache2

After you’ve made the required changes, create a php info file and restart apache daemon to apply changes by issuing the following commands.

echo '<?php phpinfo(); ?>'| tee /var/www/html/info.php
systemctl restart apache2

Check if the PHP time zone has been correctly configured by visiting the phpinfo script file from a browser at the following URL, as illustrated in the below image. Scroll down to the date setting to check the php timezone setting.

http://192.168.1.14/info.php

 Check PHP timezone setting

Install Nagios Core

Before downloading and compiling Nagios Core from sources, first make sure you install the following pre-required packages in your system, by issuing the below command.

apt install autoconf gcc libc6 make apache2-utils libgd-dev

After all necessary dependencies and packages for compiling Nagios from sources are installed on your Debian system, visit Nagios official website at https://www.nagios.org/downloads/nagios-core/ and download the latest version of Nagios Core stable source archive by issuing the wget utility, as shown in following command excerpt.

wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.4.tar.gz

After Nagios source tarball has been downloaded, extract the tar archive and enter the extracted nagios directory, with the following commands. Run ls command inside nagios extracted directory in order to list the source files.

tar xzf nagios-4.3.4.tar.gz
cd nagios-4.3.4/
ls

Download Nagios

While you are inside the Nagios extracted sources directory, start Nagios compilation process from sources by issuing the below commands. First, configure Nagios to be compiled with Apache web server http configuration path pointing to sites-enabled directory.

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

Next, compile Nagios by issuing the following command, as illustrated in the below images.

make all

Configure and make Nagios

Build Nagios from source

Next, create the nagios system user and group and add nagios account to the Apache runtime user in order for the nagios user to have the required permissions to access web resources.

useradd nagios
usermod -a -G nagios www-data

Now, start to install Nagios binary files, CGI scripts and HTML files by issuing the following command. The final output of the make install command should display the binary locations, as shown in the below image.

make install

Installcompiled Nagios files

Next, install Nagios daemon systemd init files and enable nagios service system-wide by issuing the following commands.

make install-init
systemctl enable nagios.service

Install Nagios init files

Also, install and configure Nagios external command file by running the below command.

make install-commandmode

Install Nagios commandmode

Next, run the following command in order to install Nagios sample configuration files which are required by Nagios daemon to start and operate properly.

make install-config

Install Nagios sample configuration files

Finally, install the Apache web server configuration file for Nagios, which will be located in /etc/apacahe2/sites-enabled/ directory, by executing the below command.

make install-webconf

Install Nagios apache web config

Create the nagiosadmin user account with the corresponding password required by Apache web server to be able to perform log in to Nagios web tool by issuing the following command.

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

Create a htpasswd file for Nagios

In order to access Nagios web panel, first, restart Apache HTTP server and start Nagios service by issuing the following commands.

systemctl restart apache2
systemctl start nagios

Then, log in to Nagios Web Interface by opening a browser and visiting your server’s IP address or domain name or FQDN and append /nagios URL path via HTTP protocol, as illustrated in the below screenshots. Use the nagiosadmin user with the password configured earlier for this user in order to login to Nagios web interface.

Login to Nagios

Nagios Dashboard

Install Nagios Plugins

Nagios engine is now installed on the system. However, the Nagios software is not operational yet, because the plugins that are used for checking hosts and services are not installed. This behavior can be observed by navigating to Hosts menu where you will notice the plugin errors, as shown in the below screenshot.

Nagios no plugins installed yet

In order to compile and install some basic Nagios Plugins from source, first make sure you install the following libraries and dependencies by issuing the following command.

apt install libmcrypt-dev make libssl-dev bc gawk dc build-essential snmp libnet-snmp-perl gettext libldap2-dev smbclient fping default-libmysqlclient-dev

 After all dependencies packages had been installed in your system, go to Nagios Plugins repository page by visiting the following address https://github.com/nagios-plugins/nagios-plugins/releases and grab the latest source code archive with the help of wget command line utility, as shown in the below command example.

wget https://github.com/nagios-plugins/nagios-plugins/archive/release-2.2.1.tar.gz

 Then, extract the compressed Nagios Plugins source code tarball and enter the extracted nagios-plugins directory by executing the following commands.

tar xfz release-2.2.1.tar.gz
cd nagios-plugins-release-2.2.1/

Download Nagios Plugins

In order to compile and install Nagios Plugins from sources, execute the following scripts and commands with root privileges, as shown in the below excerpt.

./tools/setup
./configure
make
make install

After all the plugins are installed, you can locate them at /usr/local/nagios/libexec/ system path. Issue the ls command in order to examine all content of the /usr/local/nagios/libexec/ directory and see what plugins are available for Nagios in your system.

ls /usr/local/nagios/libexec/

Nagios Libexec directory

In order for Nagios engine to pick-up and use the installed plugins, you need to restart and check the nagios service status with the following commands.

systemctl restart nagios.service
systemctl status nagios.service

To test if the Nagios Plugins are working as expected, log in back to Nagios Web Interface by opening a browser and visiting your server’s IP address, FQDN or domain name and navigate to Hosts or Services menu, as illustrated in the below screenshot. The monitored hosts and services should be checked now by the plugins and should display the correct output, depending on the plugin command exit status. The displayed colors are: green for OK, yellow for Warning and red for Critical.

Nagios monitored services

In order to use the HTTPS protocol to access Nagios engine web interface via a secured connection, issue the following command to enable Apache web server SSL module and SSL site configuration file.

a2enmod ssl
a2ensite default-ssl.conf

Next, in order to force the visitors to be automatically redirected to HTTPS protocol each time they visit Nagios web interface from their browsers, open /etc/apache2/sites-enabled/000-default.conf file for editing and add the following rewrite rules after DocumentRoot statement as shown in the below example.

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}/$1

Enable https in apache

Finally, restart Apache daemon to apply all rules configured so far and visit Nagios web interface via HTTP protocol.

systemctl restart apache2.service

In order to check if Apache is listening on the HTTPS port, execute netstat command and look for 443 port binding, as shown in the below image.

netstat -tlpn

Check apache with https

In case the UFW firewall application blocks incoming network connections to HTTPS port, you should add a new rule to allow HTTPS traffic to pass through the firewall by issuing the following command.

ufw allow 'WWW Full'

or

ufw allow 443/tcp

In case you run iptables firewall to protect your Debian system, add the following rule to allow port 443 inbound traffic in the firewall so that visitors can browse the Nagios Core web interface.

iptables -I INPUT -p tcp --destination-port 443 -j ACCEPT
systemctl iptables-persistent save
systemctl iptables-persistent reload

Now, you should be automatically redirected to the Nagios web panel via HTTPS protocol. Because you’re using the automatically Self-Signed certificates pairs issued by Apache at installation, an error warning should be displayed in the browser. Accept the browser warning and login to Nagios with the credentials configured earlier, as shown in the below screenshot.

Nagios with HTTPS

To add a new host to be monitored by Nagios, open /usr/local/nagios/etc/objects/localhost.cfg configuration file for editing and add the new device after the localhost definition, as shown in the below example.

define host{
        use                     linux-server
        host_name               router
        alias                   router
        address                 192.168.1.1
        }

Edit nagios config file

Replace the host_name, alias and IP address variables accordingly and restart Nagios daemon to load the new host definition and apply changes.

systemctl restart nagios

What a few minutes and navigate to Hosts menu in Nagios web interface in order to check the device status, as illustrated in the below image.

Nagios secured with SSL / TLS

That’s all! You have successfully installed and configured Nagios engine from sources in Debian 9. You can start adding your organization network devices, servers or critical services to be monitored by Nagios. For other custom configurations regarding Nagios Core and Nagios Plugins, visit the documentation pages at the following link https://support.nagios.com/kb/category.php?id=12

Share this page:

18 Comment(s)