Nagios is a free Unix software that allows you to monitor systems, networks, and infrastructure. It offers both monitoring and alerting services for configured servers, switches, applications, and services. One of the most useful features of Nagios is its double alert system. Not only will the user receive an alert when one of the monitored objects first encounters an error, a second alert will be given when the problem has been resolved. Nagios is a useful software that will give any system administrator some peace of mind when managing their systems, and can be installed on all Linux distributions. Thanks to the extensive capabilities of Nagios, it can even provide other benefits along with its traditional monitoring services such as reduced server downtime and resulting business loss prevention, assistance for IT upgrades and upgrade planning, and security breach detection.
This guide will cover how to install Nagios on Ubuntu 14.
Getting Started
In order to complete this tutorial, you will need the following:
• 1 server (Cloud Server or Dedicated Server) running Ubuntu 14
Before proceeding, you will also need to ensure that you are able to execute commands as root. This means that you should either be able to login as the root user, or ideally, execute root commands under a user with superuser privileges.
Tutorial
Nagios requires certain prerequisite packages which we can install using the default package manager utility for Ubuntu 14, apt-get. Execute the following command as root. You can either be logged in as the root user, or when using an account with superuser priviliges, append the command sudo to the start of the line.
apt-get install wget build-essential libgd2-xpm-dev apache2-utils unzip
To run Nagios, you will also need to have the Apache web server and PHP. The Apache HTTP server is the most popularly used open-source multi-platform web server, providing the full range of web features such as CGI, SSL, and virtual domains. PHP, which stands for PHP: Hypertext Preprocessor, is another popular open-source tool and is a scripting language that is especially suited for web development. Install Apache and PHP (as well as some prerequisite packages) with apt-get:
apt-get install apache2 php5 php5-mysql php5-gd libgd-dev libapache2-mod-php5
When Apache is installed, you will need to make some modifications to its configuration in order for the web server to be able to handle PHP. First, we will need to modify the serving preferences of Apache. By default, Apache will look for a file called index.html in the webroot directory /var/www/html/. However, we want it to serve instead the index.php file, even if the HTML file is also present. We can easily modify the preferences of Apache by opening the configuration file from /etc/apache2/mods-enabled/dir.conf using the text editor nano:
nano /etc/apache2/mods-enabled/dir.conf
With the file open, you will see the following:
<ifmodule mod_dir.c="mod_dir.c">
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</ifmodule>
This file dictates the preference order of Apache web server, and the most preferred files are found to the left, with the least preferred on the right. We will need to move index.php from its current place in the middle of the list to the leftmost side, coming immediately before index.html. The modified final file should look as follows:
<ifmodule mod_dir.c="mod_dir.c">
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</ifmodule>
Save the file and close nano when you are finished. Next, restart Apache for the changes to take into effect:
service apache2 restart
To run Nagios, you will need to create a specific user to serve as the Nagios user. You can easily create a new user with the useradd command, which will create a user with whichever name is specified after the command (in this case “nagios”). Execute the following as root:
useradd -m nagios
After entering the user creation command, you will need to provide the user with a password. Execute the passwd command and when prompted, enter the new password.
passwd nagios
Next, create the “nagcmd” group for Nagios. This will allow external commands to be submitted through the web interface. We will add both the Nagios user and the Apache user to the group. The usermod command will allow us to modify various details of a user, such as the group.
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd www-data
To actually install Nagios, you will first to obtain some information about the latest stable release versions from the Nagios downloads page. Check both the versions of Nagios Core and the Nagios Plugins, you will need them. While Nagios Core will provide the base functionality above, the Plugins package is required in order to best take advantage of the Nagios offering by allowing you to monitor hosts, devices, services, protocols, and applications. At the time of writing, the latest stable release of Nagios Core is 4.2.1 and Nagios Plugins is 2.1.2. The following command, which uses the web utility wget, will then download the Nagios Core and Plugins files. Replace the respective version numbers with the latest numbers if they have changed.
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.2.1.tar.gz
wget http://www.nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gz
The previous step will have downloaded two archived TAR files to whichever directory you executed the command in. To unarchive the files so that you can use them, use the command tar with the options xzf to extract (x the files, filter the archive through gzip (z, and output the new unpacked directory with the same file name (f). First, start with the Nagios Core:
tar xzf nagios-4.2.1.tar.gz
Next, change to the new unpacked directory:
cd nagios-4.2.1/
To compile and install Nagios, you will need to build it from source using make. Execute all of the following commands in the Nagios directory as the superuser. Don’t forget to append sudo to the beginning of each command if you are not directly signed in as root.
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-config
make install-commandmode
When the above make commands have completed, you will need to finally install the Nagios web interface using:
make install-webconf
There is a chance that you will receive the following error when installing the web interface:
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf
/usr/bin/install: cannot create regular file ‘/etc/httpd/conf.d/nagios.conf’: No such file or directory
Makefile:296: recipe for target 'install-webconf' failed
make: *** [install-webconf] Error 1
This issue occurs if Nagios tries to create its configuration file, nagios.conf, inside the /etc/httpd.conf/ directory. However, this file should instead be in the /etc/apache2/sites-enabled directory. We can ensure that the configuration file is placed in the correct directory by executing the following command in place of make install-webconf as root:
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-enabled/nagios.conf
After the web interface installation completes, double check that the configuration file is where it should be using ls, which will list files in a given directory:
ls -l /etc/apache2/sites-enabled/
To log into the Nagios web interface, you will have to create a new administrative user account called nagiosadmin. You will use this to log into the Nagios web interface, so remember the password that you provide for this account. Create the user with:
sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
Restart the Apache web server for the changes made to take into effect with the service command, which allows you to manage all system services:
service apache2 restart
With Nagios and its web interface installed, you will need to then configure the Nagios account to set up the correct email address where you would like to receive alerts. Edit the /usr/local/nagios/etc/objects/contacts.cfg configuration file with the nano text editor to modify the email address associated with the nagiosadmin contact definition:
nano /usr/local/nagios/etc/objects/contacts.cfg
With the file open in nano, search for the following section of code. Locate the field called email in the contact definition for nagiosadmin. When you have found the correct section, edit the field to your email.
[...]
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
}
[...]
Save and close the file when you are finished editing.
Now, we are ready to unzip and install the Nagios Plugins. In the directory where you downloaded the TAR archives, unarchive the plugins file nagios-plugins-2.1.2.tar.gz using tar xzf:
tar xzf nagios-plugins-2.1.2.tar.gz
When the unarchiving is done, change to the newly created plugin directory with the command cd:
cd nagios-plugins-2.1.2/
In the plugin directory, execute the following three commands to compile and install the Nagios Plugins:
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
You can easily check if your configuration files have any errors with the following command executed with superuser privileges:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
If there are no configuration errors, you will now be able to start up Nagios. Start it using the service command. As a bonus, add Nagios to run on boot so that you will not need to do it manually each time.
service nagios start
ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios
To verify the installation, access the Nagios interface at the following URL, replacing the text with the IP address of your Ubuntu 14 server:
http:///nagios
The login details will be nagiosadmin, with the password that you set above. The Nagios administration console will display a variety of details about Nagios, Nagios news, and your system upon successful login.
Conclusion
Congratulations! With Nagios set up on your Ubuntu 14 server, you can feel safer knowing that your system is now more secure. With the ability to receive double alerts on a variety of possible desired objects to monitor, your Ubuntu 14 server is better equipped for whatever may happen. Explore the Nagios web interface and the online documentation to see what features will best suit your needs, and if you enjoyed this tutorial, feel free to share it with your friends!