We use cookies to improve our services and provide a better experience. By continuing to use this site, you agree with our Cookies Policy.

Tutorials
More than 400 step-by-step articles to guide you through online project development.
HomeTutorialsHosting ServicesCloud Servers

How to Install and Configure Nagios Monitoring Software

KVM VPS
Germany Cloud Servers
Linux Cloud Server
OpenVZ VPS
cPanel KVM
USA VPS
Linux KVM Hosting
Ubuntu VPS
Windows Cloud Server
Brazil VPS

Nagios is known as the top, open source solution to monitor your servers. It provides complete monitoring of Linux operating systems and distributions – including operating system metrics, service state, process state, file system usage and more. Server monitoring is easy with Nagios software because of flexibility to monitor your servers with both: agent based and agent-less monitoring.

Nagios monitoring system is useful for ensuring that your critical services are up and running. Using a monitoring service, such as Nagios, is an essential tool for all servers.

This tutorial will explain you how to install and set up Nagios monitoring system and web interface management to your Linux based server. We will use Ubuntu 14.04 x64 distribution as an example.

NOTE: You must have super user privileges in order to perform the steps explained below.

Follow These Steps to Install and Configure Nagios Monitoring Software

This part of tutorial will explain you how to install Nagios Core on your server. This procedure must be done only once.

Requirements

Installation of Nagios core requires the local server to have at least 2 GB of SWAP memory, so you have to enable SWAP memory first.

On the VPS server you will have to upgrade your RAM memory at least to 4 GB if you want to have 2 GB of SWAP.

If you are planning to run Nagios on the dedicated server, so you must run following commands:

sudo dd if=/dev/zero of=/swap bs=1024 count=2097152
sudo mkswap /swap && sudo chown root. /swap && sudo chmod 0600 /swap && sudo swapon /swap
sudo sh -c “echo /swap swap swap defaults 0 0 >> /etc/fstab”
sudo sh -c “echo vm.swappiness = 0 >> /etc/sysctl.conf && sysctl -p”

Installing Required Packages

Nagios Core installation requires your operating system to be up to date and also have some development libraries, which allows you to compile the Core from the source, installed on your server.

First, update your operating system’s repository:

apt-get update

If you are installing Nagios on FRESH Ubuntu 14.04 installation, it is recommended to upgrade your existing operating system by running the following command:

apt-get upgrade

NOTE: Do not upgrade your existing installation if you are already running any software on this server, as it may interrupt work of existing programs.

and then it is time to install required packages:

sudo apt-get install apache2 php5 build-essential libgd2-xpm-dev openssl libssl-dev xinetd apache2-utils unzip

NOTE: By following this tutorial you will also install apache2 and php5 packages. Do not install them if you are using another package, such as Nginx, for Web Server.

Creating user and group

You have to create a new user and group for Nagios monitoring system. It will run all the processes under this user. Create a user named “nagios” and group named “naggrp”:

sudo useradd nagios
sudo groupadd naggrp
sudo usermod -a -G naggrp nagios

Install Nagios Core

Your server is now prepared for Nagios Core installation. First you have to download the source code of the latest stable Nagios Core version. It can be downloaded from Official Nagios website. You will be asked to provide some personal details of yours, however there is a small button “skip to download”, click on it if you do not want to receive the notifications about latest releases of Nagios. Download the latest version:

cd ~
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.1.1.tar.gz

Extract the newly downloaded archive:

tar xvf nagios-*.tar.gz

Go to the extracted folder:

cd nagios-*

Once you are in the Nagios installation folder, you have to configure it before installing:

./configure –with-nagios-group=nagios –with-command-group=naggrp

Compile Nagios as follows:

make all

Run the following commands in order to install Nagios, init scripts and sample configuration files:

sudo make install
sudo make install-commandmode
sudo make install-init
sudo make install-config
sudo /usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-available/nagios.conf

In order to be able to issue external command via the web interface, you have to add web server’s user to Nagios group:

usermod -G naggrp www-data

Installation of Nagios Plugins

The installation of Nagios Core is completed, now you must download the latest stable version of Nagios plugins and install them. The latest plugins can be found in the official site of Nagios. Download the latest version:

cd ~
wget http://nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz

Extract downloaded archive:

tar xvf nagios-plugins-*.tar.gz

Navigate into newly extracted directory and configure the Nagios Plugins before the installation:

cd nagios-plugins-*
./configure –with-nagios-user=nagios –with-nagios-group=nagios –with-openssl

Compile Nagios Plugins as follows:

make

And, finally, install Nagios Plugins:

sudo make install

Installing Nagios RPE

Nagios RPE or Remote Plugins Executor allows users to execute Nagios Plugins on other Linux machines. It allows users to monitor remote machine metrics – CPU load, Disk usage and etc. The latest release of NRPE can be downloaded from official Nagios website.
In this tutorial, 2.15 version is used:

cd ~
wget http://downloads.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz

Extract downloaded archive:

tar xvf nrpe-*.tar.gz

Navigate to extracted directory:

cd nrpe-*

Configure NRPE as follows:

./configure –enable-command-args –with-nagios-user=nagios –with-nagios-group=nagios –with-ssl=/usr/bin/openssl –with-ssl-lib=/usr/lib/x86_64-linux-gnu

Build and install NRPE using the following commands:

Make all
Sudo make install
Sudo make install-xinetd
Sudo make install-daemon-config

Edit the startup script of xinetd after the installation is finished using your favorite text editor, for example nano:

nano /etc/xinetd.d/nrpe

And edit only the only_from line by adding the IP of your Nagios Core server in order to allow incoming connections only from Nagios Core Server:

Only from 127.0.0.1 Nagios_Core_Server_IP

Save and exit and finally restart xinetd daemon:

service xinetd restart

Nagios installation is now completed. The next step is to configure it.

In this part of tutorial, you will learn how to perform basic configuration of Nagios. This part must be performed only once, on the Nagios Core server.

First, you have to organize your Nagios Core configuration. Open Nagios configuration file with your favorite text editor:

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

Search for the following line and uncomment it (delete # sign):

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

Save and exit.

Next – create folder, where Nagios Core will store information about server that are monitored:

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

Add your e-mail as contact e-mail address for notifications from Nagios Core server:

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

And change nagios@localhost to your e-mail address as displayed below:

email nagios@localhost ; << ***** CHANGE THIS TO YOUR EMAIL ADDRESS ******

Save the file and exit.

Define check_nrpe command

Add the new function, called check_nrpe to your Nagios configuration, which allows you to use check_nrpe command in your Nagios service definitions. Open the file where all commands are defined:

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

And add the following lines in the end of the file:

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

Save the file and exit.

Configure Apache Web Server

Nagios allows you to access the information about the servers you are monitoring via web interface, so in this part you will learn how to configure Apache Web Server properly.

First, you have to enable Rewrite and CGI modules:

sudo a2enmod rewrite
sudo a2enmod cgi

Next, create user, named, for example “nagiosadmin”, which will be used to access Nagios Web Interface:

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

You will be asked to create the password. Enter it, and write down somewhere – you will need it to access Nagios Web Interface.

Enable Nagios Web interface by creating a symlink from Nagios configuration file to sites-enabled directory as follows:

sudo ln -s /etc/apache2/sites-available/nagios.conf /etc/apache2/sites-enabled/

Everything is set now. Finally, restart Nagios and Apache services:

sudo service nagios start
sudo service apache2 restart

Nagios will not start on boot by default, however you can enable on boot start-up by running the following command:

sudo ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios

Restricting Web Access for particular IP address

This part of tutorial is optional, but recommended. In this part you will learn how to restrict web access to Nagios Web Interface for particular IP address. It can be done by editing Apache’s configuration file:

nano /etc/apache2/sites-available/nagios.conf

And comment the following two lines (add # sign in front of them):

Order allow,deny
Allow from all

And uncomment the following lines (delete # sing in the front of them) and add the specific IP or range of IP addresses you want to allow to access Nagios Web Interface:

# Order deny,allow
# Deny from all
# Allow from 127.0.0.1

As these lines will appear twice in the configuration file, so you will need to perform these steps once more.
Save the file, exit and restart Nagios and Apache services as displayed below:

sudo service nagios restart
sudo service apache2 restart

Everything is set now. Try to access your Nagios Core Web Interface.

Accessing Nagios Web Interface

In order to access Nagios Web Interface you have to open your favorite internet browser and type the following address in the navigation bar:

http://your_nagios_server_IP/nagios/

Nagios admin area cannot be accessed without identification. System will ask you to enter the username and password. You must enter the login credentials you have created earlier (nagiosadmin in my case). You will see Nagios Admin area after the successful login. Now click “Hosts” button which can be found in the left side. You will see that the Nagios Core server now is monitoring only localhost. That means that it monitors only itself.

Nagios Monitoring System Web Interface

Click on the “Statistic” icon in order to see detailed information:

Nagios Monitoring System Web Interface

Do not be afraid to try exploring the admin area by yourself.

In this part of tutorial you will learn how to add new services to your monitoring service. You must repeat these steps on all servers that you want to monitor. The server we use for example is also running Ubuntu 14.04 operating system.

First, update the repository of the server:

sudo apt-get update

Install Nagios Plugins and Nagios Remote Plugins Executor (NRPE):

sudo apt-get install nagios-plugins n9agios-nrpe-server

Configure Nagios NRPE server

Now you have to edit Nagios Remote Plugin Executor (NRPE) configuration file. Open configuration file with your favorite text editor:

sudo nano /etc/nagios/nrpe.cfg

Nagios NRPE configuration file is very big, however you are interested just in 3 lines. You have to specify the following values:

server_address – Enter the current IP address of your current Server (client).
allowed_hosts – Enter localhost IP address and the IP of your Nagios Core server.
command[/dev/hda1] – Change the values to your root filesystem’s name*.

as displayed in example below:

server_address=client_private_IP
allowed_hosts=127.0.0.1, nagios_core_server_private_IP
command[/dev/hda1]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/ploop21979p1

NOTE: Filesystem’s name can be retrieved by running the command: df –h /:

Filesystem Size Used Avail Use% Mounted on
/dev/ploop21979p1 20G 730M 18G 4% /

Save the file, exit the text editor and restart Nagios NRPE server:

sudo service nagios-nrpe-server restart

Adding Host To Nagios Core Configuration

Almost everything is set at the moment. Now you just have to inform your Nagios Core server that you have the server which is waiting to be monitored.

Open the directory you previously defined as the place where you put the information about monitored servers:

cd /usr/local/nagios/etc/servers

and create the configuration file for the monitored server:

touch /usr/local/nagios/etc/servers/monitored_server1.cfg

open this file:

nano /usr/local/nagios/etc/servers/monitored_server1.cfg

and add the following lines:

define host {
use linux-server
host_name Example_Host1Plus
alias My Monitored Server #1
address IP_ADDRESS
max_check_attempts 5
check_period 24×7
notification_interval 30
notification_period 24×7
}The most important parts in this text are:
host_name Example_Host1Plus
alias My Monitored Server #1
address IP_ADDRESS

You must change these values to your specific values of your Nagios server. Save the file and restart Nagios service:

sudo service Nagios restart

Check Hosts section in Nagios Web Interface, the new host is already monitored:

Nagios Web Interface

The steps you provided so far will just check if the host is up or not. You can add any other services or processes to monitor on your Nagios Server by editing the same file. Let’s start to monitor how server is responding to ping:

nano /usr/local/nagios/etc/servers/monitored_server1.cfg

and add the following line:

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

Add the following lines to monitor SSH service:

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

NOTE: Do not forget to change the host_name value to your own.

Save the file and restart Nagios Core server:

sudo service nagios restart

Check monitored services on your Host. As you can see SSH check has been included in Nagios Monitoring system for this host.

Nagios Web Interface

You can add even more services or processes to check in your Nagios configuration. The list of available commands can be found in /usr/local/nagios/etc/objects/commands.cfg file. Open the file:

less /usr/local/nagios/etc/objects/commands.cfg

and look for the sample commands section:

################################################################################
#
# SAMPLE SERVICE CHECK COMMANDS
#
# These are some example service check commands. They may or may not work on
# your system, as they must be modified for your plugins. See the HTML
# documentation on the plugins for examples of how to configure command definitions.
#
# NOTE: The following ‘check_local_…’ functions are designed to monitor
# various metrics on the host that Nagios is running on (i.e. this one).
################################################################################# ‘check_local_disk’ command definition
define command{
command_name check_local_disk
command_line $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
}

In this file there is a full list of basic commands, however If you are an experienced user, you can create your own, modified commands.

After reading this tutorial you should be able to set up, configure your own Nagios Core server and add hosts for monitoring to the configuration. This will help you to ensure that all your critical services are up and running and it successfully increases the stability of your servers. Nagios also allows users to set up notifications, which can be sent via e-mail or other channels in order to notify you about critical services being down or inaccessible. You can visit official website of Nagios in order to learn more about this useful and powerful monitoring tool.

USA VPS
Linux Cloud Server
Windows Cloud Server
Brazil VPS
Ubuntu VPS
cPanel KVM
KVM VPS
OpenVZ VPS
Germany Cloud Servers
Linux KVM Hosting
Rate this Tutorial:
No Comments Leave a Comment
Leave a Comment

Other (3)

Popular Keywords