How to use Raspberry Pi to monitor network? (Nagios)

1. Install Nagios on your Raspberry Pi:

a. Download the latest version of Nagios from the official website.

b. Extract the downloaded file and move it to the /opt directory.

c. Change the ownership of the extracted folder to the nagios user.

d. Install the necessary dependencies for Nagios.

2. Configure Nagios:

a. Create a new user and group for Nagios.

b. Create a new configuration file for Nagios.

c. Configure the Nagios server to monitor the network.

3. Install Plugins:

a. Download the latest version of the Nagios Plugins from the official website.

b. Extract the downloaded file and move it to the /opt directory.

c. Change the ownership of the extracted folder to the nagios user.

d. Install the necessary dependencies for the plugins.

4. Start Nagios:

a. Start the Nagios service.

b. Access the Nagios web interface.

c. Configure the network devices to be monitored.

d. Monitor the network devices.

Today I wanted to use my Raspberry Pi to monitor my network with Nagios.
But I was lost trying to find out what was the best option to do this.
I saw many how-to guides about NEMS, Nagios3 but nothing perfect with the latest Nagios version.
I’ll explain here what I found and how to do the same step by step.

Nagios requires a few prerequisites: Raspberry Pi OS, Apache and PHP.
Then Nagios Core and its plugins can be installed from source, by downloading the latest version on the official website.
After that, the configuration can start to add all the hosts and services to monitor.

Yes, Nagios from source is the best way to monitor your network on Raspberry Pi.
Nagios is like a failure detector, it will check one thing regularly (like every 5 minutes) and alert you when this check doesn’t work as expected.

In this post I’ll explain how to install Nagios on your Raspberry Pi to check your network status.
But I’ll start by listing all the ways to get Nagios I found on Internet and to show you why most of them are useless or unsuitable.

What is the best way to get Nagios on Pi?

I’ll start this post by explaining to you what I found to get Nagios quickly on the Raspberry Pi, and why they are bad options for you.

NagiosPi?

My first search showed me “NagiosPi”.
I was excited to see that Nagios created a specific version for the Raspberry Pi.
This is the main reason that led me to install it on my Pi.
But I was quickly cooled, seeing this is a side project, with no update since 2013.

This was not suitable for me, I know that Nagios releases new updates at least once a year, so I want something recent.

APT?

Next step, I looked into apt repository what I could get.
Did the guy from NagiosPi stopped his project because Raspbian added the Nagios package in apt?
Maybe, but I only find Nagios 3 in the apt sources, the latest version when I wrote this line was Nagios 4.4.6.

It could be the easiest way to get Nagios, but I want the latest version or not too far from it.

NEMS?

Next search, I found a pre-configured SD card with Nagios and a lot of monitoring tools.
This is the NEMS Linux project, NEMS stands for Nagios Enterprise Monitoring Server.
Seems interesting no?

Yes, I tried it, but this was not suitable for Raspberry Pi.
Even when doing nothing, I have a lot of load, and couldn’t do anything without lag.
Maybe too many tools for the Raspberry Pi.

load average on nems

From sources?

The last option I was thinking about was to install Nagios manually from the sources.
But I thought this will be difficult and time-consuming to compile everything.

Hum … not so long finally, maybe 30min to get everything working with the latest version.
So for me, it’s really the best option I found, and I’ll now explain to you how to install it on your Raspberry Pi.

How to install Nagios on your Raspberry Pi

Install Raspberry Pi OS

First thing you need, is an operating system ?
Start with Raspbian, I think Raspbian Lite is perfect for this goal, but take the Desktop version if you prefer.

Here are the first steps you need to complete before going further:

That’s it, we are ready to start.

Downloads and prerequisites

Nagios is a web interface, so we need to install Apache and PHP:
sudo apt install apache2 libapache2-mod-php

Then there are two things we need to download and install:

  • Nagios Core: It’s the main part of the Nagios system.
  • Nagios Plugins:  This will add basic check commands to Nagios, so you’ll not have to create all the scripts yourself.

We need to download sources for them, check their websites for the latest version:
Nagios Core downloads
Nagios Plugins downloads

Finally, download archives with wget:
cd ~
mkdir Downloads
cd Downloads
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.6.tar.gz
wget http://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz

Now we are ready to move to the installation process.

Install Nagios on Raspberry Pi

Create Nagios user and group

We need to create a specific user for Nagios, and a group for Nagios and Apache users (nagcmd):
sudo useradd -m -s /bin/bash nagios
sudo groupadd nagcmd
sudo usermod -a -G nagcmd nagios
sudo usermod -a -G nagcmd www-data

Compile and install Nagios

Now we have to extract files from the archive, compile the sources and install Nagios on the Raspberry Pi:

  • Move to the “Downloads” directory:
    cd /home/pi/Downloads
  • Extract files from the archive downloaded before:
    tar zxvf nagios-4.4.6.tar.gz
  • Don’t forget to change the version number if you download another one.
  • Compile :
    cd nagios-4.4.6/
    ./configure --with-command-group=nagcmd
    make all

    This could take a few minutes.
  • Install:
    sudo make install
    sudo make install-init
    sudo make install-config
    sudo make install-commandmode

This is the end of the Nagios core installation.
We now need to configure the Apache part to allow access to the Nagios page.

Configure Apache for Nagios

  • Enable the CGI module for Apache:
    sudo a2enmod cgi
  • Copy the Apache configuration to the Apache folder:
    sudo cp sample-config/httpd.conf /etc/apache2/sites-enabled/nagios.conf
  • Create the first user for the web interface:
    sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
  • Restart Apache:
    sudo service apache2 restart
  • Check if you have access to Nagios:
    Go to http://<RASPBERRY IP>/nagios
    (check this tutorial if you don’t know how to find the current IP address).
    You should get something like this:

    It’s normal, we’ll fix that now.

The Nagios installation is almost done, we just need to run the Nagios service.

Start the Nagios service

Nagios has a service running in background to launch a check at a predefined frequency (every 5min for example).
We need to start it to finish the installation:
sudo service nagios start

And to start Nagios automatically on boot:
sudo ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios

If you refresh the web interface, you should now see “Daemon running with PID XXX”.
Everything is fine with Nagios, we just want to add plugins now.

Install Nagios Plugins

Follow this procedure to install the Nagios plugins:

  • Get back to the Downloads folder:
    cd /home/pi/Downloads
  • Extract files from the archive:
    tar zxvf nagios-plugins-2.3.3.tar.gz
  • Compile and install:
    cd nagios-plugins-2.3.3/
    ./configure --with-nagios-user=nagios --with-nagios-group=nagios
    make
    sudo make install

All plugins are now available in the /usr/local/nagios/libexec/ folder and ready to use in Nagios.

You may need to restart Nagios to apply changes:
sudo service nagios restart

How to configure Nagios to fit your needs?

Nagios vocabulary

Nagios uses a specific vocabulary to define each object in the configuration
So first, I have to be sure you know it:

  • Command: a command is basically a script from the Nagios plugins folder, we define it in Nagios to use it later with parameters (ex: check_load is a command).
  • Service: a service is a check that applies to one or more computers, and that uses a command to run a particular verification (ex: check if the load is above 10 on Raspberry Pi is a service).
  • Host: a host is a computer, a server, anything with an IP address.
  • Hostgroup: you can define a host group, like “Raspberry Pi”, and apply the same services to all the hosts inside this group.

We’ll now see how to configure each one in the Nagios configuration file.
For the example I’ll monitor my Raspberry Pi Zero from the Raspberry Pi where Nagios is installed.

Prepare the host

Nagios uses mainly SNMP to connect to other hosts and get all the information.
So we need to install SNMP on every new host before adding it in the configuration file.

This is not mandatory, it depends on which services you’ll create for this host.
Anyway, I’ll show you how to do it, so you have a complete tutorial.

In my case, I followed these steps on the Raspberry Pi Zero:

  • Connect to the host via SSH.
  • Install snmpd:
    sudo apt install snmpd
  • Move to the configuration folder:
    cd /etc/snmp
  • Remove (or backup) the snmpd.conf file:
    sudo rm snmpd.conf
  • Create a new snmpd.conf file:
    sudo nano snmpd.conf
  • Paste these lines into:
    com2sec readonly default RASPBERRYTIPS
    group MyROGroup v1 readonly
    group MyROGroup v2c readonly
    view all included .1 80
    access MyROGroup "" any noauth exact all none none
    sysName Pi Zero

    Replace RASPBERRYTIPS by the community you want to use.
    Replace sysName by your host name or description.
    There are many options available in snmpd.conf, but for this test we don’t need more than that.
  • Save and exit (CTRL+O, CTRL+X)

  • Restart snmpd

    sudo service snmpd restart

We can now add this host in Nagios.

Add a host

  • Go back to the Raspberry Pi hosting Nagios.
  • Move to the objects configuration folder of Nagios:
    cd /usr/local/nagios/etc/objects
  • Create a new file for your host, like pizero.cfg:
    sudo nano pizero.cfg
  • Paste these lines into:
    define host {
    use linux-server  ; Host group to use
    host_name Pi Zero  ; Name of this host
    alias pizero  ; Alias
    address 192.168.1.18  ; IP Address
    }

    This is really a basic host definition, with the host name, alias and IP address.
    We also add it to the Linux servers host group to inherit group-defined services if you add some later

  • Save and exit (CTRL+O, CTRL+X).
  • Now we need to tell Nagios that we added a new file.
    Add this line in /usr/local/nagios/etc/nagios.cfg (just under localhost.cfg will be perfect) .
    cfg_file=/usr/local/nagios/etc/objects/pizero.cfg
  • Restart Nagios:
    sudo service nagios restart
  • Go back to the Nagios web interface, in the Hosts menu.
    You should have a new line like this:
    nagios add host

Nagios already monitors the ping response for our new host, but we want more.
So we need to add a service inside, but first we need to define the command to use.

Add a command

Previously in this tutorial you downloaded and installed Nagios plugins
Nagios plugins provide many commands to check the local host, but not so much for remote hosts.
You can also download many other commands on this website (specifically look for SNMP plugins).

For this example, I’ll monitor the time synchronization between my two Raspberry Pi.

To add a command in Nagios, follow these steps:

The command is now ready to be used for your host, but we need to create a new service to apply it.

Add a service

To create a new service, follow these steps:

  • Open your host configuration file:
    sudo nano /usr/local/nagios/etc/objects/pizero.cfg
    Adapt the filename to what you have created before
  • Paste these lines:
    define service {
    use local-service  ; Name of service template to use
    host_name Pi Zero
    service_description Time synchronization
    check_command check-ntp!1!5
    }

    Don’t forget to adapt these lines to your previous configuration.
    On the check_command line, we define all arguments, separated by exclamation points

  • Save and exit.
  • Check your configuration:
    sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
    This could be useful to know if you have configuration errors before restarting Nagios.
    In my case, I wrote check_ntp instead of check-ntp ?
  • Restart Nagios:
    sudo service nagios restart

Now in the status detail from your host you should see the new service.

nagios service detail

If you understood how to create a host, a command and a service, you are now ready to do anything in Nagios.
Feel free to download new plugins on Internet, or even code your plugins (it’s nothing more than a basic script to check something).

Video

Related questions

Is it possible to create the configuration from the web interface?  Unfortunately no, Nagios doesn’t provide a configuration tool in the Core version. But there are several configuration tools you could install. The one I often use is Thruk, I didn’t try it yet on Raspberry Pi, but you can download it here if you want.

Is it possible to monitor Windows or other hosts? Yes. You could monitor anything on the network. SNMP is also available for Windows and most of the network equipments (router, switch, home automation device, …).  As you can create your check commands, everything is possible.

If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!

Conclusion

That’s it, you know how to install and configure a Nagios server on your Raspberry Pi to monitor your network or anything you want.
With this tutorial, you get the latest Nagios version, with no excessive load average (currently 0.00 on my Pi ^^).

Once Nagios is installed, it’s not really different from any Nagios server on Linux.
So feel free to check Linux tutorials for advanced configuration.

And if you want to create easy-to-read graphs with the data generated in Nagios, take a look at how to install Grafana on your Raspberry Pi.

Additional Resources

Not sure where to start?
Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Watch the Raspberry Pi Bootcamp course now.

Master your Raspberry Pi in 30 days
Don’t want the basic stuff only? If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.
Download the e-book.

VIP Community
If you just want to hang out with me and other Raspberry Pi fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Create, understand, and improve any Python script for your Raspberry Pi.
Learn the essentials step-by-step without losing time understanding useless concepts.
Get the e-book now.

You can also find all my recommendations for tools and hardware on this page.

How to Use Raspberry Pi to Monitor Network with Nagios

Monitoring your network is an important part of keeping your system secure and running smoothly. With the Raspberry Pi, you can use the open source Nagios monitoring system to keep an eye on your network. Here’s how to get started.

Step 1: Install Nagios

The first step is to install Nagios on your Raspberry Pi. You can do this by running the following command:

sudo apt-get install nagios3

Once the installation is complete, you’ll need to configure Nagios. To do this, edit the configuration file located at /etc/nagios3/nagios.cfg.

Step 2: Configure Nagios

In the configuration file, you’ll need to set up the hosts and services that you want to monitor. You can do this by adding the following lines to the file:

define host {
    host_name    raspberrypi
    address      192.168.1.1
    check_command check-host-alive
}

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

This will set up a host called “raspberrypi” with an IP address of 192.168.1.1, and a service called “PING” that will check the host’s ping response time. You can add additional hosts and services as needed.

Step 3: Start Nagios

Once you’ve configured Nagios, you can start it by running the following command:

sudo service nagios3 start

You can then access the Nagios web interface by pointing your browser to http://localhost/nagios3.

Step 4: Monitor Your Network

Once you’ve set up Nagios, you can start monitoring your network. You can view the status of your hosts and services, as well as set up alerts for when something goes wrong. You can also use Nagios to generate reports and graphs to help you better understand your network.

Conclusion

Using the Raspberry Pi and Nagios, you can easily monitor your network and keep an eye on any potential problems. With a few simple steps, you can set up Nagios and start monitoring your network in no time.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?