22.4 C
Texas
Muhammad Nabeel
Network and System Administrator

How to Install Nagios Server and Monitor your Hosts on CentOS 7

Nagios is also known as Nagios Core. It is a free and open source application that monitors systems, networks and infrastructure. Nagios offers monitoring and alerting services for servers. In this article you will learn that how to install nagios server and monitor your hosts.

Prerequisites

You must login on the server as root user or superuser privileges.
A LAMP stack required. Follow this article if you want to install it: Install LAMP in CentOS 7 and RHEL7

Install Dependencies

- Advertisement -
yum install gcc glibc glibc-common gd gd-devel make net-snmp openssl-devel xinetd unzip -y

Create Nagios User and Group

you must create a user and group for Nagios process. Create a “nagios” user and “nagcmd” group, then add the user to the group with these commands:

useradd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios

Install Nagios Core

Download the latest stable release of Nagios Core from the given link copy nagios core link address and download it to your server.

Donwload Nagios Core using command wget

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

Extract archive and switch to Nagios directory

tar -xvf nagios-4.4.3.tar.gz
cd nagios-4.4.3

Configure Nagios Script using below command

./configure --with-command-group=nagcmd
make all

Now run following commands to install Nagios, init scripts, and sample configuration files

make install
make install-commandmode
make install-init
make install-config
make install-webconf
make install-daemoninit

If you want to issue external commands from web interface to Nagios, you need to add the web server user “apache” to “nagcmd” group

usermod -G nagcmd apache

Now create nagios user “nagiosadmin” and restart the apache services.

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

These login details will be used to access the Nagios web interface.

Install Nagios Plugins

Download the latest stable release of Nagios Plugin from the given link copy nagios plugin link address and download it to your server.

Donwload Nagios plugin using command wget

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

Extract archive and switch to Nagios Plugin directory

tar -xvf nagios-plugins-2.2.1.tar.gz
cd nagios-plugins-2.2.1

Configure Nagios Plugin Script using below command

./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl

Now compile Nagios Plugins

make

Now Install Nagios Plugin

make install

Install NRPE

Use following commands

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

Extract NRPE archive and switch to NRPE directory

tar xvf nrpe-2.15.tar.gz
cd nrpe-2.15

Configure NRPE

./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

Now compile and install NRPE

make all
make install
make install-xinetd
make install-daemon-config

Add your server IP address in xinetd startup script

vi /etc/xinetd.d/nrpe

Modify the only_from line and add the IP address of the your Nagios server to the end, in my case it is 192.168.130.138

only_from = 127.0.0.1 192.168.130.138

Save and exit.

Only Nagios server’s IP must be allowed to communicate with NRPE.

Now restart xinetd service to start NRPE

systemctl restart xinetd

Now Nagios Server is installed and we need to configure it.

Configure Nagios

Create the directory which will store the configuration file for each server that will monitor be monitor by Nagios.

mkdir /usr/local/nagios/etc/servers

Now open the main Nagios configuration file

vi /usr/local/nagios/etc/nagios.cfg

Then add below line

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

save and exit

Configure Nagios Contacts

If you want, you can also configure nagios contacts as per your requirements using below command

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

Add a new NRPE command to our Nagios configuration

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

Add the following lines at the end of the file:

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

Save and exit.

Now you will be able to use check_nrpe command in Nagios service definitions.

Nagios is ready to use, Lets restart the services and add them to start on server boot.

systemctl daemon-reload
systemctl restart httpd.service
systemctl start nagios.service
systemctl enable nagios.service

Access Nagios Web Interface

Open your web browser and browse below link to login your Nagios server. You must enter the login credentials that you created earlier. We will use the username “nagiosadmin”

http://nagios_server_ip/nagios

Now you will see default Nagios home page. Under “current Status” left navigation bar Click on the “Hosts” option, to see hosts which are monitoring by the Nagios.

Here you can see Nagios is monitoring only “localhost” it means itself.

Monitor a CentOS 7 Host with NRPE

In this section you will learn that how to add a new host to Nagios server, so it will be monitored. You need to repeat this section for each CentOS or RHEL server you want to monitor.

Login to the server you want to monitor and install the EPEL repository

yum install epel-release -y

Then install Nagios Plugins and NRPE:

yum install nrpe nagios-plugins-all -y

Now modify NRPE configuration file

vi /etc/nagios/nrpe.cfg

Find the line allowed_hosts and add the IP address of your Nagios server, In my case Nagios server address is 192.168.130.138

allowed_hosts=127.0.0.1,::1,192.168.130.138

Save and exit.

this configuration will accept requests from your Nagios server

Now Restart NRPE to apply changes

systemctl start nrpe.service
systemctl enable nrpe.service

All configurations done on host machine, Now we have to add these hosts to Nagios server configuration so it can start monitoring them.

Add Host to Nagios Configuration

On your Nagios server, create a new configuration file for each remote host that you want to monitor in /usr/local/nagios/etc/servers/ direcotry. I will create host1.cfg file for my fisrt host.

vi /usr/local/nagios/etc/servers/host1.cfg

Add the following host definition in host file, replace the host_name value with your remote hostname (In my case it is host1), the alias value with a description of the host, and the address value with the IP address of the remote host (My host1 address is 192.168.130.222)

define host {
use linux-server
host_name host1
alias Apache server host1
address 192.168.130.222
max_check_attempts 5
check_period 24x7
notification_interval 30
notification_period 24x7
}

With above configurations, nagios will only monitor if the host is up or down.

save and exit

Now restart Nagios server to apply new changes.

systemctl reload nagios.service

Once done, access your Nagios web interface go to Under “current Status” left navigation bar Click on the “Hosts” option, and check all hosts which are monitoring by the Nagios. now you will see that host1 is showing up there.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article