Nagios Logo Nagios is a tool for system monitoring. Nagios constantly checks if other machines are working properly. It also verifies that various services on those machines are working fine. In addition, Nagios accepts other processes or machines reporting their status, for example, a web server can directly report if it is not overloaded to Nagios. The main purpose of system monitoring is to detect as soon as possible any system that is not working properly so that users of that system will not report the issue to you first.

Today I will cover installing Nagios on FreeBSD, with its various dependencies such as PHP. Also, I will look at htpasswd authentication and finally configuring email notifications.

Nagios & PHP Installation

PHP is a dependency of Nagios and installed automatically, however, we will also need to install mod_php72 to use the Nagios Web Interface.

# pkg install nagios4 mod_php72
Message from php72-xml-7.2.25:

--
This file has been added to automatically load the installed extension:
/usr/local/etc/php/ext-20-xml.ini

=====
Message from php72-filter-7.2.25:

--
This file has been added to automatically load the installed extension:
/usr/local/etc/php/ext-20-filter.ini

=====
Message from nagios4-4.4.5,1:

--
Enable Nagios in /etc/rc.conf with the following line:

   nagios_enable="YES"

 Configuration templates are available in /usr/local/etc/nagios as
 *.cfg-sample files.  Copy them to *.cfg files where required and
 edit to suit your needs.

 If you don't already have a web server running, you will need to
 install and configure one to finish off your Nagios installation.
 When used with Apache, the following should be sufficient to publish
 the web component of Nagios (modify the allow list to suit):

   <Directory /usr/local/www/nagios>
     Require ip 127.0.0.1
     php_flag engine on
     php_admin_value open_basedir /usr/local/www/nagios/:/var/spool/nagios/
   </Directory>

   <Directory /usr/local/www/nagios/cgi-bin>
     Options ExecCGI
   </Directory>

   ScriptAlias /nagios/cgi-bin/ /usr/local/www/nagios/cgi-bin/
   Alias /nagios/ /usr/local/www/nagios/

=====
Message from mod_php72-7.2.25:

--
Make sure index.php is part of your DirectoryIndex.

You should add the following to your Apache configuration file:

<FilesMatch "\.php$">
    SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
    SetHandler application/x-httpd-php-source
</FilesMatch>

If you are building PHP-based ports in poudriere(8) or Synth with ZTS enabled,
add WITH_MPM=event to /etc/make.conf to prevent build failures.

Configure PHP for Apache

There is quite a lot of detail here so follow closely. Our first objective is to make sure we have Apache & PHP working together before we move onto Nagios configuration.

Apache 2.4 Configuration

Make sure your /usr/local/etc/apache24/httpd.conf contains the following lines.

# vi /usr/local/etc/apache24/httpd.conf

<IfModule !mpm_prefork_module>
        LoadModule cgid_module libexec/apache24/mod_cgid.so
</IfModule>

<IfModule mpm_prefork_module>
        LoadModule cgi_module libexec/apache24/mod_cgi.so
</IfModule>

<IfModule dir_module>
    DirectoryIndex index.php index.html index.htm AddType application/x-httpd-php .phpAddType application/x-httpd-php-source .phps
</IfModule>

<FilesMatch ".php$">
    SetHandler application/x-httpd-php
</FilesMatch>

<FilesMatch ".phps$">
    SetHandler application/x-httpd-php-source
</FilesMatch>

Note: The CGI directives are not needed for PHP but will be later used for Nagios.

PHP 7.2 Configuration

Create php.ini file and setup a test phpinfo page to display. You will want to remove the phpinfo.php file when you have confirmed PHP is working.

# cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
# cat /usr/local/www/apache24/data/phpinfo.php 
<?php 
phpinfo(); 
?>

Restart Apache

# service apache24 restart

View PHP Information

Verify that PHP is working by visiting http://yourhostname/phpinfo.php. You should see a formatted output of your server and its PHP configuration. If you do, then PHP is installed and working properly.

Once this is verified, you should remove the phpinfo.php files so as not to show the outside world sensitive information about your installation.

Nagios Configuration

Let us start by copying all the needed configuration files in to place and setting their ownership. After that we can start Nagios.

# cp /usr/local/etc/nagios/nagios.cfg-sample /usr/local/etc/nagios/nagios.cfg
# cp /usr/local/etc/nagios/resource.cfg-sample /usr/local/etc/nagios/resource.cfg
# cp /usr/local/etc/nagios/cgi.cfg-sample /usr/local/etc/nagios/cgi.cfg
# cp /usr/local/etc/nagios/objects/commands.cfg-sample /usr/local/etc/nagios/objects/commands.cfg
# cp /usr/local/etc/nagios/objects/contacts.cfg-sample  /usr/local/etc/nagios/objects/contacts.cfg
# cp /usr/local/etc/nagios/objects/timeperiods.cfg-sample  /usr/local/etc/nagios/objects/timeperiods.cfg
# cp /usr/local/etc/nagios/objects/templates.cfg-sample /usr/local/etc/nagios/objects/templates.cfg
# cp /usr/local/etc/nagios/objects/localhost.cfg-sample  /usr/local/etc/nagios/objects/localhost.cfg
# chown root:nagios /usr/local/etc/nagios/nagios.cfg
# chown root:nagios /usr/local/etc/nagios/resource.cfg
# chown root:nagios /usr/local/etc/nagios/cgi.cfg
# service nagios start

Create an Apache Configuration File for Nagios

The below is sufficient to run the Nagios Web Interface with htpasswd authentication.

vi /usr/local/etc/apache24/Includes/nagios.conf

   <Directory "/usr/local/www/nagios/">
     AuthName "Nagios Access"
     AuthType Basic
     AuthUserFile /usr/local/etc/nagios/.htpasswd.users
     php_flag engine on
     php_admin_value open_basedir /usr/local/www/nagios/:/var/spool/nagios/
     Require valid-user
   </Directory>

   <Directory "/usr/local/www/nagios/cgi-bin/">
     Options ExecCGI
     AddType application/x-httpd-php .php
     AuthName "Nagios Access"
     AuthType Basic
     AuthUserFile /usr/local/etc/nagios/.htpasswd.users
     Require valid-user
   </Directory>

   ScriptAlias /nagios/cgi-bin/ "/usr/local/www/nagios/cgi-bin/"

   Alias /nagios/ "/usr/local/www/nagios/"

Create htpasswd Nagiosadmin User for Nagios Web Interface

This enables us to access the Web Interface that we have password protected in the nagios.conf file.

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

All systems Go!

It is just a matter of restarting Apache and we should be able visit http://your-hostname/nagios/ to log into the Web Interface.

service apache24 restart

Enable Email Notifications.

One setting that is very important to configure is that of email notifications. It is also very simple, providing that your host has a Fully Qualified Domain Name. Just insert your email address into where the /usr/local/etc/nagios/objects/contacts.cfg file suggests.

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 ******
}

If you host does not have a FQDN, you will have to alter the return address on the mail command under the /usr/local/etc/nagios/objects/commands.cfg file. /var/log/maillog is your friend when you are trying to determine mail problems that you encounter.

Note: Remember to always restart Nagios when you change any of its configuration files.

Wrapping Up

Next I will cover the Nagios Service Check Acceptor so we can monitor custom remote services that will report to our Nagios server. You can find that post here.