Setup Raspberry Pi 4 as Server Monitor

In this guide I will show you step by step how to setup raspberry pi with Nagios running to monitor your servers/switches/firewalls/printers/etc. Hope you learn something of value!

Purchasing Raspberry Pi

The first step is to obviously purchase all of the required equipment. I recommend purchasing the following from CanaKit. It will get you the newest Raspberry pi 4 as well as a case (recommended) and all of the passive cooling fins to help keep it running as cool as possible. The kit is also quite nice since it comes with an on/off switch as well as a mini-HDMI to HDMI output cable.

Raspberry Pi 4 Canakit
Raspberry Pi 4 CanaKit

Unfortunately the one thing that does not come with the kit is a storage option. If you already have a micro SD card laying around you should be able to format it correctly. I opted for a 32GB since it was on sale for me at the time and I plan to use the Pi to store some bigger files as well. Also you will need an SD adapter for microSD as shown below so that you can format the card with your computer. I left the link here:

Samsung 32 GB SD Card
Samsung Micro SD Card

The only other thing that you will need to purchase if you do not have already is a monitor, keyboard, mouse, and a spare computer to configure the pi. If you already have all of these things then you should be good to go.

Initial Raspberry Pi Setup

The first thing you will want to do is unpack everything from the CanaKit and lay it all out. You will have your raspberry pi as well as tiny little aluminum fins that will be used for cooling. On the backside of these is an adhesive strip that you can use to place them in the orientation shown below. This will help to keep your board at lower temperatures.

Then once that is done, you simply put the pi inside of the CanaKit case and plug in the usb-c power adapter. There should be a red light on your raspberry pi if it is turned on. Next we will need to format the SD card to properly boot up NOOBS to install raspbian.

Downloading NOOBS

Before we can begin to setup your Raspberry Pi with nagios we will need to get an operating system running. A very easy way to do this is to download NOOBS from their official site here.

Once you have the Zip file downloaded make sure to extract it so we can move on to the next step. This download will take a little bit of time depending on your internet connection since it is approximately 1GB.

Formatting the Micro SD Card

You will need to format your SD Card first before you can load your NOOB files onto it. To do this you will plug your micro SD into the SD card adapter and then into your laptop so it can be read. At this point your computer will recognize the card and it will show up as a drive.

Windows:

Open up Disk Management and you will see a list of all of your available drives. At the bottom of the window you will see a list of all your disks that can be formatted. Find your SD Card and right click on the blue area and select the Format option as shown here:

Select Yes that you would like to reformat the drive and then input the correct formatting options. The label you can name whatever you like, but then select FAT32 as the file system and unit size as default. Performing a quick format will be perfectly fine for this step as well. Then you can click Okay and it will quickly format the SD card so you can move on to the next step of loading NOOBS.

Mac:

If you are on a Mac you will want to follow similar steps as Windows but just use a different application to format the drive. So firstly, plug in your micro SD into its SD adapter and then into your computer. Once it is recognized and mounted onto your system you can open up disk utility. Find your drive on the left hand size and select the Erase option at the top. Name it whatever you would like and make sure to set the format option to MS-DOS FAT. Then once this is done you can move on to the next step.

Install NOOBS and Raspbian

Now that your SD card is formatted properly there is a very easy step that must take place. You will simply copy everything that was in the .zip download of NOOBS and paste it onto your fresh SD card that you just formatted. From there you can just eject the drive from your computer and put it into your Raspberry Pi and turn it on.

After your Raspberry Pi boots up you will be shown a screen with different OS options to install. Just check the Raspbian OS and then select install. This will do all of the required install for you and will take a moment. After you are all setup you will be brought to your desktop view with everything in place.

Installing Nagios

Now we can finally being to setup Raspberry Pi with Nagios in these final steps. From here it is a matter of installing and configuring Nagios as shown from their official site. To start you will want to make sure your OS is all up to date. Run the commands below to make sure of this:

sudo apt-get update
sudo apt-get upgrade

To make things easier you will want to become a root user so that you have rights to use the following commands. To do this on Raspbian you will enter:

sudo -i

You will then need to make sure that you install all of the required packages for nagios to operate properly and to finish the rest of this tutorial.

apt-get install -y autoconf gcc libc6 make wget unzip apache2 apache2-utils php libgd-dev

Next you will need to download the actual nagios core source code and extract it.

cd /tmp
wget -O nagioscore.tar.gz https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.4.3.tar.gz
tar xzf nagioscore.tar.gz

Then compile this code with the following:

cd /tmp/nagioscore-nagios-4.4.3/
./configure --with-httpd-conf=/etc/apache2/sites-enabled
make all

Next you will need to create a nagios group and give Nagios permission as well as Apache permission to the files:

make install-groups-users
usermod -a -G nagios www-data

Install Binaries and Service Daemon this will install all of the required files for nagios to display properly as well as make the service start when turned on.

make install
make install-daemoninit

Next you will need to install the command files and sample configuration files for adding more hosts to Nagios in the future.

make install-commandmode
make install-config

Next you will need to install apache configuration files and settings that allow apache to run properly.

make install-webconf
a2enmod rewrite
a2enmod cgi

Since Nagios will be using authentication by default with this method you will need to create an apache user. This command will create an account called nagiosadmin and allow you to specify a password to use. Note that when typing your password in the terminal it will look like nothing is happening, just make sure to hit enter to confirm.

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

Now you will need to restart both the Apache and Nagios services.

systemctl restart apache2.service
systemctl start nagios.service

Now both of the services should be up and running and Nagios should be working normally. Before we can test this we will need to get your IP address. Firstly, open up a terminal and type in:

ifconfig

A list of your interfaces should be shown on the left hand side, if you are wired the interface you would look at would be the eth0, and if using wifi then wlan0. Under these interfaces there should be a section labeled inet: that will follow with your ip address. Remember this IP for the next testing steps.

Testing NagiosCore

Test whether you can view Nagios by going to your browser and typing in that IP address you found in the previous step followed by /nagios. For example:

http://192.168.1.31/nagios

This will bring up a login for you to authenticate, remember that the username is nagiosadmin and the password is whatever you set it to. Once you login you should be brought to the welcome screen and a side panel that has multiple tabs for monitoring services and hosts. In the next step we will install the nagios plugins so that these services will show up properly!

Installing Nagios Plugins

Firstly, you need to make sure that you have all the required packages installed. Run the following command to do this.

apt-get install -y autoconf gcc libc6 libmcrypt-dev make libssl-dev wget bc gawk dc build-essential snmp libnet-snmp-perl gettext

Then simply download the nagios plugins source code:

cd /tmp
wget --no-check-certificate -O nagios-plugins.tar.gz https://github.com/nagios-plugins/nagios-plugins/archive/release-2.2.1.tar.gz
tar zxf nagios-plugins.tar.gz

Then Compile and install them:

cd /tmp/nagios-plugins-release-2.2.1/
./tools/setup
./configure
make
make install

Keeping Raspbian Awake

Now since I plan to use my raspberry pi as a constant server monitor I want the screen to be displayed 24/7 and not have it black screen on me constantly. To do this there is a very simple way, I have a previous post showing this that takes literally 3 seconds.

Share with others!

Leave a Reply