Lampros - Weird Bricks

Installing Nagios on FreeBSD 9.1 - Basic Installation

07 November, 2013 | Nagios

Nagios should need no introduction :) It's the most common open source solution for monitoring servers and other hosts.

I'll describe here the steps I took to install Nagios.

Disclaimer- This post heavily relied on 'How to install and configure Nagios in FreeBSD' by I found some steps to not be necessary (the MySQL installation) and others not as clear. In this post I'll go a step further and show how to setup configuration for hosts.

PHP must be installed from source - this is what happens if you don't and then try to install Nagios from ports:

nagios-3.5.1 depends on file: /usr/local/include/php/main/php.h - not found

This is what happens if you try to install PHP from a package - i.e. you have the following:

pkg_info | grep -i php 
php5-5.4.7 PHP Scripting Language

In other words, although PHP is installed, the Apache PHP module is not. To install the Apache PHP module you do need to install PHP and PHP extensions from source.

To check if you have the module installed try apachectl -M - like this:

apachectl -M | grep -i php
Syntax OK

Yes, the above output should show a PHP module. In this case there is none.

With that out of the way, let's go ahead and install it :)

1. Install Packages (will save us tons of time)

pkg_add -r perl python27 cmake tcl85 wget curl unzip pcre m4 gmake libxml2 libcheck libxslt xcb-proto freetype2 gd libXpm t1lib help2man gmake libXaw bigreqsproto xcmiscproto xtrans inputproto xf86bigfontproto libIce xorg-macros libtool libltdl apache22 

The above should take about 7 minutes

2. Install PHP5 from ports

cd /usr/ports/lang/php5;make config-recursive

(make sure to add the APACHE module - once you select that, it will also install apache22 as a dependency)

Do make config-recursive again to make sure that all port configurations are complete:

make config-recursive 

Now compile from source:

time make install clean

(about 7 minutes)

3. Install PHP5-extensions from ports
* make sure to add GD
* make sure to remove PDO_SQLITE, SQLITE3

cd /usr/ports/lang/php5-extensions/;make config-recursive

Do make config-recursive again to make sure that all port configurations are complete:

make config-recursive 

Now compile from source:

time make install clean

(took about 8 minutes)

4. Install Nagios

cd /usr/ports/net-mgmt/nagios;make config-recursive

No need to run make-config recursive twice, there's very few dependencies:

make install clean

(took under 5 minutes)

Make nagios start on boot:

echo 'nagios_enable="YES"' >> /etc/rc.conf

5. Configure Nagios

Basically we're using the sample files by copying them to the filenames that Nagios expects to find:

cd /usr/local/etc/nagios/ 
cp cgi.cfg-sample cgi.cfg 
cp nagios.cfg-sample nagios.cfg 
cp resource.cfg-sample resource.cfg

cd /usr/local/etc/nagios/objects/ 
cp commands.cfg-sample commands.cfg 
cp contacts.cfg-sample contacts.cfg 
cp localhost.cfg-sample localhost.cfg 
cp printer.cfg-sample printer.cfg 
cp switch.cfg-sample switch.cfg 
cp templates.cfg-sample templates.cfg 
cp timeperiods.cfg-sample timeperiods.cfg

Start Nagios with:

/usr/local/etc/rc.d/nagios start

You should get output like:

Performing sanity check of nagios configuration: 
OK Starting nagios.

6. Apache configuration

Make it start on boot:

echo 'apache22_enable="YES"' >> /etc/rc.conf 
echo "apache22_http_accept_enable="YES"" >> /etc/rc.conf

Then edit the file: /usr/local/etc/apache22/httpd.conf and comment out the following line (should be line 85), so that it looks like this:

#LoadModule unique_id_module libexec/apache22/

Also comment out lines 167-171 so that they look like this:

 #<Directory /> # AllowOverride None # Order deny,allow # Deny from all #</Directory> 

and also add the following at the bottom of the file so that Apache knows what files to process with the PHP module:
[code] DirectoryIndex index.php index.html AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps [/code]
Now check if Apache sees the PHP5 module with the command: apachectl -M | grep php
You should see output like this:

 apachectl -M | grep php php5_module (shared) Syntax OK

These are the PHP extensions I got in the output of the command: php -m
[code]php -m [PHP Modules] Core ctype date dom ereg filter gd hash iconv json libxml mhash mysqlnd pcre PDO Phar posix Reflection session SimpleXML SPL standard tokenizer xml xmlreader xmlwriter [Zend Modules][/code]

7. Nagios VirtualHost

Use your favorite editor and create the following VirtualHost in the file:
[code] AuthName "Nagios Access" AuthType Basic AuthUserFile /usr/local/etc/nagios/htpasswd.users Require valid-user php_flag engine on php_admin_value open_basedir /usr/local/www/nagios/:/var/spool/nagios/ Options ExecCGI ScriptAlias /nagios/cgi-bin/ /usr/local/www/nagios/cgi-bin/ Alias /nagios /usr/local/www/nagios/[/code]

Notes about the above:
1. You need to have some kind of authorization for the Apache VirtualHost. If you don't, Nagios will throw warnings like these:

2. Regarding the line:

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

Again the above is a Nagios expectation, so I recommend you leave that there.

To add users that are authorized to access Nagios use the htpasswd command - the manual can be found here.

To add the first user try the following:

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

In the above replace nagiosadmin with the username you want
replace somepassword with the password for the user

Restart Apache with:

/usr/local/etc/rc.d/apache22 restart

8. Test that it all works

To test if Nagios works, simply fire up a browser and go to the IP address/hostname of your machine/nagios - like this:

You should get a prompt for a username and password that looks like this:

enter the username and password that we've set above - you should get the nagios main screen:

Click on 'Hosts' on the left and you should see the nagios report for the Nagios server itself: