Retrospective IT

Install Nagios 4 on Ubuntu 16.04 LTS – Part One

Install Nagios 4 on Ubuntu 16.04 LTS - Part 1

I spent a lot of time over the past few months learning Nagios inside and out so I could monitor our infrastructure of close to 200 servers across 75+ locations. Since learning how to install and use Nagios can be daunting and intimidating to new Nagios users, I wanted to document my setup in hopes of helping others out. Please note that some of these settings are based on person preference and can be changed.

This post will be split into a four part series and will utilize Nagios version 4.2.1, Nagios Plugins version 2.13, and the NCPA client (latest versions at the time of writing).

This post assumes the following:

  • A base install of Ubuntu 16.04 LTS with a static IP address
  • This post assumes you are root (i.e. sudo su – )


First, we’ll need to install the prerequisites for our installs

apt install build-essential openssl libssl-dev xinetd apache2 apache2-utils php7.0 php7.0-gd libapache2-mod-php7.0 libperl-dev libgd-dev perl python2.7 snmp unzip

First, we’ll move to the /tmp folder and download all the files we need

cd /tmp
curl -L -O https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.2.1.tar.gz
curl -L -O http://nagios-plugins.org/download/nagios-plugins-2.1.3.tar.gz
curl -L -O https://raw.github.com/NagiosEnterprises/ncpa/master/client/check_ncpa.py

Compile and Install Nagios

Create the Nagios user and group accounts

useradd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd www-data

Decompress the Nagios source code

cd /tmp
tar xvzf nagios-4.2.1.tar.gz
cd nagios-4.2.1

Compile and install Nagios

./configure --with-nagios-group=nagios --with-command-group=nagcmd
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf

During my setup, install-webconf gave an error. To fix this, use the following command:

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

Compile and Install Nagios Plugins

Decompress the Nagios Plugins source

cd /tmp
tar xvzf nagios-plugins-2.1.3.tar.gz
cd nagios-plugins-2.1.3

Compile and install Nagios Plugins. (installed to /usr/local/nagios/libexec by default)

./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl
make
make install

Configure Nagios Web Interface

Enable the site within Apache2

a2ensite nagios.conf

I disabled the default Apache2 site

a2dissite 000-default.conf

Enable the cgi and rewrite modules

a2enmod cgi rewrite

Create the default user password (default user is nagiosadmin)

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

Reload Apache2

systemctl reload apache2

You should now be able to test the web interface by opening your browser and going to the link below and logging in with nagiosadmin and the password that was created earlier

http://yourserver/nagios

Configure NCPA Client

Copy the NCPA plugin you downloaded earlier to the Nagios Plugins directory

cp /tmp/check_ncpa.py /usr/local/nagios/libexec/.

Change the permissions on the plugin so Nagios can execute it

chown nagios:nagios /usr/local/nagios/libexec/check_ncpa.py
chmod 755 /usr/local/nagios/libexec/check_ncpa.py

There is a bug in the NCPA script on Ubuntu 16.04. The work around is to change the first line of check_ncpa.py to the following:

nano /usr/local/nagios/libexec/check_ncpa.py
#!/usr/bin/env pythin2.7

Configure Nagios Core

During configuration and before restarting Nagios, a pre-flight check needs to check the configuration files for any errors. If an error is detected during a restart, the Nagios service will fail. To perform a pre-flight check, run the following command:

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

Now configure the systemd file

nano /etc/systemd/system/nagios.service

[Unit]
Description=Nagios
BindTo=network.target
[Install] WantedBy=multi-user.target
Alias=nagios.service

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

Enable the service and reload systemd

systemctl enable /etc/systemd/system/nagios.service
systemctl daemon-reload

Start and see the status of the Nagios service\

systemctl start nagios && systemctl status nagios
(ctrl+c) to exit

We need to add entries into nagios.cfg to tell Nagios where we are going to store our cfg files. These files/directories can be placed anywhere you want, but I tend to put them in /usr/local/nagios/etc

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

Add the entries for a servers, commands, contacts, groups, and templates under “You can also tell Nagios to process all config files in a particular directory.” If you prefer to put all your contacts, commands, etc… in a single file, you can add those files under “You can specify individual object config files as shown below”

cfg_dir=/usr/local/nagios/etc/servers
cfg_dir=/usr/local/nagios/etc/commands
cfg_dir=/usr/local/nagios/etc/contacts
cfg_dir=/usr/local/nagios/etc/groups
cfg_dir=/usr/local/nagios/etc/templates

Now create the directories that we just listed.

mkdir /usr/local/nagios/etc/servers
mkdir /usr/local/nagios/etc/commands
mkdir /usr/local/nagios/etc/contacts
mkdir /usr/local/nagios/etc/groups
mkdir /usr/local/nagios/etc/templates

That’s it for part one. Make sure to check out the other parts of this series.

Install Nagios 4 on Ubuntu 16.04 LTS – Part One
Install Nagios 4 on Ubuntu 16.04 LTS – Part Two
Install Nagios 4 on Ubuntu 16.04 LTS – Part Three
Install Nagios 4 on Ubuntu 16.04 LTS – Part Four

Leave a Reply

Your email address will not be published. Required fields are marked *