Introduction
Nagios (now known as Nagios Core) is a free and open source computer-software application that monitors systems, networks and infrastructure. Nagios offers monitoring and alerting services for servers, switches, applications and services. It alerts users when things go wrong and alerts them a second time when the problem has been resolved.
Prerequisites
To install Nagios on your server, make sure you are logged into your server with a sudo user.
This guide is based on SELinux being disabled or in permissive mode. To disable SELinux use the below command:
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
Step 1 - Install Required Dependencies
We need to install Apache, PHP and some libraries like gcc, glibc, glibc-common and GD libraries and its development libraries before installing Nagios with source. To do so, we can use the default package installer yum.
First, install the required packages:
yum install -y httpd httpd-tools php gcc glibc glibc-common gd gd-devel make net-snmp openssl-devel xinetd unzip
Step 2 - Create Nagios User and Group
We must create a user and group that will run the Nagios process. Create a nagios user and nagcmd group with these commands:
useradd nagios
groupadd nagcmd
Then add both the nagios user and the apache user to the nagcmd group.
Then add the user to the group with these commands:
usermod -G nagcmd nagios
usermod -G nagcmd apache
Step 3 - Download Nagios Core and Nagios Plugin
Download the latest Nagios Core 4.4.3 and Nagios plugins 2.2.1 packages. Go to the Nagios downloads page, copy the link address for the latest stable release so you can download it to your Nagios server.
cd /tmp
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.3.tar.gz
wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
Extract the Nagios Core and Plugins archive with this command:
tar xvf nagios-4.4.3.tar.gz
tar xvf nagios-plugins-2.2.1.tar.gz
Step 4 - Configure Nagios Core
Now we will configure Nagios Core. To do so, we need to go to the Nagios directory and run the configure file. If everything goes fine, it will show the output in the end as sample output. Please see below:
cd nagios-4.4.3
./configure --with-command-group=nagcmd
Sample output:
Creating sample config files in sample-config/ ...
*** Configuration summary for nagios 4.4.3 2019-01-15 ***:
General Options:
-------------------------
Nagios executable: nagios
Nagios user/group: nagios,nagios
Command user/group: nagios,nagcmd
Event Broker: yes
Install ${prefix}: /usr/local/nagios
Install ${includedir}: /usr/local/nagios/include/nagios
Lock file: /run/nagios.lock
Check result directory: /usr/local/nagios/var/spool/checkresults
Init directory: /lib/systemd/system
Apache conf.d directory: /etc/httpd/conf.d
Mail program: /bin/mail
Host OS: linux-gnu
IOBroker Method: epoll
Web Interface Options:
------------------------
HTML URL: http://localhost/nagios/
CGI URL: http://localhost/nagios/cgi-bin/
Traceroute (used by WAP):
Review the options above for accuracy. If they look okay,
type 'make all' to compile the main program and CGIs.
Now compile Nagios with this command:
make all
Now we can run these make commands to install Nagios, init scripts, and sample configuration files:
make install
make install-init
make install-commandmode
make install-config
make install-webconf
Step 5 - Customize Nagios Configuration
Open the "contacts.cfg" file with your choice of editor and set the email address associated with the nagiosadmin contact to receive email alerts.
vi /usr/local/nagios/etc/objects/contacts.cfg
Sample Output:
###############################################################################
#
# CONTACTS
#
###############################################################################
# Just one contact defined by default - the Nagios admin (that's you)
# This contact definition inherits a lot of default values from the
# 'generic-contact' template which is defined elsewhere.
define contact {
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email admin@example.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
Step 6 - Configure Web Interface
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
Enter a password at the prompt. Remember this login, as you will need it to access the Nagios web interface.
Restart Apache to make the new settings take effect.
systemctl start httpd.service
Step 7 - Compile and Install Nagios Plugins
cd /tmp
cd nagios-plugins-2.2.1
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
Step 8 - Verify Nagios Configuration Files
Verify the Nagios configuration file using the following command:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Sample Output:
Nagios Core 4.4.3
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2019-01-15
License: GPL
Website: https://www.nagios.org
Reading configuration data...
Read main config file okay...
Read object config files okay...
Running pre-flight check on configuration data...
Checking objects...
Checked 8 services.
Checked 1 hosts.
Checked 1 host groups.
Checked 0 service groups.
Checked 1 contacts.
Checked 1 contact groups.
Checked 24 commands.
Checked 5 time periods.
Checked 0 host escalations.
Checked 0 service escalations.
Checking for circular paths...
Checked 1 hosts
Checked 0 service dependencies
Checked 0 host dependencies
Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
Step 9 - Add Nagios Services to System Startup
To make Nagios work across reboots, we need to add nagios and httpd with chkconfig and systemctl command.
systemctl enable nagios
systemctl enable httpd
Restart Nagios to make the new settings take effect.
systemctl restart nagios.service
Step 10 - Access Nagios Web Interface
Open your web browser, and go to your Nagios server:
http://nagios_server_public_ip/nagios
Because we configured Apache to use htpasswd, you must enter the login credentials that you created earlier. We used nagiosadmin as the username.
Conclusion
Finally, you have successfully installed and configured Nagios core service in your server.