Nagios installation and monitoring Hosts in CentOS 8/RHEL8
What is nagios?
Nagios monitors your entire IT infrastructure to ensure systems, applications, services, and business processes are functioning properly. In the event of a failure, Nagios can alert technical staff of the problem, allowing them to begin remediation processes before outages affect business processes, end-users, or customers.
Prerequisites
1. Setup a LAMP Stack in the server machine
2. Set SELINUX in permissive mode
sed -i 's/SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config
setenforce 0
Step 1: Install dependencies
- Update the system
dnf update
- Install Required Packages
yum install gcc glibc glibc-common gd gd-devel make net-snmp openssl-devel xinetd unzip
Step 2: Create user and Group
- Create a “nagios” user and “nagcmd” group,then add the user to the group
useradd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
Step 3: Download and Install Nagios core
- Download the latest version of Nagios
export VER="4.4.5"
curl -SL https://github.com/NagiosEnterprises/nagioscore/releases/download/nagios-$VER/nagios-$VER.tar.gz | tar -xzf -
- Change the directory into nagios Folder
cd nagios-$VER
- Configure the Nagios before Building it
./configure --with-command-group=nagcmd
- Compile and Install the Nagios core
make all
make install
make install-commandmode
make install-init
make install-config
make install-webconf
In order to issue external commands via the web interface to Nagios, we must add the web server user, apache
, to the nagcmd
group
usermod -G nagcmd apache
Step 4: Download and Install nagios plugins
- Download and install latest version nagios plugins
cd
VER="2.2.1"
curl -SL https://github.com/nagios-plugins/nagios-plugins/releases/download/release-$VER/nagios-plugins-$VER.tar.gz | tar -xzf -
- Change to the plugins source directory
cd nagios-plugins-$VER
- Configure the Nagios Plugins
./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl
- Compile and install Nagios Plugins
make
make install
Step 5: Downalod and Install NRPE
cd
curl -L -O http://downloads.sourceforge.net/project/nag ios/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz
- Extract the NRPE archive
tar xvf nrpe-*.tar.gz
- Change to the extracted directory
cd nrpe-*
- Configure NRPE before Building it
./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
- Compile and Install the NRPE
make all
make install
make install-xinetd
make install-daemon-config
- Open the xinetd startup script
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 YourPrivateIp
- Restart the xinetd service to start NRPE:
service xinetd restart
Step 6: Editing the configuration files
- Open the main Nagios configuration file
vi /usr/local/nagios/etc/nagios.cfg
- Now find an uncomment this line by deleting the
#
:
#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
- Open a another configuration file ==commands.cfg==
vi /usr/local/nagios/etc/objects/commands.cfg
- Add the following to the end of the file:
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
- 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
- Restart the nagios and apache
systemctl daemon-reload
systemctl start nagios.service
systemctl restart httpd.service
Step 7: Accessing the Web Interface
Access the web interface with the following command
http://Nagios_server_IP/nagios
Enter the admin user name and Password
After successfully entering the username and password you will be prompted into nagios home page
Step 8: Setting up the Hosts to Monitor
Login into Host server that you need to monitor
- Install the EPEL repository
yum install epel-release
- Install Nagios Plugins and NRPE in Host
yum install nrpe nagios-plugins-all
- Update the NRPE configuration file
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
allowed_hosts=127.0.0.1,yournagiosserverip
- Restart NRPE to put the change into effect:
systemctl restart nrpe.service
systemctl enable nrpe.service
Login into Nagios Server and Add the following Changes
- Edit the Configuration file
vi /usr/local/nagios/etc/servers/yourhost.cfg
- Add the following code into the file and change the ==host_name== with your host server name , ==address== with your host server ip address, ==alias==value with a description to the host
define host {
use linux-server
host_name hostservername
alias description to the host
address hostserverip
max_check_attempts 5
check_period 24x7
notification_interval 30
notification_period 24x7
}
With the configuration file above, Nagios will only monitor if the host is up or down.
- Restart the Nagios
systemctl restart nagios
- Enter to the nagios interface to watch your hosts
Step 9: Adding extra service Blocks
- You can add extra service blocks in the configuration file to monitor /usr/local/nagios/etc/servers/yourhost.cfg
Adding SSH Service block
define service {
use generic-service
host_name yourhost
service_description SSH
check_command check_ssh
notifications_enabled 0
}
Adding PING service Block
define service {
use generic-service
host_name yourhost
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
After adding you can monitor the service in nagios web interface( In Image shown below we are monitoring two hosts and their SSH service)