Prerequriments:

* 2 ubuntu 20.04 servers i.e for nagios & client with root login enabled


Step 1: update the repository

sudo apt update

Step 2: Enable root login

sudo sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

sudo systemctl restart ssh

sudo passwd  #change the root password if necessay

reboot

Step 3: install dependent packages

ssh root@ip-addr

apt install -y autoconf bc gawk dc build-essential gcc libc6 make wget unzip apache2 php libapache2-mod-php libgd-dev libmcrypt-dev make libssl-dev snmp libnet-snmp-perl gettext

Step 4: download and extract nagios core

cd ~/

wget https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.4.6.tar.gz

tar -xf nagios-4.4.6.tar.gz
cd nagioscore-*/

Step 5: compile and install nagios

./configure --with-httpd-conf=/etc/apache2/sites-enabled
 make all

Step 6: create nagios user, group & add the “www-data” apache user to nagios group

make install-groups-users

usermod -a -G nagios www-data



Step 7:Install nagios binaries, service daemon & command mode script


make install

make install-daemoninit

make install-commandmode


Step 8: Then,install sample script configuration


make install-config

 

Step 9: Now install the Apache configuration for Nagios and enact the mod_rewrite and mode_cgi modules.


make install-webconf

a2enmod rewrite cgi


Step 10:Now restart the Apache service.


systemctl restart apache2


Step 11: Create nagiosadmin user


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

Note: now it will ask for a password for accessing the nagios dashboard.


Step 12: setup firewall rules to allow apache and nagios server port to firewall


for svc in Apache ssh

do

ufw allow $svc

done


Then, re-start the firewall service

ufw enable 

Note :Type ‘y’ to activate the firewall service

ufw status numbered

Note : Both the ssh & apache service is added to firewall


Step 13 : Installing Nagios Plugins and NRPE Plugin


apt install monitoring-plugins nagios-nrpe-plugin



After the installation, go to “/usr/local/nagios/etc” and create a directory for all server hosts configuration


cd /usr/local/nagios/etc

mkdir -p /usr/local/nagios/etc/servers

Step 14: edit nagios config file “nagios.cfg”

nano nagios.cfg


In the config file uncomment the “cfg_dir”

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


Step 15: edit “resource.cfg” file, by changing default configuration as 

nano resource.cfg

$USER1$=/usr/lib/nagios/plugins


Step 16: Edit “object/contacts.cfg” , by adding mail addresses

nano object/contacts.cfg

define contact{

        ......

         email             email@host.com

}


Step 17: Edit “object/commands.cfg” , by adding the following lines at the end of file

nano object/commands.cfg

define command{

         command_name check_nrpe

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

}

Note: All the basic nagios config has been completed


Step 18: Restart the  nagios

systemctl start nagios

systemctl enable nagios

systemctl status nagios


Step 19: Restart apache server

systemctl restart apache2


Now, Open the browser and type the following url

http://ip-address/nagios (http://192.168.0.196/nagios)

Login in with the user “nagiosadmin” and type the password, a prompt will appear





Dashboard will be like:



Now Add Host to Monitor
Step 20: Install NRPE Server on client machine
    ssh root@ip-addr
    apt update
    apt install nagios-nrpe-server monitoring-plugins
Step 21: go to the directory "/etc/nagios" and edit he config file "nrpe.cfg"
    cd /etc/nagios
    nano nrpe.cfg
Change the following
    uncomment the "server_address" and add client address
        server_address=192.168.0.197
        allowed_hosts=127.0.0.1,::1,192.168.0.196
now edit "nrpe_local.cfg" config file in client machine
        nano nrpe_local.cfg
    change the ip addr with client machine ip addr, and paste the configuration in the file
command[check_root]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /
command[check_ping]=/usr/lib/nagios/plugins/check_ping -H 192.168.0.197 -w 100.0,20% -c 500.0,60% -p 5
command[check_ssh]=/usr/lib/nagios/plugins/check_ssh -4 192.168.0.197
command[check_http]=/usr/lib/nagios/plugins/check_http -I 192.168.0.197
command[check_apt]=/usr/lib/nagios/plugins/check_apt

Now restart the nagios  NRPE services
    systemctl restart nagios-nrpe-server
    systemctl enable nagios-nrpe-server

Check the NRPE server is up & running
    systemctl status nagios-nrpe-server

Check Nagios & NRPE server in client
    /usr/lib/nagios/plugins/check_nrpe -H 192.168.0.197
    /usr/lib/nagios/plugins/check_nrpe -H 192.168.0.197 -c check_ping

Step 22: Adding host configuration to nagios server


cd /usr/local/nagios/etc
nano servers/client01.cfg

ADD THIS LINES BY CHANGING NAME AND IP-ADDRESS OF CLIENT

# Ubuntu Host configuration file1

define host {
        use                          linux-server
        host_name                    client01
        alias                        UBUNTU-HOST
        address                      192.168.0.197
        register                     1
}

define service {
      host_name                       client01
      service_description             PING
      check_command                   check_nrpe!check_ping
      max_check_attempts              2
      check_interval                  2
      retry_interval                  2
      check_period                    24x7
      check_freshness                 1
      contact_groups                  admins
      notification_interval           2
      notification_period             24x7
      notifications_enabled           1
      register                        1
}

define service {
      host_name                       client01
      service_description             Check Users
      check_command                   check_nrpe!check_users
      max_check_attempts              2
      check_interval                  2
      retry_interval                  2
      check_period                    24x7
      check_freshness                 1
      contact_groups                  admins
      notification_interval           2
      notification_period             24x7
      notifications_enabled           1
      register                        1
}

define service {
      host_name                       client01
      service_description             Check SSH
      check_command                   check_nrpe!check_ssh
      max_check_attempts              2
      check_interval                  2
      retry_interval                  2
      check_period                    24x7
      check_freshness                 1
      contact_groups                  admins
      notification_interval           2
      notification_period             24x7
      notifications_enabled           1
      register                        1
}

define service {
      host_name                       client01
      service_description             Check Root / Disk
      check_command                   check_nrpe!check_root
      max_check_attempts              2
      check_interval                  2
      retry_interval                  2
      check_period                    24x7
      check_freshness                 1
      contact_groups                  admins
      notification_interval           2
      notification_period             24x7
      notifications_enabled           1
      register                        1
}

define service {
      host_name                       client01
      service_description             Check APT Update
      check_command                   check_nrpe!check_apt
      max_check_attempts              2
      check_interval                  2
      retry_interval                  2
      check_period                    24x7
      check_freshness                 1
      contact_groups                  admins
      notification_interval           2
      notification_period             24x7
      notifications_enabled           1
      register                        1
}

define service {
      host_name                          client01
      service_description             Check HTTP
      check_command                   check_nrpe!check_http
      max_check_attempts              2
      check_interval                        2
      retry_interval                         2
      check_period                        24x7
      check_freshness                  1
      contact_groups                    admins
      notification_interval           2
      notification_period             24x7
      notifications_enabled           1
      register                                 1
    
SAVE & EXIT

Now restart the Nagios Server
    systemctl restart nagios



References: 

https://www.howtoforge.com/tutorial/how-to-install-nagios-on-ubuntu-2004/
https://support.nagios.com/kb/index.php