Friday, 1 April 2016

Installing Nagios 4 on a Raspberry Pi

The following post is a section of the book 'Just Enough Linux'.  The entire book can be downloaded in pdf format for free from Leanpub or you can read it online here.
Since this post is a snapshot in time. I recommend that you download a copy of the book which is updated frequently to improve and expand the content.
---------------------------------------

Historically, installation of Nagios on a Raspberry Pi has been somewhat ‘problematic’. The reason being that while Nagios has been compiled to work with a wide range of different computer types, installation onto the ARM architecture used by the Pi has not been common. There has been good work done to pre-compile and distribute images for use, but in may cases these do not support the later versions of Raspbian and they take some of the ‘mystery’ and learning away from the process.
What follows is heavily drawn from the good work of Farooq Mohammed Ahmed who has been a stalwart of a range of open source topics including the Raspberry Pi and Nagios.
First we need to set ourselves as the root user for all the following commands;
We should be able to see the immediate difference as the command prompt changes to indicate that we are now operating as root;
pi@raspberrypi:~ $ sudo -i
root@raspberrypi:~#

Get the required supporting packages

We need to download and install the required packages via apt-get install.
We will be informed of the additional packages that will also need to be installed and asked to agree to the process. Once we agree it will start and work its way through the various applications.
Then we need to create a ‘nagios’ user using useradd and then set the password using passwd (choose something appropriate and note it down somewhere).
Now we create a group ‘nagcmd’ (using groupadd) to facilitate external commands via the Web User Interface and then add both Nagios (‘nagios’) and Apache (‘www-data’) users to it with the usermod command.
Now we can download Nagios Core and associated plugins using wget to the /tmp directory. The latest current versions are Nagios 4.1.1 and Nagios Plugins 2.1.1. We’ll do this in the /tmp directory to keep out of the way of the rest of the system during the installation (hence the cd into /tmp).
Once these have finished downloading we need to un-bundle (tar) and decompress the files;
Once this is complete we will have two folders with the name nagios-4.1.1 and nagios-plugins-2.1.1

Compile and configure Nagios Core

First we will start with Nagios Core install by changing into the appropriate directory;
Now we’re going to compile the source and install it. All the files from this process will go into /usr/local/nagios/ directory.
This process will whistle through a considerable amount of processing and then display a configuration summary.
*** Configuration summary for nagios 4.1.1 08-19-2015 ***:

 General Options:
 -------------------------
        Nagios executable:  nagios
        Nagios user/group:  nagios,nagios
       Command user/group:  nagios,nagcmd
             Event Broker:  yes
        Install ${prefix}:  /usr/local/nagios
    Install ${includedir}:  /usr/local/nagios/include/nagios
                Lock file:  ${prefix}/var/nagios.lock
   Check result directory:  ${prefix}/var/spool/checkresults
           Init directory:  /etc/init.d
  Apache conf.d directory:  /etc/httpd/conf.d
             Mail program:  /bin/mail
                  Host OS:  linux-gnu
          IOBroker Method:  epoll

 Web Interface Options:
 ------------------------
                 HTML URL:  http://localhost/nagios/
                  CGI URL:  http://localhost/nagios/cgi-bin/
 Traceroute (used by WAP):  /usr/sbin/traceroute


Review the options above for accuracy.  If they look okay,
type 'make all' to compile the main program and CGIs.
As the final portion of the output states, assuming that the configuration looks OK we can then go ahead and start the make process. This will carry out the process of building the executable programs and libraries from the available source code.
This is a complex process that will take some time to complete (especially on a ‘Pi’) so prepare to be patient while it works through the required computation.
Once successfully complete we will continue with installing Nagios as follows;
  • 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-config : This installs sample config files in /usr/local/nagios/etc
  • make install-commandmode : This installs and configures permissions on the directory for holding the external command file
Now we need to install and configure web access to Nagios via our web server apache. First copy the appropriate files and adjust permissions;
Then make the directory for the httpd.conf file by creating the /etc/httpd/conf.d/nagios.conf directory;
And then make install-webconf to install the Apache config file for the Nagios web interface
Now we can create a user and a password to use to access the Nagios Web User Interface via the Apache HTTP server. The name ‘nagiosadmin’ used below can be substituted for an alternative if desired.
We will be prompted (twice) for a new password for the ‘nagiosadmin’ user. Note it down somewhere appropriate.
Once that is done we need to restart the Apache service

Compile and configure Nagios plugins

Now we need to install the plugins for Nagios by first changing to the nagios-plugins-2.1.1 directory;
Then we compile and install the plugins in a similar way to the process we used earlier on Nagios Core. Firstly we compile the appropriate executables from the source;
As with the previous effort this will proceed through a considerable amount of processing. Then we can carry out the make process on the installation;
Once complete we need to ensure that the Nagios service starts up at runtime when the system boots up by creating a link from nagiosin the init.d directory;
At this point we should have successfully installed Nagios. We should verify this by checking for errors;
An output should be produced that looks a little like the following;
Nagios Core 4.1.1
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 08-19-2015
License: GPL

Website: https://www.nagios.org
Reading configuration data...
   Read main config file okay...
   Read object config files okay...

Running pre-flight check on configuration data...

Checking objects...
        Checked 8 services.
        Checked 1 hosts.
        Checked 1 host groups.
        Checked 0 service groups.
        Checked 1 contacts.
        Checked 1 contact groups.
        Checked 24 commands.
        Checked 5 time periods.
        Checked 0 host escalations.
        Checked 0 service escalations.
Checking for circular paths...
        Checked 1 hosts
        Checked 0 service dependencies
        Checked 0 host dependencies
        Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check
Per the final line, it looks as if everything has gone smoothly.

Configure Nagios as a service

Now we can create a nagios.service file with the file editor ‘nano’ (or the editor of your choice) with the following content. This will allow us to start and stop Nagios in a standardised way.
[Unit]
Description=Nagios
BindTo=network.target

[Install]
WantedBy=multi-user.target

[Service]
User=nagios
Group=nagios
Type=simple
ExecStart=/usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg
To enable the cgi links from the Nagios Web UI we move the cgi.load file as follows;
Then we can restart the Apache web service;
If no errors are reported then we can start the Nagios service.
At this point Nagios should be running, but it’s worth checking to see if there are any reported problems;
If everything has gone smoothly we should see a report similar to the following;
● nagios.service - Nagios
   Loaded: loaded (/etc/systemd/system/nagios.service; enabled)
   Active: active (running) since Sat 2016-03-26 14:33:15 NZDT; 1min 17s ago
 Main PID: 1619 (nagios)
   CGroup: /system.slice/nagios.service
           ├─1619 /usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.c...
           ├─1620 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var...
           ├─1621 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var...
           ├─1622 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var...
           ├─1623 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var...
           ├─1624 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var...
           ├─1625 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var...
           └─1626 /usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.c...

Mar 26 14:33:15 raspberrypi nagios[1619]: nerd: Fully initialized and ready ...!
Mar 26 14:33:15 raspberrypi nagios[1619]: wproc: Successfully registered man...r
Mar 26 14:33:15 raspberrypi nagios[1619]: wproc: Registry request: name=Core...0
Mar 26 14:33:15 raspberrypi nagios[1619]: wproc: Registry request: name=Core...3
Mar 26 14:33:15 raspberrypi nagios[1619]: wproc: Registry request: name=Core...1
Mar 26 14:33:15 raspberrypi nagios[1619]: wproc: Registry request: name=Core...4
Mar 26 14:33:15 raspberrypi nagios[1619]: wproc: Registry request: name=Core...2
Mar 26 14:33:15 raspberrypi nagios[1619]: wproc: Registry request: name=Core...5
Mar 26 14:33:17 raspberrypi nagios[1619]: Successfully launched command file...6
Mar 26 14:33:17 raspberrypi nagios[1619]: Successfully launched command file...6
Hint: Some lines were ellipsized, use -l to show in full.

Access the Nagios Web Interface

We are now ready to access the Nagios Web User Interface. We can do this by using the static IP address that we set up for our Pi at the following URL http://10.1.1.230/nagios. This can be accessed from a machine (our Windows desktop for example) by simply typing it into the browser.
(Remembering that the IP address that you enter here needs to be your address. Not necessarily 10.1.1.230.)
When we first enter this in our browser we will be asked to authenticate ourselves;
Login
We need to enter the username and password that we set up earlier to access the Web interface. The one we used in the example was ‘nagiosadmin’. Once entered we should see the front page of our Nagios page looking a little like the following;
Nagios Start Page
Congratulations!
We’ve managed to successfully install Nagios on a Raspberry Pi! Now all we need to do is to use it :-)


The post above (and heaps of other stuff) is in the book 'Just Enough Linux' that can be downloaded for free (or donate if you really want to :-)).

15 comments:

  1. Excellent!
    Without any issues I was able to follow your guide.
    Now for me to see what I can do with Nagios.

    Thanks!

    ReplyDelete
  2. I am trying to run the commands but when i authenticate i get an internal server error what is that i am not doing correct.

    ReplyDelete
    Replies
    1. I'm not sure what would be causing the problem. The best I could recommend is to re-try the installation and see if there is something different in the process that might be causing the problem.

      Delete
  3. I wonder if you can help I still get Not Found

    The requested URL /nagios/< was not found on this server.

    ReplyDelete
    Replies
    1. Hi. That would tend to indicate that the web server is running, but for some other reason the Nagios page isn't operative. I can't really suggest why unfortunately. Try a repeat of the install and see if the problem is consistent.

      Delete
  4. Works great for me! Thanks for the guide!

    ReplyDelete
  5. Works great for me! Thanks for the guide!

    ReplyDelete
  6. Hi!

    This is useless:
    mkdir /etc/httpd
    mkdir /etc/httpd/conf.d
    mkdir /etc/httpd/conf.d/nagios.conf
    make install-webconf

    You do the config with:
    install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-enabled/nagios.conf
    That is all you need. It writes the Config to /etc/apache2/sites-enabled/nagios.conf.
    The Path under /etc/httpd/conf.d is old and inactive stuff.
    Read here, Point 10 (Sorry, is in german): https://help.ubuntu.com/lts/serverguide/httpd.html

    ReplyDelete
    Replies
    1. Thanks! That's a great link and the good news is that Ubuntu appears to auto translate it :-). I should clearly go through the instructions and update them.

      Delete
  7. Good tutorial, perfect function

    ReplyDelete
  8. root@mackapi:~# apt-get install apache2 libapache2-mod-php5 build-essential libgd2-xpm-dev
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Note, selecting 'libgd-dev' instead of 'libgd2-xpm-dev'
    Package libapache2-mod-php5 is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    E: Package 'libapache2-mod-php5' has no installation candidate
    root@mackapi:~#

    anyone hit this issue?

    ReplyDelete
    Replies
    1. This has occurred with the transition from using PHP 5 to PHP 7 with Raspbian. There are some different ways to approach resolving this outlined here https://www.raspberrypi.org/forums/viewtopic.php?t=195818

      Delete
  9. Great turorial, even applies tot latest version of Nagios, just need to change the filenames

    ReplyDelete
  10. Hello. Everything went ok but upon connecting to my Nagios, all i can see is the... page's source code

    ReplyDelete
    Replies
    1. Please accept my apologies. This book and the versions of software that it is referencing is pretty old. I suspect that is where things haven't gone according to the instructions. Over time I have moved away from Nagios to a Prometheus / Grafana stack which I have found a lot more flexible. If you are interested in that option you could check out the following - https://leanpub.com/rpcmonitor

      Delete