Install Nagios 4 in CentOS7 and monitor your servers in CentOS7

Nagios 4 installation in CentOS 7 server and monitor CentOS 7 servers

Nagios, is a free and open source computer-software application that monitors systems, networks and infrastructure. Nagios offers monitoring and alerting services for servers, switches, applications and services. It alerts users when things go wrong and alerts them a second time when the problem has been resolved.

In this tutorial, you will find the steps for the installation of Nagios 4 on CentOS 7. You will also find basic configuration, so that you will be able to monitor host resources via the web interface. You will also find the steps to use the Nagios Remote Plugin Executor (NRPE), that will be installed as an agent on remote hosts, to monitor their local resources.

Nagios is useful for keeping a detail list of all your servers, and making sure your critical services are up and running.

To install Nagios in a machine from root user use below steps:

-Create nagios userid

sudo adduser nagios
sudo passwd nagios
sudo gpasswd -a nagios wheel

 

 -Create group nagcmd, and add user “nagios” to the group with these commands

sudo groupadd nagcmd
sudo usermod -a -G nagcmd Nagios

sudo usermod -G nagcmd apache Run this command to add the webserver user (apache) to the nagcmd group.

 

-Change to Nagios user:

su – nagios

 

-Disable SELinux

Prior to starting our install, we need to disable SELinux.

sudo setenforce 0
Modify /etc/selinux/config and change enforcing to disabled.

 

-Update the server:

Use Nagios account to run below commands:

sudo yum update
sudo yum install wget — install wget in your system
sudo yum install net-tools — install net-tools in your system

 

-Install Build Dependencies

As we are building Nagios Core from source, we need to install few development libraries. These libraries will allow us to complete the build.
Command to install the required packages:

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

 

-Install Apache

Apache is the most popular web server in the world.

sudo yum install httpd — this command will install apache
sudo systemctl start httpd.service — this command will start apache in the machine
sudo systemctl enable httpd.service — this command will enable apache to start on boot

you can check if apache is installed properly in your system by hitting below link in web browser:

http://your_IP_Address//

 

-Install MySQL :

We will install MariaDB, which is a MySQL drop-in replacement. MariaDB is a community-developed fork of the MySQL relational database management system. MariaDB it will organize and provide access to databases where our site can store information. Commands to install MySQL are as listed below:

sudo yum install mariadb-server mariadb -- install mariadb
sudo systemctl start mariadb -- this command will start mariadb
sudo mysql_secure_installation -- run simple security script

You will be asked for root password, but as you have just installed MySQL you will not be having it, so leave it blank by pressing enter. Then enter Y to set new root password.

For rest of the questions, you can simply hit “Enter” key.

sudo systemctl enable mariadb.service — enable MariaDB to start on boot.

 

-Install PHP

PHP is an open source general-purpose scripting language, that is used for web development and can be embedded into HTML. PHP helps in display of dynamic content.

sudo yum install php php-mysql — This command will install php

Restart web server (Apache) for it to work with php.

sudo systemctl restart httpd.service — restart apache.

Install PHP FastCGI Process Manager (php-fpm) – It is a PHP Modules

sudo yum install php-fpm

TEST PHP

In CentOS 7, directory is located at /var/www/html/. We can create info.php file at that location by typing:

sudo vi /var/www/html/info.php

This will open a blank file. We want to put the following text, which is valid PHP code, inside the file:

<?php phpinfo(); ?>

If you are running a firewall, run the following commands to allow HTTP and HTTPS traffic:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

Now open url : http://IP_address_of_your_machine/info.php

You should remove this file after this test, as it could actually give information about your server to unauthorized users. To do this, you can type this: sudo rm /var/www/html/info.php

 

-Your LAMP stack is installed and configured.

LAMP stack stands for Linux, Apache, MySQL and PHP/Phython/PERL. It is an open source web platform commonly used to run dynamic web sites. It is considered as platform of choice for development and deployment of high performance web applications which require a strong and reliable foundation.

 

-Install Nagios 4

To install Nagios, you need to first download the source code for the latest stable release of Nagios Core. To download visit the page: https://www.nagios.org/downloads/core-stay-informed/ and either enter your details or click the Skip to download link below the form. Then copy the link address for the latest stable release. At the time of writing this post, the latest stable release is Nagios 4.3.2.

Then follow below steps

cd ~ — change directory to your home directory.

curl -L -O https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.2.tar.gz This command will download latest stable Nagios to your present directory.

tar xvf nagios-*.tar.gz this command will extract the Nagios.

cd nagios-* change directory to Nagios folder.

sudo ./configure --with-command-group=nagcmd use this command to configure Nagios

sudo make all this command will compile Nagios

Execute below make commands to first install Nagios, then install init scripts and finally install sample configuration files.

sudo make install
sudo make install-commandmode
sudo make install-init
sudo make install-config
sudo make install-webconf

 

-Install Nagios Plugins

To install Nagios plugin, do visit the page: http://nagios-plugins.org/download/?C=M;O=D. From their copy the link address for the latest stable release. At the time of writing this post, the latest release is Nagios Plugin 2.2.1.

cd ~
curl -L -O http://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
tar xvf nagios-plugins-*.tar.gz
cd nagios-plugins-*

sudo ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl – Execute this command to configure the Nagios plugin.

sudo make – command to compile Nagios plugin

sudo make install – this command will install Nagios plugin.

 

-Install NRPE

NRPE allows you to remotely execute Nagios plugins on other Linux/Unix machines. This allows you to monitor remote machine metrics (disk usage, CPU load, etc.). NRPE can also communicate with some of the Windows agent add-ons, so you can execute scripts and check metrics on remote Windows machines as well.

To install NRPE, visit the page: https://sourceforge.net/projects/nagios/files/ . From their copy the link address for the latest stable release. At the time of writing this post, the latest release is Nagios Plugin 3.1.0

cd ~
curl -L -O https://sourceforge.net/projects/nagios/files/nrpe-3.x/nrpe-3.1.0/nrpe-3.1.0.tar.gz
tar xvf nrpe-*.tar.gz
cd nrpe-*
sudo ./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

Use below commands to build and install NRPE :

sudo make all
sudo make install
sudo make install-config

Update Services File – To update service file, open file “/etc/services” and at the end of the file enter “nrpe 5666/tcp # Nagios services”. Commands used are:

vim /etc/services (then shift + G to go the end line of the file then add “nrpe 5666/tcp # Nagios services” and finally save and exit.

 

[nagios@nagios-centos ~]$ tail -1 /etc/services

nrpe 5666/tcp # Nagios services

Then from directory nrpe-3.1.0 enter below commands, which will install service/daemon.

sudo make install-init
sudo systemctl enable nrpe.service

Run below two commands to configure firewall:

firewall-cmd --zone=public --add-port=5666/tcp
firewall-cmd --zone=public --add-port=5666/tcp –permanent

Then you need to update configuration file

The file nrpe.cfg is where the following settings will be defined. It is located:

/usr/local/nagios/etc/nrpe.cfg

allowed_hosts=

At this point NRPE will only listen to requests from itself (127.0.0.1). If you wanted your nagios server to be able to connect, add it’s IP address after a comma (in this example it’s 104.198.73.135):

allowed_hosts=127.0.0.1,104.198.73.135,::1

dont_blame_nrpe=

This option determines whether or not the NRPE daemon will allow clients to specify arguments to commands that are executed. We are going to allow this, as it enables more advanced NPRE configurations.

dont_blame_nrpe=1

Service / Daemon Commands

Below are the commans for starting / stopping / restarting / status NRPE.

sudo systemctl start nrpe.service
sudo systemctl stop nrpe.service
sudo systemctl restart nrpe.service
sudo systemctl status nrpe.service

Steps to Test NRPE

Now check that NRPE is listening and responding to requests, run below command to test NRPE.

/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1

You should see the output similar to the following:

NRPE v3.1.0-rc1

If you get the NRPE version number (as shown above), NRPE is installed and configured correctly.

You can also test from your Nagios host by executing the same command above, but instead of 127.0.0.1 you will need to replace that with the IP Address / DNS name of the machine with NRPE running.

 

-Configure Nagios

You need to configure Nagios in your Nagios server just once.

Open the main Nagios configuration file in your favorite text editor. We’ll use vim to edit the file:

sudo vim /usr/local/nagios/etc/nagios.cfg

Now you need to find and uncomment this line by deleting the #:

#cfg_dir=/usr/local/nagios/etc/servers then Save and exit the file

Now you need to create the “servers” directory. The purpose of that directory will be to store the configuration file for each server that you will monitor:

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

Configure Nagios Contacts

Open the Nagios contacts configuration in your favorite text editor. We’ll use vim to edit the file:

sudo vim /usr/local/nagios/etc/objects/contacts.cfg

Find the email directive, and replace its value (the highlighted part) with your own email address:

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

Save and exit.

Configure check_nrpe Command

You need to add a new command to Nagios configuration. First open the commands.cfg file ( the file location is /usr/local/nagios/etc/objects)

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

and then add the following to the end of the file:

define command{

command_name check_nrpe

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

}

Save and exit. This allows you to use the check_nrpe command in your Nagios service definitions.

 

Configure Apache

Use htpasswd to create an admin user, called “nagiosadmin”, that can access the Nagios web interface:

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

You need to enter a password at the prompt. Remember this login, as you will need it to access the Nagios web interface.

Note: If you do not want to create a user that is not named “nagiosadmin”, you will need to edit file /usr/local/nagios/etc/cgi.cfg and change all the “nagiosadmin” references to the user you created.

 

-Nagios is now ready to be started. First let’s start Nagios and restart Apache:

sudo chkconfig nagios on
sudo systemctl start nagios.service
sudo systemctl restart httpd.service

Nagios is now running, so let’s try and log in.

Accessing the Nagios Web Interface

Open your web browser of your choice, and open the Nagios server

http://IP_of_nagios_server/nagios

Because we configured Apache to use htpasswd, you must enter the login credentials that you created earlier. We used “nagiosadmin” as the username:

 

After authenticating, you will be see the default Nagios home page.

Click on the Hosts link, in the left navigation bar, to see which hosts Nagios is monitoring:

As you can see, Nagios is monitoring only “localhost”, or itself.

Let’s monitor another host with Nagios!

 

-Monitor a CentOS 7 Host with NRPE

In this section, we’ll show you how to add a new host to Nagios, so it will be monitored. Repeat this section for each CentOS or RHEL server you wish to monitor.

On a server that you want to monitor, install the EPEL repository:

sudo yum install epel-release

Now install Nagios Plugins and NRPE:

sudo yum install nrpe nagios-plugins-all

Now, let’s update the NRPE configuration file. Open it in your favorite editor (we’re using vi):

sudo vi /etc/nagios/nrpe.cfg

Find the allowed_hosts directive, and add the private IP address of your Nagios server to the comma-delimited list ( IP 104.198.73.135 is an example):

allowed_hosts=127.0.0.1,104.198.73.135

Save and exit. This configures NRPE to accept requests from your Nagios server, via its private IP address.

Restart NRPE to put the change into effect:

sudo systemctl start nrpe.service
sudo systemctl enable nrpe.service

Once you are done installing and configuring NRPE on the hosts that you want to monitor, you should add these hosts to your Nagios server configuration before it will start monitoring them.

Add Host to Nagios Configuration

On your Nagios server, create a new configuration file for each of the remote hosts that you want to monitor in /usr/local/nagios/etc/servers/. “nagios-client” hostname is used as an example.

sudo vi /usr/local/nagios/etc/servers/nagios-client.cfg

Add in the following host definition, replacing the host_name value with your remote hostname , the alias value with a description of the host, and the address value with the private IP address of the remote host:

define host {

use linux-server

host_name nagios-client

alias My first nagios client

address 104.197.176.187

max_check_attempts 5

check_period 24×7

notification_interval 30

notification_period 24×7

}

With the configuration file above, Nagios will only monitor if the host is up or down. If this is sufficient for you, save and exit then restart Nagios. If you want to monitor services, read on.

Add any of these service blocks for services you want to monitor. Note that the value of check_command determines what will be monitored, including status threshold values. Here are some examples that you can add to your host’s configuration file:

Ping:

define service {

use generic-service

host_name nagios-client

service_description PING

check_command check_ping!100.0,20%!500.0,60%

}

SSH (notifications_enabled set to 0 disables notifications for a service):

define service {

use generic-service

host_name nagios-client

service_description SSH

check_command check_ssh

notifications_enabled 0

}

If you’re not sure what use generic-service means, it is simply inheriting the values of a service template called “generic-service” that is defined by default.

Now save and quit. Reload your Nagios configuration to put any changes into effect:

sudo systemctl reload nagios.service

Once you are done configuring Nagios to monitor all your remote hosts, you should be set. Be sure to access your Nagios web interface, and check out the Services page to see all your monitored hosts and services.

 

-Now that you can monitor your multiple hosts and various services running on the host, you can list which services are critical and can monitor those through Nagios. You can also set up notification, for example you receive an email when your disk utilization reaches a warning or critical threshold.

You can also set notification, so that you are alerted if some services need immediate attention or your website is down or best being alerted/notified before a problem occurs.

Leave a Reply

Your email address will not be published. Required fields are marked *