;

How to install and Configure Nagios on CentOS 7

Try it in our public cloud & Get $5 Credit
CLAIM NOW

Nagios is a very popular server monitoring software application that’s been in development since 1999 and is still regularly updated. The server administrator who uses Nagios gains access to a wide variety of features such as SSH-enabled remote monitoring, network and host traffic monitoring, and a versatile plugin ecosystem. Nagios is free and open source under the GNU General Public License, version 2.

Getting started

Before you begin, make sure you have the following prepared:
• 1 Remote server (Cloud Server or Dedicated Server) running CentOS 7.
• All commands should be run as the root user

Tutorial

The first step is to make sure that Selinux is disabled.

setenforce 0
sed -i 's/enforcing/disabled/' /etc/sysconfig/selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config

Once you’ve done that, you’ll need to install some basic prerequisite packages for Nagios.

yum install gcc glibc glibc-common gd gd-devel net-snmp openssl-devel wget unzip nano -y

Let’s install the Apache webserver and PHP.

yum install httpd php php-cli -y

We’ll be adding a Nagios user to our system, along with nagcmd, so that Nagios and Apache can be integrated together.

useradd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd apache

Now, we’ll proceed with the installation of Nagios proper. This requires that you download Nagios and the Nagios plugins from the official site.

cd /tmp
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.2.1.tar.gz
wget https://nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gz

Unzip the Nagios archive you downloaded and use the script inside to install the program.

tar -zxvf nagios-4.2.1.tar.gz
cd nagios-4.2.1/
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf

Open the contact file in your favorite text editor and change the enclosed email address to your own.

nano /usr/local/nagios/etc/objects/contacts.cfg

[...]
# 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 nagios@localhost ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ****** } [...]

Now, set the password that you'll use to access Nagios.

htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

After that, you can unzip and install the Nagios plugins from the second archive you downloaded.

cd /tmp
tar -zxvf nagios-plugins-2.1.2.tar.gz
cd nagios-plugins-2.1.2/
./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl
make
make install

Just to be safe, check that your config file has no errors.

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

Nagios Core 4.2.1
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 09-06-2016
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

The final step is to start up the Nagios and Apache daemons.

service httpd start
service nagios start

In order to access your Nagios interface, you can use the convenient web-based interface. This should be located at this URL:

http://your_ip/nagios/

Here's the login information that you can use the first time:

Login : nagiosadmin
Password : The password you set previously

Feel free to change the login name to something more obfuscated.

Conclusion

Now that you know how to install Nagios on your system, you can use it as a quick and simple, yet full-featured, monitoring tool for any system under your purview. If you found this article helpful, feel free to share it with your friends and let us know in the comments below!