How to install Nagios in 30 Minutes #howto

Last Modified: 15th Dec 2015

Circa 1999, Nagios is the most popular open source monitoring software.

It monitors remote resources like load, CPU, memory or service metrics via Nagios Remote Plugin Executor (NRPE). It also allows you to monitor public exposed services, like availability (ping) and SSH, HTTP or HTTPS.

This tutorial (#howto install Nagios) will walk you through installing and configuring Nagios 4 in Ubuntu 14.04 LTS. A couple of notes before we start:

We assume you have one target server, a typical web server with the LAMP stack, and one dedicated machine for your Nagios Server.

We will install the agent on monitored servers using the official binary packages. The server side, we will build from sources. That’s because the packages included in Ubuntu are quite old (nagios3).

So, without further ado, let’s start by installing Nagios.

#howto install Nagios Server

As a prerequisite, we need an Apache server with PHP enabled:

 $ sudo apt-get install apache2 libapache2-mod-php5 php5 apache2-utils

We also need some Apache modules to be enabled for the Nagios web interface:

$ sudo a2enmod rewrite

$ sudo a2enmod cgi

Now let’s create the user and groups for the Nagios service:

$ sudo addgroup --system nagios

$ sudo addgroup --system nagioscmd

$ sudo adduser --home /usr/local/nagios --shell /bin/true --ingroup nagioscmd --system nagios

In order to run commands through the web interface we must add the Apache user to the Nagios commands group:

$ sudo usermod -G nagioscmd www-data

Next, let’s install the dependencies required to build and install Nagios Core server:

$ sudo apt-get install build-essential libgd2-xpm-dev openssl libssl-dev xinetd unzip

Now go to the Nagios Core Downloads page, (you probably want to click on “skip to download” in the contact form) and copy the link to the latest stable release (4.1.1 at the time of writing). We will download it to our server:

$ cd ~
$ curl -L -O https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.1.1.tar.gz

Let’s uncompress the tarball:

$ tar xvzf nagios-4.1.1.tar.gz

Change to the source directory:

$ cd nagios-4.1.1

And build using the typical Unix autotools process:

$ ./configure --with-nagios-group=nagios --with-command-group=nagioscmd

$ make all

To finish, install the files including init scripts, sample configuration and Apache configuration:

$ sudo make install

$ sudo make install-commandmode

$ sudo make install-init

$ sudo make install-config

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

Enable the Nagios Apache configuration:

$ sudo a2ensite nagios

And make sure Nagios init script is called when the server boots up and shutdowns:

$ sudo update-rc.d nagios defaults

As we want to protect our Nagios installation, let’s create some user credentials. The system is already preconfigured to check for these credentials:

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

The Apache configuration is ready now, so let’s apply settings:

$ sudo service apache2 restart

Install Nagios Plugins

Once the server is ready, we need to install some plugins for the resources we want to monitor. Navigate to the Nagios Plugins Downloads page and copy the link (2.1.1 at the time of writing). Let’s download the package:

$ cd ~

$ curl -L -O http://nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz

Uncompress, build and install:

$ tar xvzf nagios-plugins-2.1.1.tar.gz

$ cd nagios-plugins-2.1.1

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

$ make

$ sudo make install

Install NRPE

We will use NRPE to run checks on remote servers. Go to the NRPE Downloads page and copy the link to the latest stable version (2.15 at the time of writing). Then download it to your server:

$ cd ~

$ curl -L -O http://downloads.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz

Uncompress, build and install:

$ tar xzvf nrpe-2.15.tar.gz

$ cd nrpe-2.15

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

$ make all

$ sudo make install

$ sudo make install-xinetd

$ sudo make install-daemon-config

Configure NRPE

Nagios server will also run checks in the local server. That’s why we need to configure NRPE to accept these connections. NRPE runs with xinetd, so you need to edit the configuration file:

$ sudo vi /etc/xinetd.d/nrpe

And modify the only_from key to add the IP address of the Nagios server. Don’t forget to add a space character in between. In my case the IP is 10.240.0.6:

only_from = 127.0.0.1 10.240.0.6

Save, exit and restart xinetd to apply changes:

$ sudo service xinetd restart

#howto configure Nagios

Let’s start configuring our Nagios installation:

Nagios monitored servers

In order to keep things clean, all monitored servers will be placed into a single directory. To make that happen, we need to edit the Nagios configuration file:

$ sudo vi /usr/local/nagios/etc/nagios.cfg

And uncomment this file (by deleting the # character):

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

Save, exit and create that directory:

$ sudo mkdir /usr/local/nagios/etc/servers

Configure Nagios administrators

Nagios users and administrators are called contacts. Let’s edit the contacts.cfg file:

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

Fill in the email you want the notifications to go to:

email                           nagios@localhost        ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******

Configure check_nrpe command

As we want to run checks on remote servers using NRPE, we are going to define the command that will be used to retrieve the metrics. To do that, edit the commands.cfg file:

$ sudo vi /usr/local/nagios/etc/objects/commands.cfg

And add at the end of the file:

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

Don’t forget to save and exit.

The server side is now ready. Let’s validate the changes and apply all configuration changes:

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

Total Warnings: 0
Total Errors:   0

$ sudo service nagios start

Login Screen

Now we should be able to access Nagios web interface on: http://your_ip/nagios/.

Nagios Web UI

Great! We completed the initial setup. Nagios now does some basic checks on its own server. We may need to fine tune things a bit more but, first, let’s configure that LAMP server we want to monitor:

Adding a server to Nagios

If your server runs Debian or Ubuntu, just apt-get the packages:

$ sudo apt-get install nagios-plugins nagios-nrpe-server

On the other hand, if you run CentOS, RHEL or Scientific Linux:

# yum install epel-release

# yum install nrpe nagios-plugins-all openssl

Then edit the NRPE configuration file:

$ sudo vi /etc/nagios/nrpe.cfg

Under allowed_hosts add the IP address of your Nagios server, separated by a comma. In my case the IP is 10.240.0.6:

$ allowed_hosts=127.0.0.1,10.240.0.6

We need to configure which commands for checks are allowed to run from the Nagios server through NRPE. Some of them are enabled by default. In this example, we changed the check_disk to use sda1 (instead of hda1) to reflect our server configuration:

command[check_users]=/usr/lib/nagios/plugins/check_users -w 5 -c 10
command[check_load]=/usr/lib/nagios/plugins/check_load -w 15,10,5 -c 30,25,20
command[check_sda1]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/sda1
command[check_zombie_procs]=/usr/lib/nagios/plugins/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/lib/nagios/plugins/check_procs -w 150 -c 200

Save your changes and exit. Let’s restart NRPE to apply the changes. On CentOS, RHEL or Scientific Linux:

# systemctl start nrpe
# chkconfig nrpe on

On Debian/Ubuntu:

$ sudo /etc/init.d/nagios-nrpe-server restart

We now need to tell the Nagios server about this new server. To do this, you need to add a new file under the directory we created earlier:

$ sudo vi /usr/local/nagios/etc/servers/yourhost.cfg

And add the host definition:

define host {
use                             linux-server
host_name                       yourhost
alias                           My LAMP server
address                         10.240.0.5
max_check_attempts              5
check_period                    24x7
notification_interval           30
notification_period             24x7
}

This definition will only monitor availability, which means we probably need to add some additional checks in the same file. Here are some examples:

define service {
use                             generic-service
host_name                       yourhost
service_description             PING
check_command                   check_ping!100.0,20%!500.0,60%
}

define service {
use                             generic-service
host_name                       yourhost
service_description             SSH
check_command                   check_ssh
}

define service {
use                             generic-service
host_name                       yourhost
service_description             Current Load
check_command                   check_nrpe!check_load
}

define service {
use                             generic-service
host_name                       yourhost
service_description             Current Users
check_command                   check_nrpe!check_users
}

define service {
use                             generic-service
host_name                       ‘yourhost’
service_description             Disk Utilization
check_command                   check_nrpe!check_sda1
}

define service {
use                             generic-service
host_name                       ‘yourhost’
service_description             Zombie processes
check_command                   check_nrpe!check_zombie_procs
}

define service {
use                             generic-service
host_name                       ‘yourhost’
service_description             Total processes
check_command                   check_nrpe!check_total_procs
}

Save, exit and restart Nagios server to apply changes:

$ sudo /etc/init.d/nagios restart

That’s it! Nagios now monitors your LAMP server. You can complete all these steps in less than 30 minutes.

If you’re setting up Nagios in a production environment, there are a few additional things to consider:

  1. Creating a password for the web interface is an obvious first step. However, that’s not enough for production environments. We recommend you follow security practices and also validate the trust of the chain between the server and clients using SSL. NSClient++ is a great solution for that.
  2. Amending the configuration file in the command line is not that easy. What’s more, the configuration has to be duplicated (for server and client). Automation software like Puppet, Chef or Ansible is a great pain relief here. You can also use a graphical interface like Icinga or Centreon.
  3. When Nagios was designed, servers infrastructures were pretty much static. On Nagios you always need to tell the server what new nodes to monitor. Nowadays, of course, cloud infrastructure is dynamic and ever changing. While you can automate some things with 3rd party Nagios APIs, there is no escaping the fact that there is no interface, or devops workflows ready to support tags, containers, et cetera. You may need to look beyond Nagios for those.
  4. Do you need graphs? If your infrastructure has grown even to the slightest, then, yes, you probably need graphs. One of the most popular tools for graphs used to be pnp4nagios. These days you will go further with Graphite. It doesn’t only do graphs but it also provides a time series database for storing all metrics you collect.
  5. Do you need to scale? The bad news is, Nagios doesn’t scale very well and doesn’t provide high availability. This might not be a problem when you’re just starting out with small test accounts. You do however need to take that into account when planning for future deployments. You can play with mod_gearman but good luck with that. Some Nagios forks have tried to address this shortcoming, namely Shinken and Naemon.

Summary

Nagios is a good choice if you’re on zero budget and your requirements are simple. You can install it in 30 minutes and get your basic monitoring requirements covered.

On the other hand, if you are looking to monitor a production environment and your infrastructure is a bit more involved, you probably need to look elsewhere.

That’s where we shamelessly toot our own horn. Server Density offers a user interface (we like to think it’s very intuitive) that supports tagging, elastic graphs and advanced infrastructure workflows. It plays well with your automation tools and offers mobile apps too. Server Density is not free but it saves you significant time and effort, which translates to significant savings for your boss.

We hope you found this #howto install Nagios tutorial useful. Have we missed something? Any suggestions? Let us know in the comments.

Articles you care about. Delivered.

Help us speak your language. What is your primary tech stack?

Maybe another time