Introduction

In our previous two articles we’ve explained how to install Nagios core on a Centos 7 system and how to install Nagios plugin and the Nagios Remote Plugin Executor. In this article we will explain how to configure Nagios so that we can have the web interface up and running. Note that this needs to be done only once on the Nagios server. You may make amendments as deemed necessary.

Update the Nagios main configuration file:
The nagios.cfg file is the heart of your nagios core configuration. The location of this file may vary depending on where you installed nagios. The general location of the file is /usr/local/nagios/etc/nagios.cfg This file contains information about the location of other configuration files and template files which nagios will read in order to determine which assets are to be monitored and which services on those assets are to be monitored. We can define the location to these configuration files using the cfg_file directive. By default Nagios already has most of the configuration files set up and ready to use as shown below:

[ssuri@linuxnix:/usr/local/nagios/etc] $ grep -E '^cfg_file' nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
cfg_file=/nagiosap/objects/hostgroups/linux.cfg
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg

We will talk about the purpose of these configuration files in greater detail in later articles. You could also define a directory location and tell nagios to read all files ending with .cfg extension within that directory.

Add an email address in contacts.cfg file
Now we will specify an email address to which nagios can forward alert related emails. This information is defined in the /usr/local/nagios/etc/objects/contacts.cfg file by default under the email directive.

[ssuri@linuxnix:~] $ grep email /usr/local/nagios/etc/objects/contacts.cfg
email sahil.suri@example.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******

We will talk about configuring email addresses for multiple users and groups in detail in a separate article.

Add configuration for check_nrpe command in commands.cfg file
The NRPE plugin that we installed earlier provides a plugin or command named check_nrpe. We use this command on the nagios core server to run active checks on remote client machines. But in order to use this command it must be defined in the commands.cfg file located at the path /usr/local/nagios/etc/objects/. Given below is the required command definition that needs to be added.

define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

Apache configuration
Use the htpasswd command to create an admin user, called “nagiosadmin”, that will be used to access the Nagios web interface.

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

If you create a user that is not named “nagiosadmin”, you will need to edit /usr/local/nagios/etc/cgi.cfg and change all the “nagiosadmin” references to the user you created.

Now we need to edit the nagios configuration file for apache located at /etc/httpd/conf.d/nagios.conf.
In this file comment the following lines:

Order allow,deny
Allow from all

Replace these lines with the following line:

Require all granted

If you are working with an older version of apache then add the following lines instead of the above:

Order allow,deny
Allow from all

With this our apache configuration is complete. Now we need to restart apache with the following command.

systemctl restart httpd

Our nagios configuration is also complete with this now. We need to restart the nagios service in order to allow our changes to take effect.

systemctl restart nagios.service

Ensure that nagios is running

[ssuri@linuxnix:~] $ sudo systemctl status nagios
* nagios.service - Nagios Core 4.4.2
Loaded: loaded (/usr/lib/systemd/system/nagios.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2018-10-13 17:32:06 UTC; 1 weeks 1 days ago
Docs: https://www.nagios.org/documentation
Process: 15354 ExecStopPost=/usr/bin/rm -f /usr/local/nagios/var/rw/nagios.cmd (code=exited, status=0/SUCCESS)
Process: 15352 ExecStop=/usr/bin/kill -s TERM ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 3225 ExecReload=/usr/bin/kill -s HUP ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 15359 ExecStart=/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg (code=exited, status=0/SUCCESS)
Process: 15357 ExecStartPre=/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg (code=exited, status=0/SUCCESS)
Main PID: 15361 (nagios)
Tasks: 6
Memory: 4.0M
CGroup: /system.slice/nagios.service
|-15361 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
|-15362 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
|-15363 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
|-15364 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
|-15365 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
`-15366 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg

Oct 22 04:32:06 linuxnix.example.org nagios[15361]: Auto-save of retention data completed successfully.
Oct 22 05:32:06 linuxnix.example.org nagios[15361]: Auto-save of retention data completed successfully.
Oct 22 06:32:06 linuxnix.example.org nagios[15361]: Auto-save of retention data completed successfully.
Oct 22 07:32:06 linuxnix.example.org nagios[15361]: Auto-save of retention data completed successfully.
Oct 22 08:32:06 linuxnix.example.org nagios[15361]: Auto-save of retention data completed successfully.
Oct 22 09:32:06 linuxnix.example.org nagios[15361]: Auto-save of retention data completed successfully.
Oct 22 10:32:06 linuxnix.example.org nagios[15361]: Auto-save of retention data completed successfully.
Oct 22 11:32:06 linuxnix.example.org nagios[15361]: Auto-save of retention data completed successfully.
Oct 22 12:32:06 linuxnix.example.org nagios[15361]: Auto-save of retention data completed successfully.
Oct 22 13:32:06 linuxnix.example.org nagios[15361]: Auto-save of retention data completed successfully.
[ssuri@linuxnix:~] $

Access the web interface
Once we’ve ensured that nagios is running on the server we can access the nagios web interface by pointing our browser to the URL http://<nagios_server_ip>/nagios. Because we configured Apache to use htpasswd, we need to enter the login credentials that we created earlier. We used “nagiosadmin” as the username.

Conclusion

This completes the nagios server setup. In our next article we will demonstrate how to configure a nagios client machine to be monitored by our nagios core server.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.