Saturday, August 1, 2020

Setting Up a Raspberry Pi 3 as a Home Network Monitor

I repurposed my Raspberry Pi 3 to monitor our home network. In the picture, it's the small black box underneath the white cable modem and to the right of the cable amplifier. I followed the steps outlined in the article "How to use Raspberry Pi to monitor network? (Nagios)" by Patrick Fromaget, at his website RaspberryTips. I ran into a couple of head-scratchers when I installed Pi OS Lite. Out of the box, the OS was configured for British keyboards, and it was a little tricky choosing the right options for my cheap wireless keyboard/trackpad. The other roadblock was that wi-fi was disabled. To enable it, I had to unblock it:

rfkill unblock wlan
shutdown -r now


I'd had some experience with Nagios, so I had no problems building and installing it from source, following Fromaget's instructions. I customized Nagios to monitor the temperature of the monitoring Pi. First, I added a /usr/local/nagios/libexec/check_cputemp executable. It's a shell script:

#!/bin/bash

cpu_lo_warn=-30
cpu_lo_crit=-40
cpu_up_warn=75
cpu_up_crit=85
cpu_temp=$(</sys/class/thermal/thermal_zone0/temp)
cpu_temp=$((cpu_temp/1000))

if [ -z $cpu_temp ]; then
  echo "UNKNOWN - Undefined CPU temperature"
  exit 3
fi
if [ $cpu_lo_crit -gt $cpu_temp ]; then
  echo "CRITICAL - CPU temperature $cpu_temp too cold"
  exit 2
fi
if [ $cpu_lo_warn -gt $cpu_temp ]; then
  echo "WARNING - Low CPU temperature $cpu_temp"
  exit 1
fi
if [ $cpu_temp -gt $cpu_up_crit ]; then
  echo "CRITICAL - CPU temperature $cpu_temp too hot"
  exit 2
fi
if [ $cpu_temp -gt $cpu_up_warn ]; then
  echo "WARNING - High CPU temperature $cpu_temp"
  exit 1
fi
echo "OK - CPU temperature $cpu_temp"
exit 0

Next, I added a check_local_cputemp command, by adding these lines to /usr/local/nagios/etc/objects/commands.cfg:

define command {

    command_name    check_local_cputemp
    command_line    $USER1$/check_cputemp
}


Then I added a CPU Temperature service to localhost, by adding these lines to /usr/local/nagios/etc/objects/localhost.cfg:

define service {

    use                     local-service
    host_name               localhost
    service_description     CPU Temperature
    check_command           check_local_cputemp
}


Then I restarted Nagios. Now, the services page has a CPU Temperature service for localhost.

I also wanted to monitor new equipment that might pop up on our home network, so I installed the  arpwatch and Postfix packages. I administered  a sendmail server for quite a few years, but that would be overkill for a network monitor. For information about installing and configuring Postfix to send outgoing mail, see Tutorial - Install Postfix to allow outgoing email on Raspberry Pi, at the Ste Wright website. Basically, I did a sudo apt-get install postfix and specified the Internet site and default hostname.

I installed arpwatch with sudo apt-get install arpwatch. Then I configured arpwatch to email me whenever anything new happened on the network, by creating the file /etc/arpwatch/wlan0.iface with the single line IFACE_ARGS="-m my@email.address" (where "my@email.address" is a valid email address to receive arpwatch messages). Then I issued the commands:

sudo systemctl enable arpwatch@wlan0
sudo systemctl start arpwatch@wlan0


For more information about configuring arpwatch, see How to monitor Ethernet Activity using Arpwatch tool, at LinuxHelp.

Once I had the Pi configured, I shut it down and mounted it, headless, on my crude network equipment board. It was easy, the classic Raspberry Pi case has two holes in the base for mounting. I plugged the power supply in, and it works!