Nagios is a popular and open-source application that is used for continuous monitoring of systems, networks, services, and applications. It constantly monitors the status of machines and various services.
In case any of any issue, it provides early warning so that administrator can take required actions.
Nagios performs all checks on local and host machines using the external programs-known as plugins. It also provides you with a web interface that allows viewing the status of hosts and services, history, logs, and generating reports.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to configure Nagios on their Servers.
In this context, we shall look into how to install and configure Nagios on Linux and monitor other Linux server's health.
Before proceeding with Nagios installation, let us understand some terminologies which help you to understand the configurations.
i. Host: It is a device that needs to be monitored such as a physical machine, workstation, or network device.
ii. Service: It is the service or resource being monitored on the host such as HTTP, SMTP, POP, DNS, number of logged-in users, memory usage, etc.
iii. Nagios Plugin: These are scripts that check the status of a host or service and verify whether they are working correctly. There are some predefined plugins that can check basic resources such as processor load, disk usage, ping rates, etc. Plugins are usually located in /usr/local/nagios/libexec directory.
iv. NRPE (Nagios Remote Plugin Executor) add-on: NRPE add-on executes the plugins on the remote Linux hosts which then allows Nagios to keep an eye on local resources (disk space, memory usage, and so on). NRPE add-on comprises of:
a. NRPE daemon– It runs on remote Linux/Unix hosts which needs to be monitored.
b. check_nrpe plugin– It is used by the Nagios monitoring server.
Here, We will be using the two Debian 10 (Buster) servers; both on the same network. On the monitoring server, we will install the Nagios core, Nagios plugin, and check_nrpe plugin. While on the remote host, we will install the Nagios plugin and NRPE.
You must have sudo privileges on both the servers.
The details of our machines are as follows:
1. Nagios Monitoring Server:
Hostname: nagios-server,
IP address: 192.168.72.158
2.Nagios Host:
Hostname: client
IP address: 192.168.72.159
Now, let us look into the steps to install and configure Nagios.
On the Nagios monitoring server, follow the below steps to download and install the Nagios core.
First, we will need to install Prerequisites on the Nagios monitoring server.
To do so, update the packages list using the below command in Terminal:
$ sudo apt update
Then install the required packages:
$ sudo apt install -y autoconf gcc libc6 make wget unzip apache2 php libapache2-mod-php7.4 libgd-dev openssl libssl-dev
Navigate to the /tmp directory:
$ cd /tmp
Then download the Nagios source code using the wget command. We will download the currently available latest version of Nagios that is 4.4.6. You can visit the Nagios Downloads to look for the current stable release:
$ wget -O nagios.tar.gz https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.4.6.tar.gz
The downloaded file will be saved as nagios.tar.gz in the /tmp directory.
Extract the downloaded archive nagios.tar.gz using the below command:
$ tar -xzf nagios.tar.gz
Move into the extracted folder using the cd command:
$ cd nagioscore-nagios-4.4.6/
Run the configure script and specifies the path to the Apache server configuration directory:
$ ./configure --with-httpd-conf=/etc/apache2/sites-enabled
Your configure script may fail in case of any pre-requisite missing. In that case, install the missing pre-requisites and again run the script.
Now compile Nagios using the below command:
$ make all
Now create user and group for Nagios.
$ sudo make install-groups-users
The above command will create a system user and group Nagios.
We will also need to add user "www-data" to the nagios group.
$ sudo usermod -a -G nagios www-data
Then to install binaries, use the below command:
$ sudo make install
To install service files, use the below command:
$ sudo make install-daemoninit
To install and configures the external command file, use the below command:
$ sudo make install-commandmode
Then to install the sample Nagios configuration, use the below command:
$ sudo make install-config
This installs the Apache configuration files, use the below command:
$ sudo make install-webconf
Now enable the Apache modules using the below commands:
$ sudo a2enmod rewrite
sudo a2enmod cgi
Now we will create an Apache user account that can access the Nagios web interface:
$ sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
Now set the password for the nagiosadmin user account. This password will be used to access the Nagios’ web interface.
Then restart Apache for the configuration changes to take effect:
$ sudo systemctl restart apache2
Now we have successfully installed the Nagios core engine.
For the Nagios core to work properly, you will need to install the Nagios plugin.
On the Nagios server, Navigate to the /tmp directory:
$ cd /tmp
Then download the Nagios plugin source code using the wget command. We will download the currently available latest version of the Nagios plugin that is 2.3.3. You can visit the Github page to find the recent stable release:
$ wget -O Nagios-plugins.tar.gz https://github.com/nagios-plugins/nagios-plugins/releases/download/release-2.3.3/nagios-plugins-2.3.3.tar.gz
The downloaded file will be saved as Nagios-plugins.tar.gz in the /tmp directory.
Extract the downloaded archive Nagios-plugins.tar.gz using the below command:
$ tar zxf nagios-plugins.tar.gz
Navigate to the extracted folder using the cd command:
$ cd nagios-plugins-2.3.3/
Run the configure script:
$ sudo ./configure
Now compile the Nagios plugin using the below command:
$ sudo make
$ sudo make install
Now we will install check_nrpe plugin on our Nagios server.
On the Nagios server, Navigate to the /tmp directory:
$ cd /tmp
Then download the check_nrpe source code using the wget command. We will download the recent available version of check_nrpe that is 4.0.3. You can visit the Github page to find the recent stable release:
$ wget -O Nagios-nrpe.tar.gz https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-4.0.3/nrpe-4.0.3.tar.gz
The downloaded file will be saved as Nagios-nrpe.tar.gz in the /tmp directory.
Extract the downloaded archive nagios.tar.gz using the below command:
$ tar -xzf Nagios-nrpe.tar.gz
Navigate to the extracted folder using the cd command:
$ cd nrpe-4.0.3/
Run the configure script:
$ sudo ./configure
Now compile the check_nrpe using the below command:
$ sudo make check_nrpe
$ sudo make install-plugin
Now, we will configure the Nagios main configuration file. Edi the Nagios configuration file nagios.cfg using the below command:
$ sudo nano /usr/local/nagios/etc/nagios.cfg
Now find the below line in the file and uncomment it by removing the # character from the beginning of the line.
#cfg_dir=/usr/local/nagios/etc/servers
The above line specifies the directory which contains a configuration file for all the hosts that the Nagios server will monitor.
Now save and close the configuration file.
Now we will create the directory in the path defined above:
$ sudo mkdir /usr/local/nagios/etc/servers
Now we will configure the contacts that should be notified in case of any event. Edit the Nagios contacts configuration using the below command.
In order to receive alerts from the Nagios monitoring server, you will need to configure the contacts.
Open the contacts configuration file, and specify the email address you would like to receive the emails from Nagios:
$ sudo nano /usr/local/nagios/etc/objects/contacts.cfg
In the email directive, replace nagios@localhost with your own email address and then save and close the file.
Now in order to use check_nrpe plugin, we will add a new command in the command configuration file.
i. Edit the command configuration file commands.cfg using the below command:
$ sudo nano /usr/local/nagios/etc/objects/commands.cfg
ii. Append the following lines to the end of the commands.cfg file and then save and close it:
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
iii. Now start Nagios service:
$ sudo systemctl start Nagios.service
i. Now access the Nagios web interface by visiting the below address in any web browser:
http://nagios-server-ip/nagios
In our scenario, it would be:
http://192.168.72.158/nagios
ii. In the Authentication Required dialog, type nagiosadmin as the username and password you have set earlier.
Once logged in to the web interface, you will see the default Nagios web page.
iii. Go to the Hosts tab from the left sidebar to view the monitored hosts.
At present, you will see only one host "localhost" which is the Nagios server itself.
In order to monitor other hosts, you will need to install the NRPE daemon on them.
Now you will have to install the Nagios plugin and NRPE on the hosts that need to be monitored on the Nagios server.
In the remote host (client) that needs to be monitored, follow the below steps to install Nagios plugin.
i. First, update the apt:
$ sudo apt update
ii. Then issue the below command to install the required packages:
$ sudo apt install autoconf gcc libmcrypt-dev make libssl-dev dc build-essential gettext
iii. Now, Navigate to /tmp and download the Nagios plugin.
$ cd /tmp
$ wget -O Nagios-plugins.tar.gz https://github.com/nagios-plugins/nagios-plugins/releases/download/release-2.3.3/nagios-plugins-2.3.3.tar.gz
iv. Extract the downloaded archive:
$ tar -zxf nagios-plugins.tar.gz
v. Move into extracted folder and run the configure script:
$ cd nagios-plugins-2.3.3//
$ sudo ./configure
vi. Compile the plugin:
$ sudo make
$ sudo make install
Follow the below steps to install NRPE on the remote host.
i. First, update the apt:
$ sudo apt update
ii. Issue the below command to install the pre-requisites:
$ sudo apt install -y autoconf automake gcc libc6 libmcrypt-dev make libssl-dev
iii. Now Navigate to /tmp and download NRPE.
$ cd /tmp
$ wget -O nrpe.tar.gz https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-4.0.3/nrpe-4.0.3.tar.gz
iv. Extract the downloaded archive:
$ tar xzf nrpe.tar.gz
v. Move into the extracted folder and run the configure script:
$ sudo ./configure --enable-command-args
vi. Compile the NRPE with the following commands:
$ sudo make all
$ make install-groups-users
$ sudo make install
$ sudo make install-config
$ sudo make install-init
Now if a firewall is running on your remote host, you will need to open port 5666 (used by NRPE) through it.
$ sudo ufw allow 5666/tcp
Now open the NRPE configuration file in the remote host:
$ sudo nano /usr/local/nagios/etc/nrpe.cfg
You will need to find and modify the below entries:
Add client IP address:
server_address=192.168.72.159
Add Nagios server IP address:
allowed_hosts=127.0.0.1,192.168.72.158
Also, replace /dev/vda1 with your root file system:
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1
Then start the NRPE service:
$ sudo systemctl start nrpe.service
Now check if the NRPE service is properly running:
$ sudo systemctl status nrpe.service
The active (running) in the output shows that the service is successfully running without any issues. You can also see the NRPE is allowing connection from 192.168.72.158 which is the IP address of the Nagios server.
Now to check if the check_nrpe on the Nagios server can communicate with the NRPE on the remote host, issue this command on the Nagios server:
$ /usr/local/nagios/libexec/check_nrpe -H remote_host_ip
Now in the Nagios monitoring server, you will need to create separate configuration files in the /usr/local/nagios/etc/servers/ for all remote hosts that you need to monitor.
i. For our host machine, we will create a new configuration file client.cfg:
$ sudo nano /usr/local/nagios/etc/servers/client.cfg
Replace client with the name of your host.
ii. Now add host definition to the new configuration file replacing the value of host_name with the remote hostname, alias with a short description, and the address with IP address of the remote host.
iii. This configuration will check if the host machine is up or down.
define host {
use linux-server
host_name client
alias client machine
address 192.168.72.159
max_check_attempts 5
}
iv. Now add service definition to the configuration file for monitoring the remote host. Replace the value of host_name with the remote hostname.
Note: These service definitions will use the commands preconfigured in the /usr/local/nagios/etc/nrpe.cfg file on the remote host.
Add the following service block to monitor the CPU load on the remote host
define service {
use generic-service
host_name client
service_description CPU Load
check_command check_nrpe!check_load
}
c on the remote host:
define service{
use generic-service
host_name client
service_description Total Processes
check_command check_nrpe!check_total_procs
}
v. Add the following service block to monitor disk usage on the remote host:
define service {
use generic-service
host_name client
service_description Check Disk
check_command check_nrpe!check_disk
}
vi. Add the following service block to monitor the number of users currently logged in on the remote host:
define service {
use generic-service
host_name client
service_description Current Users
check_command check_nrpe!check_users
}
vii. Now save and close the configuration file.
viii. Restart the Nagios service to apply the configuration changes:
$ sudo systemctl restart Nagios
ix. Now access the web interface using the http://nagios-server-ip/nagios. Wait for a while and your remote host will be added to the Nagios web interface.
x. Go to Hosts from the left navigation bar and you will see your remote host listed there.
xi. Then go to the Services tab, and you will see the status of all the services for a host you have configured for monitoring.
This article will guide you on how to install and configure the #Nagios #monitoring solution on Linux. We have also explained how to setup remote hosts for monitoring. Now you can easily monitor the health of #Linux #hosts and the services running on them.
Nagios is an open source monitoring system for computer systems. It was designed to run on the Linux operating system and can monitor devices running Linux, Windows and Unix operating systems (OSes).
Nagios provides complete monitoring of applications and application state – including Windows applications, Linux applications, UNIX applications, and Web applications.
To install Nagios Plugins and NRPE daemon on the Remote Linux Host:
1. Install Required Dependencies.
2. Create Nagios User.
3. Install the Nagios Plugins.
4. Extract Nagios Plugins.
5. Compile and Install Nagios Plugins.
6. Install Xinetd.