18 C
Texas

How To Monitor Remote Linux Machines With Nagios on Linux

Today we are going to learn that how to monitor remote Linux machines with Nagios on Linux. We’ve previously installed and configured Nagios on Ubuntu 20.04 and now we are moving forward to monitor the remote machines with Nagios. If you didn’t follow the previous article click on the below link first to set up the Nagios on your Ubuntu 20.04 system.

How To Install Nagios on Ubuntu 20.04

Here I’ll add CentOS machine as a remote host on Ubuntu 20.04. You can also add other Linux remote hosts. For easy setup just follow the below steps.

Step 1: Update Your System

As usual we do, first of all update the system to fetch latest updates before going to install any new package.

- Advertisement -
sudo apt update && sudo apt upgrade

Step2: Install NRPE Add-on and Nagios Plugins on Ubuntu 20.04

NRPE (Nagios Remote Plugin Executor) helps you to monitor the remote machines as well as monitor the local resources of the remote machine like CPU usage, Memory usage, Disk and other resources information’s. Install it by hitting the below command.

On Ubuntu

sudo apt install -y nagios-nrpe-server nagios-plugins

On CentOS :

To install Nagios server on CentOS firstly add EPEL repository.

sudo rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

As Nagios requires some other packages install them by typing

And then run the below command to install it.

sudo yum install -y nrpe nagios-plugins-all

Step 3: Configure NRPE Add-on

After installing the NRPE Add-on configure it to work best with our environment. Add the Nagios Server IP address by updating the /etc/nagios/nrpe.cfg file. Type the command to edit the file.

sudo nano /etc/nagios/nrpe.cfg

And then add the Nagios server IP address like below.

allowed_hosts=192.168.0.10

You can also add multiple Nagios servers by separating them with commas.

Step 4: Configuring Nagios Checks

Nrpe configuration file /etc/nagios/nrpe.cfg consists of basic commands to find the CPU, Memory, Disk usage etc.

Given command lines allow you to monitor logged in users, system load, root filesystem usage, swap usage and the total number of the processes by Nagios plugins. Run the below command.

sudo nano /etc/nagios/nrpe.cfg

Commands Explanation For CentOS & Ubuntu

command[check_users]=/usr/lib/nagios/plugins/check_users -w 5 -c 10
command[check_load]=/usr/lib/nagios/plugins/check_load -w 15,10,5 -c 30,25,20
command[check_root]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /
command[check_swap]=/usr/lib/nagios/plugins/check_swap -w 20% -c 10%
command[check_total_procs]=/usr/lib/nagios/plugins/check_procs -w 150 -c 200

Note:
-w stand for WARNING
-c stand for CRITICAL

You can verify the check results by hitting the below command.

/usr/lib/nagios/plugins/check_procs -w 150 -c 200

Output:

You’ll see the similar output

PROCS CRITICAL: 283 processes | procs=190;150;200;0;

You can also change the warnings and critical to your own value like

/usr/lib/nagios/plugins/check_procs -w 300 -c 350

Output:

PROCS OK: 282 processes | procs=282;300;350;0;

To enable the monitoring, add the given destinations to the template file on the Nagios Server. Now, restart the NRPE services by hitting the below command.

sudo systemctl restart nagios-nrpe-server

Step 5: Allow Firewall Access

After setting up the Nagios NRPE allow Nagios server to reach the NRPE server by giving access through firewall. To do this simply fire the below command in your terminal.

firewall-cmd --permanent --add-port=5666/tcp

And then reload the firewall by

firewall-cmd --reload

Add the following IP Tables rules.

iptables -I INPUT -p tcp --dport 5666 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
iptables -I OUTPUT -p tcp --sport 5666 -m conntrack --ctstate ESTABLISHED -j ACCEPT
/etc/init.d/iptables save

Step 6: Installing NRPE Plugin on Nagios Server

Simply fire the below command to install the NRPE plugin on Nagios server.

On CentOS:

sudo rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo yum -y install nagios-plugins-nrpe

On Ubuntu:

sudo apt install -y nagios-nrpe-plugin

Now, edit the /usr/local/nagios/etc/nagios.cfg file and uncomment the below line:

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

Then make a new config directory.

On CentOS:

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

On Ubuntu:

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

After setting up all these, it’s time to configure the Nagios server for monitoring the remote client machines. For this you’ll have to use the check_nrpe plugi. So, here we’ll add the command definition in order to remotely monitor our hosts. Type the below command.

On CentOS:

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

Paste the below content into file and save it.

.check_nrpe. command definition
define command{
command_name check_nrpe
command_line /usr/lib/nagios/plugins/check_nrpe -H $HOSTADDRESS$ -t 30 -c $ARG1$
}

On Ubuntu:

Create a file.

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

Then paste the below data

.check_nrpe. command definition
define command{
command_name check_nrpe
command_line /usr/lib/nagios/plugins/check_nrpe -H $HOSTADDRESS$ -t 30 -c $ARG1$
}

Step 7: Adding a Linux Host To Nagios Server

Now, we’ll add the remote host to monitor it by creating a client config file for defining the host & services definition of remote Linux host.

On CentOS:

sudo nano /usr/local/nagios/etc/servers/client.osradar.local.cfg

On Ubuntu:

sudo nano /usr/local/nagios/etc/servers/client.osradar.local.cfg

Then paste the below configuration into the file and save it.

define host{
use linux-server host_name client.osradar.local alias client.osradar.local address 192.168.135.135
}
define hostgroup{
hostgroup_name linux-server alias Linux Servers members client.osradar.local
}
define service{
use local-service host_name client.osradar.local service_description SWAP Uasge check_command check_nrpe!check_swap
}
define service{
use local-service host_name client.osradar.local service_description Root / Partition check_command check_nrpe!check_root
}
define service{
use local-service host_name client.osradar.local service_description Current Users check_command check_nrpe!check_users
}
define service{
use local-service host_name client.osradar.local service_description Total Processes check_command check_nrpe!check_total_procs
}
define service{
use local-service host_name client.osradar.local service_description Current Load check_command check_nrpe!check_load
}

After it make sure to verify the Nagios by hitting the below commands.

On CentOS:

sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

On Ubuntu:

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

And then restart the services.

sudo systemctl restart nagios

Step 8: Monitor Remote Hosts with Nagios .

Now, access the Nagios Dashboard as we’ve already installed Nagios in previous article. So, access it by typing IP/nagios/ and provide credentials to login.

Here you can see the remote hosts.

Wait for a minute and you’ll see the status of the services running on the remote machine as well as on your local machine.

So, this is how you can add remote Linux machines to monitor with the help of Nagios.

- 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