Nagios 4 on a Raspberry Pi

Introduction

As a low power device the Raspberry Pi makes for a perfect network monitoring host. I’m running Nagios 4 on a Raspberry Pi 2 with a 5V/2A power supply.

Fixed IP

A fixed IP address is desirable to easily locate the Pi in our LAN and access it over SSH and HTTP. Mine is at 192.168.2.69 (outside the DHCP range) in my network (192.168.2.0).

/etc/network/interfaces

auto lo
iface lo inet loopback
 
auto eth0
iface eth0 inet static
address 192.168.2.69
netmask 255.255.255.0
network 192.168.2.0
broadcast 192.168.2.255
gateway 192.168.2.1
 
auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
 
auto wlan1
allow-hotplug wlan1
iface wlan1 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Install required packages

Nagios needs a web server running PHP, the GNU build tools and the GD Graphics Library.

sudo apt-get install apache2 php5 libapache2-mod-php5 build-essential libgd2-xpm-dev

Users and groups

We need a user ‘nagios’ and a group ‘nagcmd’ for allowing commands over the web interface. The Apache user (www-data) needs to be added to this group too.

sudo useradd -m -s /bin/bash nagios
sudo groupadd nagcmd
sudo usermod -a -G nagcmd nagios
sudo usermod -a -G nagcmd www-data

Optionally set a password for the ‘nagios’ user:

sudo passwd nagios

Get the source tarballs

At the time of this writing Nagios is at version 4.1.1 and the plugins are at 2.2.1.

wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-4.1.1.tar.gz
wget http://nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz
tar zxvf nagios-4.1.1.tar.gz
tar zxvf nagios-plugins-2.1.1.tar.gz

Build and install Nagios

Build the required targets and install Nagios in /usr/local/nagios/ .

cd nagios-4.1.1
./configure --with-command-group=nagcmd
make all
sudo mkdir -p /etc/httpd/conf.d
sudo make install{,-init,-commandmode,-webconf}
sudo make install install-config

Create an administrative web interface user and set a password:

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

Reload Apache:

sudo /etc/init.d/apache2 reload

Build and install the plugins

Change to the plugin sources directory. Build and install all plugins.

cd ../nagios-plugins-2.1.1
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
sudo make install

Start Nagios at boot time

Create a symbolic link as an rc hook to run Nagios on system startup (is rcS.d truly the best place?):

ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios

Verify the installation

Check if all is well with your Nagios configuration:

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

Start the Nagios service:

sudo /etc/init.d/nagios start

Log in to the web interface. (Of course, use the IP address or hostname of your Raspberry Pi.) In my example I surf to: http://192.168.2.69/nagios

Screenshot from 2015-09-13 12:53:08

Configure Nagios

Configuring Nagios for monitoring is far beyond the scope of this post. Consult the documentation for all the details. Yet, let’s define some simple objects.

The main configuration can be found in /usr/local/nagios/etc/. The nagios.cfg file contains references to individual object configurations (cfg_file) and object configuration directories (cfg_dir).

Create a directory:

cd /usr/local/nagios/etc
sudo mkdir conf.d 
sudo chown nagios conf.d

Add this line to nagios.cfg:

cfg_dir=/usr/local/nagios/etc/conf.d

We have a Raspberry Pi running a web server at 192.168.2.10. We want to check if it’s reachable and if the HTTP port is available:

/etc/local/nagios/etc/conf.d/raspberrypi.cfg:

define host {
        use                     linux-server
        host_name               raspberrypi
        address                 192.168.2.10
        hostgroups              linux-servers
        notification_period     24x7
        }

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

define service{
        use                     local-service        
        host_name               raspberrypi
        service_description     HTTP
        check_command           check_http
        }

Now verify the new configuration:

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

Restart Nagios:

sudo /etc/init.d/nagios restart

Screen Shot 2015-09-13 at 2.13.23 PM

To receive e-mail notifications first set a valid e-mail address for nagiosadmin in objects/contacts.cfg
If you don’t (want to) have an MTA on your Raspberry Pi, you can use sendEmail.

sudo apt-get install sendemail

Use it as follows:

sendEmail -f from@domain.tld -t to@domain.tld -s smtp.domain.tld -u "Subject" -m "Message" -xu username -xp password

The message can be obtained via STDIN as well:

echo "Message" | sendEmail -f from@domain.tld -t to@domain.tld -s smtp.domain.tld -u "Subject" -xu username -xp password

Now edit the command definition in objects/commands.cfg to match the sendEmail command line. Restart Nagios.

# 'notify-host-by-email' command definition
define command{
        command_name    notify-host-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/sendEmail -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -t $CONTACTEMAIL$ -f noreply@domain.tld -s smtp.domain.tld -xu username -xp password
        }

That’s it for a basic Nagios setup.

4 thoughts on “Nagios 4 on a Raspberry Pi”

  1. What version of Raspbian did you use for this tutorial? When using Raspbian-Jessie, it starts to fail at “sudo make install{, -init, -commandmode, -webconf} and has serveral other failures, the biggest of which is the startup script doesn’t seem to work.

    1. Some spaces crept into that command line (my bad). Try: sudo make install{,-init,-commandmode,-webconf}

      Also create this directory if it doesn’t exist: sudo mkdir -p /etc/httpd/conf.d

      I’ve updated the post.

  2. Hello all,

    I’m trying to set up the Nagios 4 on a Raspberry pi (but I’m lucky/unlucky, it’s the RPI3).

    And the following command :
    “sudo make install {,-init, -commandmode, -webconf}”
    returns the following message (in french) :
    Ce programme est construit pour arm-unknown-linux-gnueabihf
    –This program is build for arm-unknown-linux-gnueabihf
    Signaler les anomalies à
    — Please inform

    I suppose that I need to define the arch as armv7l (or something like that …), but don’t know how …
    Could someone help me ?
    Thanks in advance !

Comments are closed.