Nagios with NginX in Ubuntu

In this article I will show you how I installed the latest version of the Nagios from sources, and how to serve it with NginX. Nagios is an open-source monitoring application with a very strong community and a huge set of plugins that can help you monitor every aspect of your servers. At the time of writing this article the latest version is 3.4.1. The platform used is Ubuntu Precise 12.04 LTS, but except dependencies, it should be the same for older distributions. It doesn't hurt to check them first.

We will start by installing the dependencies:

apt-get install libperl-dev libpng12-dev libgd2-xpm-dev

And then create the user and group under which it will run:

/usr/sbin/useradd -M -s /bin/false nagios

Go ahead and create a directory in which we can download and compile the packages:

mkdir nagios_sources && cd nagios_sources

wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.4.1.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz

tar xzf nagios-3.4.1.tar.gz
tar xzf nagios-plugins-1.4.15.tar.gz

As you can see we downloaded both the core packages and the plugins. Next we will install the main program. I will use as the destination directory "/opt/":

cd nagios-3.2.3

./configure --prefix /opt/nagios \
--sysconfdir=/etc/nagios \
--with-nagios-user=nagios \
--with-nagios-group=nagios \
--with-command-user=nagios \
--with-command-group=nagios

make all

make install
make install-init
make install-config
make install-commandmode

Next step is to create a password file:

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

Next we will install the plugins:

cd ../nagios-plugins-1.4.15

./configure --prefix /opt/nagios \
--with-nagios-user=nagios \
--with-nagios-group=nagios

make && make install

Before we finish the installation you need to associate your email address with the nagiosadmin contact in the /etc/nagios/objects/contacts.cfg file.

That's it! The only thing left is to create the start-up links for Ubuntu, to test the configuration and if everything works, start the daemon:

update-rc.d -f nagios defaults
/opt/nagios/bin/nagios -v /etc/nagios/nagios.cfg
/etc/init.d/nagios start

The /etc/nagios/objects/ folder contains all the definitions pertaining your monitored machines and you can change them any way you like. Everything is very well commented so it's very easy to modify it. But before restarting the server check first with the command above if everything works as expected.

Now, for the nginx configuration, first of all apart from a functional php environment you must also have the lightweight fcgiwrap package from Ubuntu. You can install it with:

apt-get install spawn-fcgi fcgiwrap

And this is an example of the virtual host which allows normal operation under /var/www as well as aliases for the /nagios/ folder (modify it according to your configuration):

server {
        server_name example.com;

        access_log  /var/log/nginx/example.com.access.log;
        error_log   /var/log/nginx/example.com.error.log;
 
  auth_basic            "Restricted Nagios Area!";
  auth_basic_user_file  /etc/nagios/htpasswd.users;    
   
        root    /var/www;
        index   index.php index.html;

   location / {
    try_files $uri $uri/ index.php;
  }
 
  location /nagios {
    alias /opt/nagios/share/;
  }

  location ~ ^/nagios/(.*\.php)$ {
    alias /opt/nagios/share/$1;
    include /etc/nginx/fastcgi.conf;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
  }
 
  location ~ \.cgi$ {
    root /opt/nagios/sbin/;
    rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
    fastcgi_param AUTH_USER $remote_user;
    fastcgi_param REMOTE_USER $remote_user;    
    include /etc/nginx/fastcgi.conf;
    fastcgi_pass unix:/var/run/fcgiwrap.socket;
  }  
 
  location ~ \.php$ {
    include /etc/nginx/fastcgi.conf;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
  }
}

You can now restart nginx and fcgiwrap and access your nagios installation by visiting example.com/nagios.


If you found this useful and you want to contribute, please consider sending a donation (click the button below).