Nagios Installation and Monitoring


Nagios Installation and Monitoring

       Nagios is one of the most widely used open source monitoring tools which helps us to monitor the services and applications that runs of Windows, Linux, Routers and other network devices.

      With the help of Nagios, we can monitor basic services and attributes. We can access the Nagios using web interface coming with the bundle and configuration need to be done on the file level.

  Prerequisites

       Before installing Nagios, the system need to meet few requirements for installing Nagios. Let's install those prerequisites like httpd(web server), php(scripting language), compilers and development libraries.

        We can install these prerequisties through a single command. Here we go:

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

    
      As we have just installed our prerequisites, let's create a nagios user, nagcmd group for allowing the external commands to be executed through web interface and add nagios, apache user to the nagcmd group.
   
                                       useradd nagios
                                       groupadd nagcmd
                                       usermod -a -G nagcmd nagios
                                       usermod -a -G nagcmd httpd

Install Nagios Core

        Let's download latest version of Nagios Core using the terminal or CML(command line).

                 cd ~
                     curl -L -O https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.1.1.tar.gz
                      tar xvf nagios-*.tar.gz
                      cd nagios-*

      After building Nagios, we must compile it with this command.

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

     Now compltete Nagios with this command.
                    make all
                   make install 
                   make install-init install-config install-commandmode install-webconf

Note: The purpose of the make utility is to determine automatically which pieces of a large program need to be recompiled, and issue the commands to recompile them. you can use make with any programming language whose compiler can be run with a shell command.

Install Nagios Plugins

You can find the latest release of the Nagios Plugin  and install.

 cd ~
curl -L -O http://nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz
tar xvf nagios-plugins-*.tar.gz
cd nagios-plugins-*
./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl
make
make install

Install NRPE 

You can find the source code for the latest stable release of NRPE

 cd ~
curl -L -O http://downloads.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz
tar xvf nrpe-*.tar.gz
cd 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 build and install NRPE and its xinetd startup script with these commands:

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

Open the xinetd startup script in an editor:

                                   sudo vi /etc/xinetd.d/nrpe

Modify the only_from line by adding the private IP address of the your Nagios server to the end

        only_from = 127.0.0.1 {Nagios server Private IP}

Save and exit. Only the Nagios server will be allowed to communicate with NRPE.

Restart the xinetd service to start NRPE:

        service xinetd restart

Nagios Configure:


 Till now, we have installed the nagios, required plugins, we need to configure them. You only need to perform this section once, on your Nagios server.

Organize Nagios Configuration:

Open the main nagios cofiguration file and make changes.

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

Find and uncomment below line now and then save it.

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

Now create the directory that will store the configuration file for each server that you will monitor:

           mkdir /usr/local/nagios/etc/servers

 Configure Nagios contacts: 

  Open the Nagios contacts configuration file and make changes. We have to edit this config file /usr/local/nagios/etc/objects/contacts.cfg by changing the email address.



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

Change the email address filed to receive the notifications.

Configure check_nrpe Command:

Let's add a new command to our Nagios configuration.
 
       vi /usr/loca//nagios/etc/objects/command.cfg

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

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

Configure Apache: 

We have to use htpasswd to create an admin user, called "nagiosadmin", that can access the Nagios web interface:

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

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

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

Now we are all done with Nagios configuration and its time to verify it and to do so please insert following command.

           /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg  >> MOST IMP CONF FILE


Output:
Nagios Core 4.4.3
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2019-01-15
License: GPL

Website: https://www.nagios.org
Reading configuration data...
  Read main config file okay...
  Read object config files okay...

Running pre-flight check on configuration data...

Checking objects...
       Checked 8 services.
       Checked 1 hosts.
       Checked 1 host groups.
       Checked 0 service groups.
       Checked 1 contacts.
       Checked 1 contact groups.
       Checked 24 commands.
       Checked 5 time periods.
       Checked 0 host escalations.
       Checked 0 service escalations.
Checking for circular paths...
       Checked 1 hosts
       Checked 0 service dependencies
       Checked 0 host dependencies
       Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:      0

Things look okay - No serious problems were detected during the pre-flight check

If output shows up as above, configuration file is good and we can proceed to start the Nagios service, nagios on system startup.

 Nagios is ready to be started. Let's do that, and restart Apache:

                    systemctl daemon-reload
                    systemctl start nagios
                    systemctl restart httpd

To enable Nagios to start on server boot, run this command:

               chkconfig nagios on

Login to the Nagios Web Interface


Now access the Nagios web interface using the following URL. You’ll be prompted for the username (nagiosadmin) and password you specified earlier. 

  http://localhost/nagios/


Install Nagios 4.4.3 on CentOS 7 - Nagios Login Screen

Nagios console will look like below.


Install Nagios 4.4.3 on CentOS 7 - Nagios Portal


Click on Hosts in the left pane to get a list of systems being monitored by Nagios. We have not added any host to Nagios, So it simply monitors the localhost itself.

Install Nagios 4.4.3 on CentOS 7 - Nagios Hosts List

 You can now explore the Nagios console, click on Services in the left pane to get the status of any services that are being monitored with Nagios.

Install Nagios 4.4.3 on CentOS 7 - Monitoring Servicces With Nagios

Monitor Remote Linux Systems With Nagios Monitoring Tool


             Let's now add a Linux Host to Nagios Monitoring tool using the NRPE plugin. Nagios Remote Plugin Executor (NRPE) plugin allows us to monitor applications and services running on remote Linux / Windows hosts.

            This NRPE Add-on helps Nagios to monitor local resources like CPU, Memory, Disk, Swap, Online users on the remote Linux machines.

 On Remote Linux Machine, we have to install NRPE Add-on & Nagios plugins. We have to configure epel repository on Centos to proceed further.

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

                           yum install epel-release -y

Now, install NRPE and Nagios plugin on remote host:


                      yum install -y nrpe nagios-plugins-all

  We have to modify the NRPE configuration file to accept the connection from the Nagios server. For this, edit the /etc/nagios/nrpe.cfg file by adding the Nagios server Private IP addresses.

                                             vi /etc/nagios/nrpe.cfg
                      Line 81   allowed_hosts=<Nagios Server IP>

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

Start nrpe Service and nrpe on system startup.

                   service nrpe start
                   service nrpe enable
                   chkconfig nrpe on

Add remote hosts/clients on Nagios server

On your Nagios server, edit the Nagios configuration file to include all .cfg files inside the /usr/local/nagios/etc/servers directory.
 
             vi /usr/local/nagios/etc/servers/yourhost.cfg

Note:  Replace the highlighted word, "yourhost", with the name of your host/

Add the following the host definition as below.

                     define host    {
                                 use                                         linux-server
                                 host_name                             yourhost
                                alias                                       My First Apache Server
                                address                                  <Private IP of remote Host>
                                max_check_attempts              5
                                check_period                          24*7
                                notification_interval                30
                                notification_period                 24*7
                    }

With the above configuration file , 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 particular services, add the below service blocks for services which you want to monitor.

Here are some examples that you can add to your host's configuration file:

Ping:
               define service  {
                       use                                      generic-service
                       host_name                          yourhost
                       service_description             PING
                      check_command                 check_ping!100.0,20%!500.0.60%
              }


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

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

                        systemctl reload nagios.service

Go and check the Nagios web interface to view the new services we added just now.


Monitor Remote Linux Systems With Nagios - Hosts List

Within a minute, you should start seeing status in services page.


Monitor Remote Linux Systems With Nagios - Monitor Services


That's All, Good Luck!!
































      

Comments

Popular posts from this blog

Reverse Proxy with Web-servers

Auto Build of Jenkins Jobs from local GIT