Nagios is an excellent open source monitoring solution that can be configured to monitor pretty much anything. In this article, I’ll describe how to install Nagios under Nginx on Ubuntu 14.04 LTS.
First of all, check that the system is fully up to date:
|
1 2 |
# apt-get update # apt-get upgrade |
Next, install the build-essential package so that we can build Nagios and its plugins from source:
|
1 |
# apt-get install build-essential |
Install Nginx, and verify that it has started:
|
1 2 |
# apt-get install nginx # service nginx status |
Install libgd2-xpm-dev, php5-fpm, spawn-fcgi and fcgiwrap:
|
1 2 3 |
# apt-get install libgd2-xpm-dev # apt-get install php5-fpm # apt-get install spawn-fcgi fcgiwrap |
Next, create a nagios user:
|
1 |
# adduser --system --no-create-home --disabled-login --group nagios |
Issue the following commands to create a nagcmd group, and add it as a secondary group to both the nagios and www-data users:
|
1 2 3 |
# groupadd nagcmd # usermod -G nagcmd nagios # usermod -a -G nagcmd www-data |
Download the latest Nagios core distribution from http://www.nagios.org/download - at the time of writing this was version 4.0.7.
Untar the downloaded source tarball, and configure:
|
1 2 3 4 5 6 |
# cd /usr/local/src # tar xzf nagios-4.0.7.tar.gz # cd nagios-4.0.7 # ./configure --prefix=/usr/local/nagios-4.0.7 \ > --sysconfdir=/etc/nagios \ > --with-command-group=nagcmd |
As you can see above, the appropriate command group has been specified, and we’ve indicated that Nagios configuration files should be installed under /etc/nagios.
If the configure returns correctly, make the software and install the appropriate components:
|
1 2 3 4 5 |
# make all # make install # make install-init # make install-config # make install-commandmode |
install-init will install an appropriate init script, install-config will install sample configuration files, and install-commandmode will set permissions on the external command directory.
To finish up the Nagios installation, create a symlink to our versioned prefix. This will facilitate easier upgrades:
|
1 |
# ln -s /usr/local/nagios-4.0.7 /usr/local/nagios |
As Nginx doesn’t come with an htpasswd tool (and we’ll be protecting our Nagios installation with basic HTTP auth for now), create a simple Perl script to do the job:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
# vi /usr/local/bin/htpasswd.pl #!/usr/bin/perl use strict; if ( @ARGV != 2 ){ print "usage: /usr/local/bin/htpasswd.pl <username> <password>\n"; } else { print $ARGV[0].":".crypt($ARGV[1],$ARGV[1])."\n"; } # chmod +x /usr/local/bin/htpasswd.pl |
Next, use this script to create an htpasswd file - this will be the user you’ll access Nagios via:
|
1 |
# /usr/local/bin/htpasswd.pl <user> <password> >> /etc/nagios/htpasswd.users |
Modify /etc/nagios/cgi.cfg, and substitute the <user> specified above for nagiosadmin (the default). Make sure you update /etc/nagios/objects/contacts.cfg too:
|
1 2 3 4 |
# vi /etc/nagios/cgi.cfg :%s/nagiosadmin/<user>/g # vi /etc/nagios/objects/contacts.cfg :%s/nagiosadmin/<user>/g |
Create a log directory for Nagios and set appropriate ownership. Update nagios.cfg appropriately:
|
1 2 3 4 |
# mkdir /var/log/nagios # chown nagios:nagios /var/log/nagios # vi /etc/nagios/nagios.cfg log_file=/var/log/nagios/nagios.log |
Next, we need to build and install the Nagios plugins. These can be downloaded from http://www.nagios.org/download/plugins. I’ll install some prerequisites so that the MySQL and SSL-enabled plugins are built:
|
1 2 3 |
# apt-get install libssl-dev # apt-get install libmysqlclient-dev # apt-get install mysql-client |
Untar the source tarball, and configure the plugins:
|
1 2 3 4 |
# cd /usr/local/src # tar xzf nagios-plugins-2.0.3.tar.gz # cd nagios-plugins-2.0.3 # ./configure --with-nagios-user=nagios --with-nagios-group=nagios |
By default, the prefix will be /usr/local/nagios so we don’t need to specify it here (our earlier symlink takes care of things for us).
Make and install the plugins:
|
1 2 |
# make # make install |
We’re almost ready to start Nagios. Add it to automatically start at the appropriate runlevels:
|
1 |
# update-rc.d -f nagios defaults |
Run a check of the configuration:
|
1 |
# /usr/local/nagios/bin/nagios -v /etc/nagios/nagios.cfg |
If no errors are reported (the sample configuration monitors a few services on localhost and should be enough to get you started), start up Nagios:
|
1 |
# /etc/init.d/nagios start |
Check that fcgiwrap is up (it should have been started automatically after installation):
|
1 |
# service fcgiwrap status |
And ensure that php5-fpm is running:
|
1 |
# service php5-fpm status |
It should be noted that the default configurations of fcgiwrap and php5-fpm use UNIX sockets rather than TCP, which should be fine for our purposes.
Verify the status of Nginx, it should be running as it would have been started after installation:
|
1 |
# service nginx status |
Create a configuration file for your Nagios installation as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# vi /etc/nginx/sites-available/nagios.example.com.conf server { server_name nagios.example.com; access_log /var/log/nginx/nagios.example.com-access.log; error_log /var/log/nginx/nagios.example.com-error.log; auth_basic "Private"; auth_basic_user_file /etc/nagios/htpasswd.users; root /var/www/virtualhosts/nagios.example.com; index index.php index.html; location / { try_files $uri $uri/ index.php /nagios; } location /nagios { alias /usr/local/nagios/share; } location ~ ^/nagios/(.*\.php)$ { alias /usr/local/nagios/share/$1; include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm.sock; } location ~ \.cgi$ { root /usr/local/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_params; fastcgi_pass unix:/var/run/fcgiwrap.socket; } location ~ \.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm.sock; } } |
Ensure that the correct socket paths are specified.
Disable the default site included with the Nginx installation, and enable the Nagios site:
|
1 2 |
# rm /etc/nginx/sites-enabled/default # ln -s /etc/nginx/sites-available/nagios.example.com.conf /etc/nginx/sites-enabled |
Reload Nginx:
|
1 |
# service nginx reload |
You should now be able to browse to http://nagios.example.com and access your Nagios installation.
A few things remain to be done. Install Postfix so that Nagios can send email:
|
1 |
# apt-get install postfix |
Select “Internet Site” when prompted for mailserver type. Next, install heirloom-mailx to make the /usr/bin/mail command available:
|
1 |
# apt-get install heirloom-mailx |
Modify /etc/nagios/objects/commands.cfg and update the mail commands with the appropriate path:
|
1 2 |
# vi /etc/nagios/objects/commands.cfg <update /bin/mail to /usr/bin/mail> |
Reload Nagios:
|
1 |
# service nagios reload |
And that’s it!
Note: There is an issue when scheduling command checks that results in the following error “Error: Could not open command file ‘/usr/local/nagios-4.0.7/var/rw/nagios.cmd’ for update!” even though the command file permissions and ownership are correct. I have a post open in the Nagios forums to get to the bottom of this, but for now, you can add the following to line 194 of /etc/init.d/nagios and restart Nagios:
|
1 |
chgrp www-data ${NagiosCommandFile} |
so that the command file can be written to by the web server user www-data. I will update this post once I get a solution in the Nagios forums. Another workaround is to change the group ownership of the /usr/local/nagios/var/rw directory to www-data:
|
1 2 |
# chgrp www-data /usr/local/nagios/var/rw # service nagios restart |