Nagios also known as Nagios Core is one of the oldest free and open source application used for monitoring Servers, network infrastructures, applications and boasts one of the most comprehensive monitoring solutions.
It features a plugin based methodology to collect system/application performance metrics, or even allows to configure your own monitoring scripts making Nagios a robust and flexible product to own.
In this article, we will find how to install Nagios 4.4.2 along with its plugin in CentOS 7 to monitor the host resources through a well designed Nagios web interface.
Finally we will also check, how to setup Nagios Remote Plugin Executor (NRPE) in a remote host to collect its system and performance metrics.
Prerequisites
- Web server (in this case Apache) installed and configured
- PHP installed
- You have “root” or “sudo” access to two CentOS 7 system, one for installing Nagios server(core) and another to act as a Nagios client that will send its performance metrics to Nagios server.
- The two system must be running in the same subnet.
How to Install Nagios Core
Nagios and its associated packages are available in EPEL repository. So one way to install it by using “yum” but since we are interested in version 4.4.2, therefore we will download the specific Nagios source package and compile/install it from source.
To start with, create an user especially for running Nagios commands to fetch various system/application metrics.
# useradd nagios
Then add the newly created user to the Nagios group:
# usermod -a -G nagios nagios
Compilation of Nagios from source also needs few development packages like compilers, libraries and headers.
Install them using the following commands in the terminal:
# yum install epel-release # yum install postfix gettext wget net-snmp-utils openssl-devel glibc-common unzip perl gcc automake autoconf make glibc gd-devel net-snmp perl-Net-SNMP
The Nagios web interface which is a part of Nagios core needs a web server and php to be installed as a prerequisites.
If you have not installed it earlier then install Apache as a web server along with php using the following commands and start/enable Apache.
# yum install httpd php # systemctl enable httpd # systemctl start httpd
Open the port number 80 of Apache in the firewall settings so that traffic can reach to the web server.
# firewall-cmd --zone=public --add-port=80/tcp --permanent # firewall-cmd --reload
Head over to Nagios release page in order to download the latest release of Nagios Core and extract it in a suitable location.
# cd /usr/local/src/ # wget https://github.com/NagiosEnterprises/nagioscore/releases/download/nagios-4.4.2/nagios-4.4.2.tar.gz # tar zxvf nagios-4.4.2.tar.gz
Change to the extracted folder and pass the user name and group that we have created earlier to the “configure” script to start the build process.
Moreover, If you want Nagios to send email for alerting then pass the path of “sendmail” as well to the “configure” script.
# cd /usr/local/src/nagios-4.4.2 # ./configure --with-nagios-group=nagios --with-command-group=nagios --with-mail=/usr/sbin/sendmail
Once the above “configure” script runs successfully, you will see the following configuration summary at the end.
Now to compile Nagios, issue the following command:
# make all
Next to install Nagios along with the init scripts and other default configuration, use the following “make” scripts one by one.
# make install
The following command will install the external command file that facilitates Nagios Core to work from the command line:
# make install-commandmode
To install the Nagios initialization script for managing the Nagios service, run the following command:
# make install-init
Next, run the following command to install the Nagios sample configuration files:
# make install-config
Finally, the following “make” command will install the Apache configuration file(nagios.conf) in “/etc/httpd/conf.d/”
# make install-webconf
This completes the compilation and installation of Nagios core in CentOS 7. But still we need to adjust few settings to access Nagios web interface.
To start with add the web server user i.e “apache” to the “nagios” group so that external commands can be executed via the web interface of Nagios,
# usermod -G nagios apache
To protect the Nagios web interface create an user using “htpasswd”. It will prompt you for a password, choose a reasonably strong password and restart Apache.
# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin # systemctl restart httpd
Finally start Nagios and enable it to start automatically during system reboot.
# systemctl enable nagios # systemctl start nagios
Open your favorite web browser and point it to “http://SERVER_IP/nagios”. Type in the user name and password that you have just created, Nagios web interface will be available to you.
Remember that, since we haven’t added any remote host for monitoring, therefore no host will be available in the web interface at this moment except the host on which Nagios has been installed. But the status of the host will be down until, we install the Nagios Plugin in the next step.
Install Nagios Plugin
To compile Nagios plugin, few development packages and headers are needed as a prerequisites and are already installed in the previous step.
So head over to Nagios plugin release page and download the latest version.
# cd /usr/local/src/ # wget https://github.com/nagios-plugins/nagios-plugins/archive/release-2.2.1.tar.gz
Once the Nagios Plugins archive is extracted, issue the following commands from terminal to compile and install the Nagios Plugins in your server:
# tar zxvf release-2.2.1.tar.gz # cd nagios-plugins-release-2.2.1/ # ./autogen.sh # sudo ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl # make # make install
Restart Nagios
# service nagios restart
Now visit the Nagios web interface and click hosts from left sidebar, you will find that the host on which Nagios has been installed is up for monitoring.
Remember, By default, Nagios only monitors the host on which it is installed.
Monitor Remote Host using NRPE
Until now, Nagios Core and Nagios Plugins have been installed only to monitor the host on which it is running.
Let us now install Nagios Remote Plugin Executor(NRPE) that allows us to execute commands on a remote host using Nagios web interface for monitoring and collecting system metrics from a remote host.
The steps involved to achieve this are:
- Install “check_nrpe” plugin in the Nagios server.
- Install Nagios on remote host.
- Install NRPE daemon in the remote host.
- Configure NRPE in the remote host.
- Configure Nagios server to collect remote host’s performance metrics.
1. Install check_nrpe plugin in Nagios Server
Download and extract NRPE from its release page in “/usr/local/src”.
# cd /usr/local/src # wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.2.1/nrpe-3.2.1.tar.gz # tar zxvf nrpe-3.2.1.tar.gz
Change to the extracted nrpe directory and configure the “Makefiles” using the following commands from the terminal.
# cd nrpe-3.2.1 # ./configure --with-ssl=/usr/bin/openssl --with-ssl-lib=/usr/lib/x86_64-linux-gnu
Finally to install “check_nrpe” command run the following two commands from the terminal:
# make check_nrpe # make install-plugin
Define the “check_nrpe” command in “/usr/local/nagios/etc/objects/commands.cfg” file that will be used by Nagios(Server) to collect monitored parameters from a remote host.
Add the following snippets of code at the end of the file that defines the name of the command and specifies the command-line options to execute the plugin.
define command{ command_name check_nrpe command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ }
2. Install Nagios Plugin in Remote Host
This step involves installing the Nagios plugin in the remote host exactly like step 2 where we have installed the plugins in the server to collect its own performance metrics.
But since we are also interested in collecting performance metrics from a remote host, so we need to install Nagios plugins in the remote host as well.
These plugins in the remote host will be called at the behest of Nagios server for collecting various system metrics of remote host.
# useradd nagios # yum install gettext wget openssl-devel glibc-common perl gcc automake autoconf make glibc # cd /usr/local/src/ # wget https://github.com/nagios-plugins/nagios-plugins/archive/release-2.2.1.tar.gz # tar zxvf release-2.2.1.tar.gz # cd nagios-plugins-release-2.2.1/ # ./autogen.sh # sudo ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl # make # make install
Change to the plugin directory and list all of them:
# cd /usr/local/nagios/libexec # ls
3. Install NRPE daemon in remote host
NRPE(Nagios Remote Plugin Executor) is a daemon that is needed in every remote host that you want to monitor.
To install NRPE daemon in the remote host, download and extract NRPE from its release page in “/usr/local/src”.
Remember the development libraries and headers needed for compiling NRPE daemon are already installed in the remote host as part of installing Nagios plugin in the previous step.
# cd /usr/local/src # wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.2.1/nrpe-3.2.1.tar.gz # tar zxvf nrpe-3.2.1.tar.gz
Change to the extracted nrpe directory and configure the `Makefiles`using the following commands from the terminal.
# cd nrpe-3.2.1 # ./configure --with-ssl=/usr/bin/openssl --with-ssl-lib=/usr/lib/x86_64-linux-gnu
Finally to install NRPE, run the following series of commands from the terminal:
# make all # make install # make install-plugin # make install-daemon # make install-config # make install-inetd # make install-groups-users # make install-init
Edit the file “/etc/services” and append a line to it.
# vi /etc/services ... ... nrpe 5666/tcp #NRP
The NRPE is installed and will listens for any request coming from Nagios server to execute the plugins. But for that you need to adjust few settings in the NRPE configuration file.
To allow remote host to execute the NRPE plugins on behalf of Nagios server, edit the file “/usr/local/nagios/etc/nrpe.cfg” and find the line that contains “allowed_hosts” and “server_address” and uncomment/modify the line like below.
The “allowed_host” line should contain the IP address of Nagios Server which is 10.0.2.194 in the following configuration.
# vi /usr/local/nagios/etc/nrpe.cfg ... ... allowed_hosts=127.0.0.1,::1,10.0.2.194 server_address=0.0.0.0 ... ...
Now start and enable NRPE:
# systemctl enable nrpe # systemctl start nrpe
Use “netstat” command to find if NRPE is running:
# netstat -pltn | grep nrpe
Open the port number 5666 of “NRPE” daemon in the firewall settings so as to traffic can pass through it.
# firewall-cmd --zone=public --add-port=5666/tcp --permanent # firewall-cmd --reload
To verify if the NRPE daemon in the remote host is functioning as expected, just execute the following “check_nrpe” command from the terminal of **Nagios Server** where “10.0.2.12” is the IP address of remote host.
The version number in the output of the following command signifies that NRPE is up and running as per our expectation.
# /usr/local/nagios/libexec/check_nrpe -H 10.0.2.12 NRPE v3.2.1
If you see any error instead, verify that the firewall allows traffic on TCP port 5666 or you need to consult the log file of NRPE.
4. Configure NRPE in Remote Host
The NRPE daemon is running in the remote host and can be reached from Nagios server but we have not added any NRPE commands in the remote host for monitoring, although there are lot of built in commands available in “/usr/local/nagios/etc/nrpe.cfg” to start from.
But for better understanding, define a custom command in the same file that will be executed at custom intervals and the output of the command will be sent to the Nagios server that you can view in the Nagios web interface.
Let’s say we want to monitor the disk usages of remote host. To do that, add the following NRPE command in the file “/usr/local/nagios/etc/nrpe.cfg”.
# vi /usr/local/nagios/etc/nrpe.cfg ... ... command[check_xvda1]=/usr/local/nagios/libexec/check_disk -w 25% -c 15% -p /dev/xvda1 ... ...
The above line describes the name of the command as “check_xvda1” and uses the plugin “check_disk”.
Further the plugin will produce a warning if the available disk space is 25% or lower and critical when the disk space is 15% or lower.
Be sure to replace “/dev/xvda1” with your own device name after verifying it through “fdisk” command. At the end, restart `NRPE` daemon to reload the new configuration.
# systemctl restart nrpe
5. Configure Nagios server to collect data from remote host
The last step of fetching remote host’s performance metrics into the Nagios server is to configure it properly.
To start with, you need to create/enable the folder from which Nagios server reads the configuration for remote host.
Edit the file “/usr/local/nagios/etc/nagios.cfg” and add a line that defines “cfg_dir” for remote hosts.
It is possible to have many configuration directory for a chain of remote host like printers, switches and many more.
# vi /usr/local/nagios/etc/nagios.cfg ... ... cfg_dir=/usr/local/nagios/etc/servers ... ...
Next create and edit configuration file for the remote host and define the commands and services those will be executed through NRPE plugins.
# mkdir /usr/local/nagios/etc/servers # vi /usr/local/nagios/etc/servers/remote_server.cfg define host { use linux-server host_name remotehost1 alias Database Server address 10.0.2.12 max_check_attempts 5 check_period 24x7 notification_interval 30 notification_period 24x7 } ... ...
To proceed with configuring services for remote host, add the following snippets of code to find the CPU load and free space of remote host.
The command “check_load” does not need to be defined just like we have created “check_xvda1” earlier since it is shipped along with NRPE.
... ... define service { use generic-service host_name remotehost1 service_description CPU load in Remote host check_command check_nrpe!check_load } define service { use generic-service host_name remotehost1 service_description Free space in remote host check_command check_nrpe!check_xvda1 } ... ...
Restart the Nagios server and head over to Nagios web interface. Click the host and services link from the left sidebar.
The newly added host and services will be available for monitoring.
# systemctl restart nagios
Conclusions
That’s all about how to install Nagios Core, Nagios plugin in CentOS 7 and how to add a remote host for monitoring using NRPE and access the Nagios dashboard through its web interface.
You can now add more hosts and services to build a stack of monitored systems and services to know what is happening in your infrastructure and this will definitely help you to manage any unforeseen issues before they become critical.