How to set up Nagios on Ubuntu 16.04

Created by Jordy Leffers at 06-12-2017 11:05:35 +0100

Nagios is a popular monitoring system that allows you to take count of possible server/hosting related issues, such as disk space and/or CPU usage, before these affect your customers. It's especially handy as you can configure notifications to your e-mail. In this tutorial we'll be going over how to install and configure Nagios step by step. 

This tutorial is based on the cloud containers created on the my.cloudcontainers.net page. This means that you are by default the root user, so all of the commands below don't make use of sudo. If however, you're not the root user on your system, you'll have to add "sudo" in front of the commands found in the guide below.

For the purpose of this tutorial we're going to need at least two Ubuntu servers: One that'll run our monitoring tool, Nagios, and one server to be monitored. We'll skip the steps on how to set up an Ubuntu server since you can easily get one via www.cloudcontainers.net.

The Nagios server

Log in to the server you want Nagios installed on. This is the server we're going to use to monitor our other servers and host the web interface that will output all configurable kinds of information to us.

Essentials

It's always good practice to start by updating your current packages:

apt update && apt upgrade -y


Next, we'll want to install both PHP and Apache, as well as some other packages Nagios will need:

apt install apache2 php libapache2-mod-php php-mcrypt php-mysql build-essential libgd2-xpm-dev openssl libssl-dev unzip


Great, we should now have al prerequisite packages. The next step is to add a Nagio user, a Nagio group and add the user to that group, the user will eventually run the Nagios process.

useradd nagios && groupadd nagcmd && usermod -a -G nagcmd nagios


Installing Nagios

The next step is to get ourselves the latest version of Nagios. You can find the latest version on the Nagios website, choose the appropriate version, copy the link address and use it in the following command:

wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.2.tar.gz?__hstc=118811158.d13c0262b1add2c4d8a4ba4f85611bc5.1534509957490.1534509957490.1534509957490.1&__hssc=118811158.1.1534509957490&__hsfp=1728764484#_ga=2.80077335.1406529778.1534509956-2053430851.1534509956


Then we extract the archive:

tar -xzf /the/file/you/download

And navigate to the folder you've just extracted:

cd nagios-*x*


Inside the Nagios directory, we are going to execute a ./configure script that will configure which group and user to use. Use the appropriate user name and group name you just made:

./configure --with-nagios-group=nagios --with-command-group=nagcmd


We can now compile and install Nagios with the following commands, in this order:

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


Since we'll use Apache to run Nagio's web interface, we're going to copy the Apache sample config file to the sites-available directory.

/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-available/nagios.conf


If we want the web interface to be able to externally issue commands to Nagios, we'll want to add the www-data web server user to our newly created nagcmd group:

usermod -G nagcmd www-data


The Check_NRPE plugin

Nagios uses a plugin that consists of two parts in order to monitor other servers. One part of this plugin will be installed on the Nagios server, the other will be installed on the monitored server. This plugin is called the Nagios Remote Plugin Executor (NRPE), check_nrpe is used by the Nagios server to receive data from our monitored server(s), on which the NRPE daemon will run and send data to our monitoring Nagios server.

You can find the latest version of the check_NRPE plugin here.

First move to your home directory and then download the plugin archive:

cd wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.2.1/nrpe-3.2.1.tar.gz -O check_nrpe


Unpack the archive:

tar -zxf check_nrpe


And navigate to your newly extracted folder, note that your version might be different:

cd nrpe-3.2.1


Next, we'll run our configuration script.

./configure


Build and install the plugin:

make check_nrpe make install-plugin


Configure the Nagios server

Let's configure our Nagios server, we'll have to edit some text files.

Open the Nagios configuration file,I'll be using the text editor Nano, but you can choose whichever you want. (How to use Nano)

nano /usr/local/nagios/etc/nagios.cfg

And search the following line:

#cfg_dir=/usr/local/nagios/etc/servers

Uncomment it by removing the # character, to make it look like this:

cfg_dir=/usr/local/nagios/etc/servers


Next, we'll create a directory for the purpose of storing the configuration file for each of the servers that you'll monitor:

mkdir /usr/local/nagios/etc/servers


Then, we'll make some modifications to the Nagios contacts file. We'll add your email address here.

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

Find the line asking for your email and add yours, like this:


Now, we will add a command to our Nagios that will enable us to check_nrpe using a simple command.

nano /usr/local/nagios/etc/objects/commands.cfg

On the bottom of the file, add the following text:

define command{
        command_name check_nrpe
        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
 }


Next, we will configure Apache as to run the Nagios web interface. First we'll enable two Apache modules (rewrite and cgi):

a2enmod rewrite && a2enmod cgi

You'll be asked to restart Apache, you could, but first we're going to have to configure some other settings. We're going to create an admin user to acces the web interface. Let's call it nagiosadmin

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

You'll be prompted for a password, remember this as you'll need it to log in onto the Nagios web interface.


Then, we have to create a symbolic link for the nagios.conf file we just edited, to the apache directory that holds the actually enabled sites:

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




It might be useful to prevent other IP addresses from accessing Nagios' web interface, since this could contain information you don't necessarily want to have public. This is optional but recommended. In order to achieve this, open up the nagios.conf file again.

nano /etc/apache2/sites-availabe/nagios.conf


Search for the following configurations:


We need to comment the top two lines out, and uncomment the bottom three, then add your privileged IP address you want to be able to acces the interface from. Like this: Note that this configuration block is present two times in the config file, so change them both


Next, we restart Apache so our new configurations can take effect:

systemctl restart apache2


In order for us to enable Nagios on boot time, we create the nagios.service file:

nano /etc/systemd/system/nagios.service

Then we add the following configurations:

[Unit] 
Description=Nagios BindTo=network.target 

[Install] 
WantedBy=multi-user.target 

[Service] 
Type=simple
User=nagios 
Group=nagios 
ExecStart=/usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg


Then we tell the Nagios service to run at server boot time using the following command:

systemctl enable /etc/systemd/system/nagios.service


We can also manually start, stop or check our Nagios server's status using the following commands:

systemctl start nagios systemctl stop nagios systemctl status nagios


Though the server is now running, and we could log in to the web interface, we'll still need to install the plugins the Nagios service will use to monitor itself.


The Nagios web interface

Now that we have configured our Nagios server, we can access the interface by, in your browser, navigating to the server's IP followed by /nagios.

http://nagios_server_ip/nagios


a popup will show up, requesting a username and password. This is the nagiosadmin we configured earlier in the tutorial, fill in its username (nagiosadmin) and the password you've chosen.

You will now have the following interface in front of you:



If you click on host groups in the menu on the left, you'll find Nagios is currently only monitoring itself. Also, you will find that it says that, even though you're now looking at the web interface hosted by that local server, the localhost is down.

This is because we're still missing some plugins. The process of installing these plugins will have to be done both for the Nagios server, as well as the server you want monitored. You can install these plugins (as described in the Both servers: Nagios-plugins) section right now, or install them on both the Nagios and the monitored server simultaneously.






The monitored server: before Nagios-plugins

Before installing the plugins on our server that's to be monitored, we first need some dependencies and a user.

You can repeat the steps for the monitored server on all server you want to monitor.


Add a Nagios user, a Nagios group and add the user to that group, the user will eventually run the Nagios process. In this tutorial I name the group, just like in the Nagios server, have the same name: nagcmd.

useradd nagios && groupadd nagcmd && usermod -a -G nagcmd nagios


Then we update our current packages:

apt update && apt upgrade -y

And install the following packages:

apt install build-essential libgd2-xpm-dev openssl libssl-dev unzip


Next download and install the Nagios-plugins as described below:



Both servers: Nagios-plugins

The steps of this part of the tutorial will have to be done on both servers, so either run them at the same time, or each subsequently.

We can install the Nagios plugins using the default package manager, however, that version might be outdated so it's safer to download from source. You can get the latest version on Nagios-plugins.org


Download the plugins to your home directory: 

Note that if you use a different version, you might have to edit the following commands a little.

cd curl -L -O http://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz


Extract the package:

tar xzf nagios-plugins-2.2.1.tar.gz


Navigate to the extracted directory:

cd nagios-plugins-2.2.1


And run the configuration script:

./configure --with-nagios-user=nagios --with-nagios-group=nagcmd --with-openssl

This might take a while


Compile and install the plugins:

make && make install


You should now be able to see the localhost UP with 8 service OK on the web interface:


Next we'll move on to finish the configuration on the server we want to monitor. 


The monitored server: after Nagios-plugins install

Now that we have the Nagios-plugins installed, we can install the NRPE, just like we have on the Nagios server, here as well.

Download the latest NRPE to your home directory:

Note that if you use a different version, you might have to edit the following commands a little.

cd curl -L -O https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.2.1/nrpe-3.2.1.tar.gz

Extract the package:

tar xzf nrpe-3.2.1.tar.gz

Navigate into the directory we just extracted:

cd nrpe-3.2.1


And run the configuration script:

./configure --enable-command-args --with-nagios-user=nagios --with-nagios-group=nagcmd --with-ssl=/usr/bin/openssl --with-ssl-lib=/usr/lib/x86_64-linux-gnu

This might take a while.


Next we compile and install NRPE using the following commands:

make all make install make install-config make install-init


Then we'll open up the NRPE config file:

nano /usr/local/nagios/etc/nrpe.cfg


And find the "allowed_hosts" section. Add the IP address of your Nagios server to the comma separated list, like this:

allowed_hosts=127.0.0.1,::1,83.96.162.177

This will configure NRPE to allow requests from your Nagios server.


You can now start, stop and check NRPE's status with the followin commands:

systemctl start nrpe.service systemctl stop nrpe.service systemctl status nrpe.service


To check the connection with the monitored server, run the following command on the Nagios server:


/usr/local/nagios/libexec/check_nrpe -H monitored_server_IP

Your output should be "NRPE" + whatever version you have installed.


Next we'll look for the root file system. We'll need this to let NRPE check out and monitor our system. Execute the following command on the monitored server to find where the root file system is located:

df -h /

In my case "/dev/zfsds0" would be its location.

Next, open up de NRPE config file:

nano /usr/local/nagios/etc/nrpe.cfg

We'll need to modify two settings in this file.

Let's start with the server_address. set this to the IP address of the monitored server.

On the command[check_hda1], rename it to represent your file system. Also change the location at the end of this line "/dev/hda1" and replace this with the location and name of your root file system. 

These may be commented out, un-comment them and make the described changes.

Like this:

... server_address=83.96.162.175 ... command[check_zfsds0]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/zfsds0 ...



Next, we restart the nrpe.service for our changes to take effect:

systemctl restart nrpe.service


Monitor remote hosts

Connect to the Nagios server again. In order for the Nagios server to monitor our remote hosts, we'll have to make a config file separately for every server we want to monitor. These will later show up in the web interface, just like the localhost.

Create a new configuration file on the Nagios server for the monitored server (in this tutorial I have only one monitored server, just for the purposes of this tutorial) in /usr/local/nagios/etc/servers

nano /usr/local/nagios/etc/servers/monitered_servers_name_here.cfg


Add the following configurations. Don't forget to, besides naming your server correctly, also edit the root file system name in the second block you your appropriate name.

define host {
        use                             linux-server
        host_name                       your_monitored_servers_name
        alias                           your desired servers alias
        address                         your_monitored_servers_ip
        max_check_attempts              5
        check_period                    24x7
        notification_interval           30
        notification_period             24x7
 }

 define service {
        use                             generic-service
        host_name                       your_monitored_servers_name
        service_description             /dev/hda1 free space
        check_command                   check_nrpe!check_vda1
 }

 define service {
        use                             generic-service
        host_name                       your_monitored_servers_name
        service_description             CPU load
        check_command                   check_nrpe!check_load
 }

The first block is to check the server's status, the second is to monitor disk space and the third is to monitor CPU usage.


When done, it should look roughly like this:

define host {
        use                             linux-server
        host_name                       Nagios2-Monitored
        alias                           Monitored_by_Nagios
        address                         83.96.162.175
        max_check_attempts              5
        check_period                    24x7
        notification_interval           30
        notification_period             24x7
 }

 define service {
        use                             generic-service
        host_name                       Nagios2-Monitored
        service_description             /dev/zfsds0 free space
        check_command                   check_nrpe!check_zfsds0
 }

 define service {
        use                             generic-service
        host_name                       Nagios2-Monitored
        service_description             CPU load
        check_command                   check_nrpe!check_load
 }


Restart Nagios:

systemctl restart nagios



Congratulations, you've now set up a Nagios monitoring system!

Comments

Comments are turned off.