Published February 16, 2018 by

Install and Configure Nagios on Ubuntu/Debian



Nagios is a free Monitoring Tool, It’s Provides you Monitoring environment to always keep an eye on your all Networks/systems from anywhere.

With Nagios, you can monitor your remote hosts and their services remotely in a single window. It shows warnings and indicates if something goes wrong on your servers which eventually helps us to detect some problems before they occur. It helps us to reduce downtime and business losses.

Few things that you need to keep in mind about Nagios
  • Nagios installed in /usr/local/nagios/.
  • Nagios Plugins are installed under /usr/local/nagios/libexec/.
  • Nagios will be configured to monitor few services of your local machine like Disk Usage, CPU Load, Current Users, Total Processes, etc...
  • Nagios main Page located under /usr/local/nagios/share/index.php
  • Nagios Templates are stored at /usr/local/nagios/etc/objects/templates.cfg
  • Nagios Configuration file under /usr/local/nagios/etc/objects/contacts.cfg
  • All Host files are added in this location /usr/local/nagios/etc/objects

Install Required Dependencies

We need to install Apache, PHP and some libraries like wget, unzip, gcc, glibc, glibc-common and GD libraries and its development libraries before installing Nagios 4.3.4 with the source.

# sudo apt update
# sudo apt install wget build-essential unzip openssl libssl-dev
# sudo apt install apache2 php7.0 apache2-mod-php7.0 php7.0-gd libgd-dev 

Create Nagios User and Group

Create a new nagios user and nagcmd group account and add both the nagios user and the apache user to the nagcmd group.

# adduser nagios
# groupadd nagcmd
# usermod -a -G nagcmd nagios
# usermod -a -G nagcmd www-data

Download Nagios Core and Nagios Plugin


Create a directory for your Nagios installation.

# mkdir /root/nagios
# cd /root/nagios

Download latest Nagios Core 4.3.4 and Nagios plugins 2.2.1 packages.

# wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.4.tar.gz
# wget http://www.nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz

Extract Nagios Core and its Plugins

Extract downloaded package.

# tar xzf nagios-4.3.4.tar.gz
# tar xzf nagios-plugins-2.2.1.tar.gz

Configure Nagios Core

Now we will configure Nagios Core.

# cd nagios-4.3.4/
# ./configure --with-command-group=nagcmd

After configuring, we need to compile and install all the binaries with make all and make install command, it will install all the needed libraries on your machine.

# make all
# make install

Install the init scripts, command-mode and sample nagios file for Nagios.

# make install-init
# make install-commandmode
# make install-config

Copy event handlers scripts under libexec directory. These binaries provide multiple events triggers for your Nagios web interface.

# cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/
# chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers

Create nagios apache2 configuration file.

# sudo vim /etc/apache2/conf-available/nagios.conf

Add the following lines in nagios.conf

ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
 <Directory "/usr/local/nagios/sbin">
   Options ExecCGI
   AllowOverride None
   Order allow,deny
   Allow from all
   AuthName "Restricted Area"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   Require valid-user
</Directory>
 Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share">
   Options None
   AllowOverride None
   Order allow,deny
   Allow from all
   AuthName "Restricted Area"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   Require valid-user
</Directory>

using systemd for managing process can enable nagios with systemd using following commands.

# sudo cp /etc/init.d/skeleton /etc/init.d/nagios
# sudo vim /etc/init.d/nagios

Add the following lines in nagios

DESC="Nagios"
NAME=nagios
DAEMON=/usr/local/nagios/bin/$NAME
DAEMON_ARGS="-d /usr/local/nagios/etc/nagios.cfg"
PIDFILE=/usr/local/nagios/var/$NAME.lock

Install and Configure Web Interface for Nagios

The below command will Configure Web interface for Nagios and a web admin user will be created "nagiosadmin" also set password for "nagiosadmin"

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

Restart Apache

# sudo a2enconf nagios
# sudo a2enmod cgi rewrite
# sudo service apache2 restart

Compile and Install Nagios Plugin

We have downloaded Nagios plugins in /root/nagios, Go there and configure and install it.

# cd /root/nagios/nagios-plugins-2.2.1/
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl
# make
# make install

Verify Nagios Configuration Files

We are done with the Nagios configuration.Now, It’s time to verify it

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

Add Nagios Services to System Startup

# systemctl enable nagios
# systemctl enable apache2

Restart Nagios

# systemctl start nagios.service

Login to the Nagios Web Interface

Open a browser and hit "http://Server-IP-address/nagios" and Provide the username "nagiosadmin" and password.