HostOnNet Blog

Installing Nagios Core on Ubuntu/Debian

Nagios is a very popular open source monitoring system. In this tutorial, we will cover the installation of Nagios 4 and some basic configuration to to monitor host resources via the web interface.

To follow this tutorial, you must have superuser privileges on the Ubuntu server that will run Nagios and a fully working LAMP stack.

First, update your apt-get package lists:

apt-get update

Then install the required packages:

Because we are building Nagios Core from source, we must install a few development libraries that will allow us to complete the build.

apt-get install build-essential libgd2-xpm-dev openssl libssl-dev xinetd apache2-utils unzip

We must create a user and group that will run the Nagios process. Create a “nagios” user and “nagcmd” group, then add the user to the group with these commands:

useradd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios

In order to issue external commands via the web interface to Nagios, we must add the web server user, www-data, to the nagcmd group:

usermod -G nagcmd www-data

Install Nagios Core

Create a folder called nagios-installer in the home directory and download the source code for the latest stable release of Nagios Core (nagios-4.1.1 at this time).

mkdir ~/nagios-installer
cd ~/nagios-installer
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.1.1.tar.gz

Extract the Nagios archive with this command:

tar xvf nagios-*.tar.gz

Then change to the extracted directory:

cd nagios-*

Run the following commands one by one from the Terminal to compile and install nagios.

./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-config
make install-commandmode
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-available/nagios.conf

Create a nagiosadmin account for logging into the Nagios web interface. Remember the password you assign to this account. You’ll need it while logging in to nagios web interface.

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

Now create a symbolic link of nagios.conf to the sites-enabled directory:

ln -s /etc/apache2/sites-available/nagios.conf /etc/apache2/sites-enabled/

Restart Apache to make the new settings take effect.

service apache2 restart

Download and Install Nagios Plugins

cd ~/nagios-installer
wget http://www.nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz
tar xvf nagios-plugins-*.tar.gz
cd nagios-plugins-*
./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl
make
make install

Configure Nagios

The default configuration files will work fine for getting started with Nagios. But you need to put your actual email ID to receive alerts.

Edit /usr/local/nagios/etc/objects/contacts.cfg config file and change the email addres

vi /usr/local/nagios/etc/objects/contacts.cfg

Enable Apache’s rewrite and cgi modules:

a2enmod rewrite
a2enmod cgi

Restart apache

service apache2 restart

Check nagios conf file for any syntax errors:

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

If there are no errors, start nagios service and make it to start automatically on every boot.

service nagios start

you may see the following error message while starting nagios service.

Failed to start nagios.service: Unit nagios.service failed to load: No such file or directory.

To fix this error, copy /etc/init.d/skeleton to /etc/init.d/nagios using the following command:

cp /etc/init.d/skeleton /etc/init.d/nagios

Edit file /etc/init.d/nagios

vi /etc/init.d/nagios

Add the following lines:

DESC="Nagios"
NAME=nagios
DAEMON=/usr/local/nagios/bin/$NAME
DAEMON_ARGS="-d /usr/local/nagios/etc/nagios.cfg"
PIDFILE=/usr/local/nagios/var/$NAME.lock

Save and close the file.

change the permissions of the file

chmod +x /etc/init.d/nagios

start nagios service

service nagios start
OR
/etc/init.d/nagios start

To start nagios automatically on every boot, run the following command.

sudo ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios

Access Nagios Web Interface

Open up your web browser and navigate to http://nagios-server-ip/nagios and enter the username as nagiosadmin and its password which we created in the earlier steps.

Click on the “Hosts” section in the left pane of the console. You will see there the no of hosts being monitored by Nagios server. We haven’t added any hosts yet. So it simply monitors the localhost itself only.

Let’s see how to monitor another host with Nagios in next post.


Posted in Ubuntu