Nagios 4.x Install from Source on Ubuntu 14.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 14.04 on my servers, so it’s time to create a guide to install Nagios 4 on Ubuntu 14.04. Apparently Nagios Core install scripts wern’t well tested for Debian systems. But in any case, it can be installed with a couple modifications.

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 14.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.1.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 http://downloads.sourceforge.net/project/nagios/nagios-4.x/nagios-4.1.1/nagios-4.1.1.tar.gz 
[root]$ tar -xzvf nagios-4.1.1.tar.gz
[root]$ wget http://www.nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz
[root]$ tar -xzvf nagios-plugins-2.1.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 libgd2-xpm-dev apache2 php5 apache2-utils build-essential unzip
[root]$ cd ~/nagios-4.1.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.18 libpq5 libradius1 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.1.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 http://downloads.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.14/nrpe-2.14.tar.gz
[root]$ tar -xzvf nrpe-2.14.tar.gz
[root]$ cd nrpe-2.14
[root]$ ./configure --with-ssl=/usr/bin/openssl --with-ssl-lib=/usr/lib/x86_64-linux-gnu
[root]$ make

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]$ ln -s /etc/init.d/nagios /etc/rc2.d/S20nagios

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

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

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.1.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.

20 thoughts on “Nagios 4.x Install from Source on Ubuntu 14.04

    1. Andrew Wells Post author

      Ah yes, forgot about this. This has something to do with how permissions differ in Apache 2.4.7.

      Relevant upgrade notes: http://httpd.apache.org/docs/2.4/upgrading.html

      I found that this section is causing this denied error message:

      <Directory />
          Options FollowSymLinks
          AllowOverride None
          Require all denied
      </Directory>
      

      You could technically change that to Require all granted, but that would create security issues. I would create a directory permissions specific to the nagiosgraph directory. I am currently unable to test how to do it, but I will update the article as soon as I can. In the meantime, if guides you to a solution, please post back. Thanks!

      Reply
  1. GRBerk

    Your guide was very helpful! I also got the security error with NagiosGraph, so I would really like to get a fix in place for it. I am a Linux novice, so getting around to do all of this stuff has been incredibly challenging for me. At least I’m getting a better understanding of the OS for it, though. ๐Ÿ™‚

    Reply
  2. Mike Dammer

    Is there already a manual to fix the 403 forbidden error on the nagiosgraphs? Or is there somebody that can tell me where to edit:

    Options FollowSymLinks
    AllowOverride None
    Require all denied

    Reply
  3. Ricky

    Your very first step is to set a sudo password, but if all you’re trying to do is run many commands as root, why not just leave the sudo account alone (recommended) and enter
    sudo su
    to elevate your prompt for the remainder of the session? I believe this is the recommended way, and generally people will freak out when you tell them to set a root password.

    Reply
    1. Andrew Wells Post author

      Yes, some people don’t like that. It’s a matter of preference though. Other distros have setting a root password as part of the setup process. I also get root passwords by default when provisioning Ubuntu VMs on cloud services. In the scope of this article, running sudo su would accomplish thing.

      Reply
      1. Ricky

        That depends on what you consider “nothing.” Educating users about best practices for security on the operating system they are using seems a ways away from that to me. Additionally, some people may not be aware of the fact that you can elevate an entire session without knowing the root password. Sure, it may not always be applicable, but good security practices should be practiced as often as possible.

        Either way, I thought the article was great with lots of helpful tips, like the bash alias. I have been running Nagios for years, and haven’t ever thought of that. Talk about doing things the hard way!

        Reply
  4. Barkingdoggy

    In Nagios 4.0.7, this template tweak no longer works, “Find where $corewindow is defined, and change it to something more useful.”

    What works for me is change the line
    $url = ‘main.php’;
    to
    $url = “cgi-bin/status.cgi?host=all&limit=0”;

    Reply
  5. Sven Martin Helgeland

    I’m getting: “CRITICAL – Plugin timed out while executing system call” when trying to use check_snmp plugin. Anyone have a tip for me to fix this?

    Reply
    1. Andrew Wells Post author

      Sven – I would run the command in the terminal to see how long it actually takes. Nagios has a built-in timeout value when executing plugins, so that will probably have to be increased.

      Reply
    2. Sven Martin Helgeland

      I found the solutions. This has to be done before nagios plugin is installed:
      apt-get install snmp snmp-mibs-downloader

      Reply
  6. Jay B

    If anyone is still getting the 402 Forbidden error, I fixed it on mine.
    In my nagiosgraph.conf I set it to:
    Options ExecCGI
    AllowOverride All
    Require all granted

    Note, I’m not 100% sure of the security impact by doing this, my server is in a closed environment so security is not an issue for me.

    Andrew, thanks for this article, it was very helpful.

    Reply
  7. Malintha Adikari

    Hi Andrew,

    I have multiple hosts to be monitored. I have installed NRPE client on them and configured. How can I add hosts to be monitored in my NAGIOS server machine. Could you please point me to such sample configurations

    Regards,
    Malintha

    Reply
  8. Joey

    I followed all the instructions on NagiosGraph but I get the

    Not Found

    The requested URL /nagiosgraph/cgi-bin/show.cgi was not found on this server.

    I was looking somewhere else and they said I need to add some code to /usr/local/nagiosgraph/etc/nagiosgraph-apache.conf
    but that file doesn’t exist. I even tried making a new file but that didn’t work. I’m very new to Nagios but how can I tell Apache about Nagios

    Thank You

    Reply
    1. Joey

      I even tried making changes to the /etc/apache2/sites-available/nagios.conf to point to /nagiosgraph/cgi but that just ended up breaking nagios so set it back:

      This is the code I am putting in my nagios.cfg in apache2/sites-available but didn’t work. Am I suppose to edit this file? If not, then how can I get it configured? I can’t even view a simple: https://server/nagiosgraph/cgi-bin/show.cgi

      # enable nagiosgraph CGI scripts
      ScriptAlias /nagiosgraph/cgi-bin “/usr/local/nagiosgraph/cgi”

      Options ExecCGI
      AllowOverride None
      Require all granted

      # enable nagiosgraph CSS and JavaScript
      Alias /nagiosgraph “/usr/local/nagiosgraph/share”

      Options None
      AllowOverride None
      Require all granted

      Reply

Leave a Reply

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