This article describes the installation of Nagios 4 and its basic configuration so to monitor host resources via a web interface.
As you know, Nagios is a one-stop monitoring service for entire IT infrastructures (applications, services, operating systems, network protocols, system metrics and infrastructure components). It is configured to send alerts when critical services fail and recover. Nagios is useful for keeping an inventory of your servers and making sure that the configured critical services are up and running.
Before you proceed with Nagios installation, you must have superuser privileges on the CentOS 7 server that will run Nagios and you need to install Apache and PHP.
If you’ve not installed Apache and PHP, please do the steps below,
Install Apache by
yum install httpd
Once installed, start Apache:
systemctl start httpd.service
You need to enable Apache to start on boot. Use the following command to do so,
systemctl enable httpd.service
Install PHP by,
yum install php php-mysql
In order to work with PHP, we need to restart the Apache web server by using the below command.
# systemctl restart httpd.service
Now that we have the prerequisites sorted out, come let’s move on getting Nagios 4 installed.
Install Nagios 4
Install Build Dependencies
Initially we need to install the required packages:
sudo yum install gcc glibc glibc-common gd gd-devel make net-snmp openssl-devel xinetd<unzip
Then we need to craete and a user and a group that will run nagios process. Create a “nagios” user and “nagcmd” group and add the user to the group with these commands:
#sudo useradd nagios
#sudo groupadd nagcmd
#sudo usermod -a -G nagcmd nagios
Let’s install Nagios now
#cd ~ && curl -L -O https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.1.1.tar.gz
Extract the downloaded Nagios archive with this command:
#tar xvf nagios-*.tar.gz
Then change to the extracted directory:
Before building Nagios, configure it with this command,
# ./configure –with-command-group=nagcmd
Now we can compile Nagios with this command:
# make all
Now, run these make commands to install Nagios, init scripts, and sample configuration files:
#sudo make install
#sudo make install-commandmode
#sudo make install-init
#sudo make install-config
#sudo make install-webconf
To issue external commands via the web interface to Nagios server, we must add the web server user apache to the nagcmd group, run below commands;
#sudo usermod -G nagcmd apache
Install Nagios Plugins
Now proceed with installing the latest release of Nagios Plugins, you can find latest here: Nagios Plugins Download. Copy the link address for the latest version and download it to your Nagios server.
We are using Nagios Plugins 2.1.1
#cd ~ && curl -L -O http://nagios-plugins.org/download/nagios-plugins-2.1.1.tar
Extract Nagios Plugins archive with this command and change to the extracted directory:
tar xvf nagios-plugins-*.tar.gz
Configure Nagios Plugins before its build, use this command:
#./configure –with-nagios-user=nagios –with-nagios-group=nagios –with-openssl
Now compile Nagios Plugins and install it,
#sudo make install
Download the source code for the latest stable release of NRPE at the NRPE downloads page, I’m now using the release 2.15.
#cd ~ && curl -L -O http://downloads.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz
Extract the NRPE archive and then change to the extracted directory.
#tar xvf nrpe-*.tar.gz && cd nrpe-*
Configure NRPE with these commands:
# ./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
Now proceed with the build, install NRPE and its xinetd startup script with these commands;
#sudo make install
#sudo make install-xinetd
#sudo make install-daemon-config
Open the xinetd startup script in a vi editor,
#sudo vi /etc/xinetd.d/nrpe
Modify the “only_from” line by adding the Nagios server IP to the end,
only_from = 127.0.0.1 184.108.40.206
Save and exit. Restart the xinetd service to start NRPE daemon:
#sudo service xinetd restart
Now that Nagios 4 is installed and we need to configure it.
Now let’s begin the initial Nagios configuration, open the main Nagios configuration file in your text editor,
# sudo vi /usr/local/nagios/etc/nagios.cfg
Now find an uncomment the below line by deleting the #,
#cfg_dir=/usr/local/nagios/etc/servers –>> remove # symbol infront of them
Save and exit. Now create the directory so that you can store the configuration file for each server that you wish to monitor.
#sudo mkdir /usr/local/nagios/etc/servers
Now configure Nagios Contacts, open the Nagios contacts configuration in your text editor.
#sudo vi /usr/local/nagios/etc/objects/contacts.cfg
Find the email directive and replace its default value (default: nagios@localhost) with your own email address:
email nagios@localhost ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
Save and exit.
Now, configure ‘check_nrpe’ command, let’s add a new command to our Nagios configuration so this allows you to use the ‘check_nrpe’ command in your Nagios service definitions.
#sudo vi /usr/local/nagios/etc/objects/commands.cfg
Add the following to the end of the file:
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
Save and exit.
Once completed, proceed with configuring Apache, use htpasswd to create an admin user, called “nagiosadmin”, so can access the Nagios web interface:
# sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
Enter a strong password at the prompt(Remember this login, as you will need to access the Nagios web interface).
Please Note: If you create a user that is not named “nagiosadmin”, you will need to edit /usr/local/nagios/etc/cgi.cfg and replace all the “nagiosadmin” references to that user you created.
Now Nagios is ready and let’s get its started Apache,
#sudo systemctl daemon-reload
#sudo systemctl start nagios.service
#sudo systemctl restart httpd.service
In order to enable Nagios to start on server boot, run this command,
#sudo chkconfig nagios on
Now we can access Nagios Web Interface at
http://nagios_server_public_ip/nagios (substitute the IP address highlighted with your server IP)