Nagios 4.4.1 Install from Source on Ubuntu 18.04


This post is in the category: Guides

Posts here are mostly step-by-step guides on how to replicate something I have set up in the past. Read over my About page to see how I show commands/output and read the disclaimer.


I am just starting to upgrade / install Ubuntu 18.04 on my servers, so it’s time to create a guide to install Nagios 4 on Ubuntu 18.04.

This guide is going to be very similar to my other Nagios install guides.

I will refer to this server with the hostname s10-nagios. Change at will, but adjust the commands accordingly.

Base OS Installation

Install Ubuntu Server 18.04. If you are provisioning a VM, a 24GB drive will be plenty.

Set a root password. Yeah, it’s best to use sudo for the one-off commands. But sometimes being root is just easier.

[user]$ sudo passwd root

Be sure we are on the latest updates:

[root]$ apt-get update
[root]$ apt-get dist-upgrade

Some basic reminders: Set account passwords, bash prompts, etc.

Nagios & Plugins Install

This guide is for Nagios 4.4.1. You may choose to use a different version, just make sure you adjust the commands that deal with version numbers accordingly.

Prepare the Nagios User & Group

[root]$ useradd -m -s /bin/bash nagios
[root]$ passwd nagios

Create the group needed for submitting commands from the web.

[root]$ groupadd nagcmd
[root]$ usermod -a -G nagcmd nagios
[root]$ usermod -a -G nagcmd www-data

Download / Extract Archives

[root]$ cd ~
[root]$ wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.1.tar.gz
[root]$ tar -xzvf nagios-4.4.1.tar.gz
[root]$ wget http://www.nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
[root]$ tar -xzvf nagios-plugins-2.2.1.tar.gz

By the way, both of those tar command were without referencing the docs.

Compile and Install Nagios

Install some required packages.

[root]$ apt-get install libgd-dev apache2 php apache2-utils build-essential unzip
[root]$ cd ~/nagios-4.4.1
[root]$ ./configure --with-command-group=nagcmd
[root]$ make all

After the compiling is complete, the following options are presented:

make install – This installs the main program, CGIs, and HTML files

make install-init – This installs the init script in /etc/init.d

make install-commandmode – This installs and configures permissions on the directory for holding the external command file

make install-config – This installs SAMPLE config files in /usr/local/nagios/etc You’ll have to modify these sample files before you can use Nagios. Read the HTML documentation for more info on doing this. Pay particular attention to the docs on object configuration files, as they determine what/how things get monitored!

make install-webconf – This installs the Apache config file for the Nagios web interface

make install-exfoliation – This installs the Exfoliation theme for the Nagios web interface

make install-classicui – This installs the classic theme for the Nagios web interface

I ran the following installations:

[root]$ make install
[root]$ make install-init
[root]$ make install-commandmode
[root]$ make install-config
[root]$ make install-webconf
Uh-oh. Did you notice the error message?

Check out the output from this last command:

[root]$ make install-webconf
/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
make: *** [install-webconf] Error 1

Silly Nagios installer, this is a Debian box! Let’s install this Apache2 config manually.

[root]$ /usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/conf-available/nagios.conf
[root]$ ln -s /etc/apache2/conf-available/nagios.conf /etc/apache2/conf-enabled/nagios.conf

Don’t attempt to start Nagios yet.

Compile and Install Nagios Plugins

We first need to install some prerequisites.

[root]$ apt-get install libnet-snmp-perl libperl5.26 libpq5 libsensors4 libsnmp-base libsnmp30 libtalloc2 libtdb1 libwbclient0 samba-common samba-common-bin smbclient snmp whois libmysqlclient-dev libssl-dev

Now install it

[root]$ cd ~/nagios-plugins-2.2.1
[root]$ ./configure --with-nagios-user=nagios --with-nagios-group=nagios
[root]$ make
[root]$ make install

NRPE Plugin Make & Install

Note the steps below carefully. DO NOT run “make install”. All we want to do is copy out the check_nrpe plugin, which is apparently not included in the original plugin pack.

[root]$ cd ~
[root]$ wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.2.1/nrpe-3.2.1.tar.gz
[root]$ tar -xzvf nrpe-3.2.1.tar.gz
[root]$ cd nrpe-3.2.1
[root]$ ./configure --with-ssl=/usr/bin/openssl --with-ssl-lib=/usr/lib/x86_64-linux-gnu
[root]$ make all

Instead of installing this whole package, we just want the plugin exec.

[root]$ cp src/check_nrpe /usr/local/nagios/libexec/

Configuration

Create the Apache login credentials.

[root]$ htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

Configure your Nagios services / hosts. For now, I recommend that you only setup a couple service checks for localhost while we build out the rest of the configuration.

BONUS TIP: Bash Alias

Create this bash alias to make it easier to run checks. For the rest of this guide, I will use the command nverify to run a check assuming that you created this alias.

[root]$ vim ~/.bash_aliases
alias nverify="/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg"

Don’t forget to log out and then back in to this box to see the new alias.

Check the configuration.

[root]$ nverify

Hold your breath & start the services.

[root]$ service apache2 restart
[root]$ service nagios start

After starting up Nagios, attempt to navigate to http://s10-nagios/nagios. Provide the necessary credentials (remember, username is nagiosadmin).

You will notice that there is a red X icon stating that Nagios is not running, and clicking on a CGI link will download the appropriate file – not the intended result.

The fix is simple – enable CGI!

[root]$ a2enmod cgi
[root]$ service apache2 restart

Now configure Nagios to run at startup:

[root]$ systemctl enable nagios.service

Checkpoint One

Let’s pause a moment to verify we have not made any mistakes.
  1. Reboot s10-nagios
  2. Verify that the service automatically started. Correct any startup config issues.
  3. Attempt to navigate to http://s10-nagios/nagios from a computer on the same LAN.
  4. Login using credentials previously specified.
Everything pass? Great – onward!

Nagiosgraph

Install some more prerequisites.

[root]$ apt-get install libcgi-pm-perl librrds-perl libgd-gd2-perl

Download and extract the files.

[root]$ cd ~
[root]$ wget http://downloads.sourceforge.net/project/nagiosgraph/nagiosgraph/1.5.2/nagiosgraph-1.5.2.tar.gz
[root]$ tar -xzvf nagiosgraph-1.5.2.tar.gz
[root]$ cd nagiosgraph-1.5.2

Verify that we have a working environment.

[root]$ ./install.pl --check-prereq

If everything looks good, continue with the install. I think it’s best to keep everything separate from the Nagios install to make upgrades easier.

[root]$ ./install.pl --layout standalone --prefix /usr/local/nagiosgraph

Most of the questions should be left at the default, except the last few. Questions that are not default are emphasized.

Destination directory (prefix)? [/usr/local/nagiosgraph]
Location of configuration files (etc-dir)? [/usr/local/nagiosgraph/etc]
Location of executables? [/usr/local/nagiosgraph/bin]
Location of CGI scripts? [/usr/local/nagiosgraph/cgi]
Location of documentation (doc-dir)? [/usr/local/nagiosgraph/doc]
Location of examples? [/usr/local/nagiosgraph/examples]
Location of CSS and JavaScript files? [/usr/local/nagiosgraph/share]
Location of utilities? [/usr/local/nagiosgraph/util]
Location of state files (var-dir)? [/usr/local/nagiosgraph/var]
Location of RRD files? [/usr/local/nagiosgraph/var/rrd]
Location of log files (log-dir)? [/usr/local/nagiosgraph/var]
Path of log file? [/usr/local/nagiosgraph/var/nagiosgraph.log]
Path of CGI log file? [/usr/local/nagiosgraph/var/nagiosgraph-cgi.log]
URL of CGI scripts? [/nagiosgraph/cgi-bin]
URL of CSS file? [/nagiosgraph/nagiosgraph.css]
URL of JavaScript file? [/nagiosgraph/nagiosgraph.js]
Path of Nagios performance data file? [/tmp/perfdata.log]
URL of Nagios CGI scripts? [/nagios/cgi-bin]
username or userid of Nagios user? [nagios]
username or userid of web server user? [www-data]
Modify the Nagios configuration? [n] y
Path of Nagios configuration file? [/usr/local/nagios/etc/nagios.cfg]
Path of Nagios commands file? /usr/local/nagios/etc/objects/commands.cfg
Modify the Apache configuration? [n] y
Path of Apache configuration directory? /etc/apache2/sites-enabled

Restart some services.

[root]$ service apache2 restart
[root]$ service nagios restart

Graphs can now be accessed here: http://s10-nagios/nagiosgraph/cgi-bin/show.cgi

If you see a 403 forbidden error, modify the conf file to show new Apache2 parameters.

[root]$ vim /etc/apache2/sites-enabled/nagiosgraph.conf

Replace the legacy Allow from all with Require all granted. Restart apache2 and try viewing the graph page again.

Template Hacks…err…Integration

The graphs can be integrated into the templates with a couple hacks. They are a bit cringeworthy, but they work.

To activate the pop-up graphs that we will include later, create this file.

[root]$ vim /usr/local/nagios/share/ssi/common-header.ssi
<script type="text/javascript" src="/nagiosgraph/nagiosgraph.js"></script>

To include icons for a specific service, you will need this line in your service definition:

action_url      /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&geom=1000x200' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagiosgraph/cgi-bin/showgraph.cgi?host=$HOSTNAME$&service=$SERVICEDESC$

I also have a special graph saved for ping tests. I use this to show RTA and packet loss. We are going to utilize both actionurl and notesurl for this.

action_url              /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=pl,data&db=pl,warn&db=pl,crit&geom=1000x200' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagiosgraph/cgi-bin/showgraph.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=pl,data&db=pl,warn&db=pl,crit
notes_url               /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=rta,data&db=rta,warn&db=rta,crit&geom=1000x200' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagiosgraph/cgi-bin/showgraph.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=rta,data&db=rta,warn&db=rta,crit

Since we make use of the notes icon, lets give it the graph icon as well.

[root]$ mv /usr/local/nagios/share/images/notes.gif /usr/local/nagios/share/images/notes.bak.gif
[root]$ cp /usr/local/nagios/share/images/action.gif /usr/local/nagios/share/images/notes.gif

Go into your host configuration files and add the graph templates to your service definitions. Only add them to services that are returning performance data.

Here are some example services you can change in the default configuration files.

[root]$ vim /usr/local/nagios/etc/objects/localhost.cfg

Delete the service definition for Current Load and replace with this.

define service {
        use                     local-service
        host_name               localhost
        service_description     Current Load
        check_command           check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
        action_url              /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&geom=1000x200' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagiosgraph/cgi-bin/showgraph.cgi?host=$HOSTNAME$&service=$SERVICEDESC$
}

Delete the service definition for Ping and replace with this.

define service {
        use                     local-service
        host_name               localhost
        service_description     PING
        check_command           check_ping!100.0,20%!500.0,60%
        action_url              /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=pl,data&db=pl,warn&db=pl,crit&geom=1000x200' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagiosgraph/cgi-bin/showgraph.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=pl,data&db=pl,warn&db=pl,crit
        notes_url               /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=rta,data&db=rta,warn&db=rta,crit&geom=1000x200' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagiosgraph/cgi-bin/showgraph.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=rta,data&db=rta,warn&db=rta,crit
}

Check and restart Nagios.

[root]$ nverify
[root]$ service nagios restart

Now reload your Nagios homepage to see the graph icons.

Checkpoint Two

Let’s pause a moment to verify we have not made any mistakes.

You may have to let Nagios run for about 30 minutes to give the graphs a chance to collect some data.

  1. Navigate to http://s10-nagios/nagios from a computer on the same LAN.
  2. Login using credentials previously specified.
  3. View the services page, and note that ping and other services should have graph icons next to them.
  4. Mouseover the icons, confirm that graphs are popping up.
  5. Click on the icons, verify that you are being led to the page that shows graphs.
Everything pass? Great – onward!

Monitoring Nagios / MRTG

So you now have Nagios running to ensure all your servers and gadgets are running as expected. But…how do you know that Nagios is healthy? We should monitor that too! We are going to use MRTG to graph stats about how Nagios is running.

[root]$ apt-get install mrtg

Copy the default config file for Nagios stats.

[root]$ cp ~/nagios-4.4.1/sample-config/mrtg.cfg /usr/local/nagios/etc/
[root]$ mkdir /usr/local/nagios/share/stats

Configure the working directory.

[root]$ vim /usr/local/nagios/etc/mrtg.cfg

Add this to the top.

WorkDir: /usr/local/nagios/share/stats

Make the initial run.

[root]$ env LANG=C mrtg /usr/local/nagios/etc/mrtg.cfg

Configure the HTML page.

[root]$ indexmaker /usr/local/nagios/etc/mrtg.cfg --output=/usr/local/nagios/share/stats/index.html

Create a scheduled cron job to keep the status up to date.

[root]$ vim /etc/cron.d/nagiostats
*/5 * * * *  root  env LANG=C /usr/bin/mrtg /usr/local/nagios/etc/mrtg.cfg

Checkpoint Three

Let’s pause a moment to verify we have not made any mistakes.

You may have to let Nagios run for about 30 minutes to give the graphs a chance to collect some data.

  1. Navigate to http://s10-nag-01/nagios/stats/ from a computer on the same LAN.
  2. Verify that you see a bunch of pretty graphs.
Everything pass? Great – onward!

Nagios Template Tweaks

There are several additions / tweaks that are recommended for a more efficient experience.

Set the default page to the services detail page (may want to limit results or show a different page on a large installation)

[root]$ vim /usr/local/nagios/share/index.php

Find where $url is defined, and change it to something more useful.

$url="cgi-bin/status.cgi?host=all&limit=0";

Also lets add some links to the sidebar – specifically the graphs.

[root]$ vim /usr/local/nagios/share/side.php

Add this section wherever you see fit.

<div class="navsection">
<div class="navsectiontitle">External Tools</div>
<div class="navsectionlinks">
<ul class="navsectionlinks">
<li><a href="/nagios/stats" target="<?php echo $link_target;?>">Nagiostats</a></li>
<li><a href="/nagiosgraph/cgi-bin/show.cgi" target="<?php echo $link_target;?>">Nagiosgraph</a></li>
</ul>
</div>
</div>

Conclusion

You now have a working base Nagios install. Now you are off to build your service checks, find more plugins, and tweak your alerts. I can almost feel your anticipation!

This entry was posted in Guides and tagged , on by .

About Andrew Wells

I have been developing on the LAMP stack since about 2006. I run Ubuntu XFCE on my desktop and have a history of managing Ubuntu and CentOS servers. I code web applications mostly in PHP but have experience with other languages as well. When I'm not working, I can be found working in my home lab or out snowboarding, hiking, camping, or biking depending on the season.

Leave a Reply

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