Raspberry Pi as a Bandwidth Monitor

Here’s how I started using a Raspberry Pi as a bandwidth monitor for my internet connection. It’s also a quick tutorial on how to set up your own. My internet service provider (ISP) just started offering cable internet access in addition to DSL. The largest problem with the cable internet service is that it comes with usage caps not found on the DSL service. Another catch is that my ISP can’t tell me how much data I’m currently using each month on my DSL service. (yeah, go figure) I’m not going to make the jump without knowing my average internet use.

The DSL modem/router combo my ISP provides for use with their service, unfortunately, doesn’t have the capability that allows monitoring via simple network monitor protocol (SNMP). SNMP makes it easy to get statistics on the use of a network device. Since I couldn’t directly monitor the modem, I had to come up with a different way.

Raspberry Pi bandwidth monitor graph

Your finished Raspberry Pi Bandwidth Monitor will give you nice graphs like this!

A rather simple way would be to put yet another router that does support monitoring into my internet connection chain. The downside to this is what’s called a “double-NAT” scenario. Network Address Translation creates another layer of network between my internal (house) network and the rest of the world. Some of the programs I use don’t care for that and won’t work with it in place. That method is out.

A second way would be buying another switch for inside my network that supported SNMP monitoring. They’re generally over a hundred bucks and I already had a perfectly good switch. That method’s out.

Lastly, I decided to use my Raspberry Pi as a bandwidth monitor. They’re cheap enough at ~$30 for the latest Pi 3 and I forked out another $20 for a USB to RJ45 network adapter. I already had ordered the Raspberry Pi . Amazon Prime delivered it in two days. A case and network adapter was my only new expense.

Here’s how to setup a pi bandwidth monitor.

Plug your external USB to RJ45 network adapter into your Pi, connect the cable from your internet connection into the built-in RJ45 jack, connect the USB network adapter to the cable that connects to your home network.  You should have a cable from your internet connection to the pi, then from the pi to your network switch.

Install the latest Raspbian onto your Pi’s microSD card.

On commands 6 and onward below, the 0 is a ZERO not an “oh” – e.g. eth0, br0

  1. Download and install Win32DiskImager and grab the latest version of Raspbian.
  2. Extract the Raspbian zip file to get the .img file
  3. Connect your microSD card to your PC either with a USB to SD adapter or if you have a laptop, just insert it in your laptop’s reader
  4. Open Win32DiskImager and write the image to the microSD card. (see image)win32diskwriter screenshot
  5. When it’s complete, insert the card into your Pi and boot it up with a screen, keyboard and mouse.
  6. Open a shell window and run the following commands to update your Raspbian and install the needed bridge utilities:

    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install bridge-utils

  7. Create a bridge on your pi by entering the following command in the command shell window:

    sudo brctl addbr br0

  8. Now, take a look at the interfaces (network connections) on your pi with the following command:

    sudo ip addr show
    you should see eth0, eth1 and wlan0 (if you have a pi3)

  9. Record the MAC addresses for eth0 and eth1 from step 8. You need to set your DSL modem or router to allocate a specific DHCP reservation for these MAC addresses. This way they will always have the same IP addresses and other devices coming onto your home network won’t conflict with them.  I chose to do this on my configuration to make management of IP addresses simpler, since I already used reservations for other devices.
  10. Add eth1 (the usb network adapter) and eth0 (the built-in network adapter) to the bridge with this command:

    sudo brctl addif br0 eth0 eth1
    translated, this command means add the following interfaces to bridge br0, eth0 and eth1

  11. I used DHCP to get IP addresses for the adapters. Run the following command to edit your /etc/network/interfaces file. This will save your bridge config between system boots.

    sudo nano /etc/network/interfaces
    this will open your interfaces configuration in a basic text editor

  12. Edit the file to look like the below:
     # This file describes the network interfaces available on your system
     # and how to activate them. For more information, see interfaces(5).
    
     # The loopback network interface
     auto lo br0
     iface lo inet loopback
    
     # Set up interfaces manually, avoiding conflicts with, e.g., network manager
     iface eth0 inet manual
    
     iface eth1 inet manual
    
     # Bridge setup
     iface br0 inet dhcp
        bridge_ports eth0 eth1
  13. Double check your work, then press Ctrl-O to write out the file, then the Enter key to overwrite the existing file.
  14. Enter the following command to bring your bridge online:

    ifup br0
    (that's a zero)

  15. Now, we want to set the bridge to come up every time the pi boots. To do this, we’re going to add the above command to /etc/rc.local

    sudo nano /etc/rc.local
    add the command ifup br0 before line exit 0 (if you don’t leave exit 0 as the last line, the pi won’t continue booting)
    Press Ctrl-O to write out the file, press Enter to overwrite the existing file, then press Ctrl-X to exit nano

  16. Install Cacti to monitor the interfaces on your pi.

    run the command:
    sudo apt-get install cacti

    that’s it. it will install all its dependencies(apache2, mysql, php, snmp, rrd)
    You will be asked to choose a new password for the MySQL root user. Choose cacti, or something else you can remember easily.
    You will see a warning that the include path for php has changed. Just press <Ok>.
    You will be asked to select the web server for which Cacti should be automatically configured. Choose apache2.
    You will also be asked to configure database for cacti with dbconfig-common. Press .
    You will then be asked to configure the password of the database’s administrative user. Choose cacti, or another password you can remember easily. Choose cacti also as the MySQL application password for Cacti.

Now to Configure Cacti to Monitor the Bandwidth going over your Pi’s interfaces

Cacti can be confusing when you first look at it, there’s no debate. Here’s the simplest way to set it up to use your Raspberry Pi as a bandwidth monitor.

  1. Login to Cacti by visiting the IP address of your Pi’s internal ethernet interface (see step 9 above) and the path /cacti  (e.g. http://192.168.0.100/cacti)
    Use the user/pass you configured for Cacti in step 16 above.
  2. At the top left, click Console, then click Devices from the left column
  3. At the right of your screen, you’ll see the word Add, conveniently stuck way out where you won’t see it right away. Click Add.
  4. The resulting page will ask you information about the device you’re adding to monitor, in this case, your Raspberry Pi.

    Add a Description (like a display name)
    The hostname will be the IP or hostname of your Raspberry Pi
    For Host Template, choose Generic SNMP-enabled Host.
    The only other field you need to modify, is to change SNMP version to Version 2

  5. Click Create at the bottom right
  6. The screen will refresh, and if it was correctly added, you should see basic SNMP info near the top of the screen that looks like the image below.

  7. Assuming you see the SNMP info near the top, click the link to Create Graphs for This Device. (if you don’t see the snmp info, re-check your host settings)
  8. In the section labeled Data Query [SNMP – Interface Statistics] check the boxes at the right for the ethernet interfaces I’ve highlighted in the image below.

    raspberry pi bandwidth stats selection

  9. Click Create at the bottom right, and your graphs will be done! That’s all there is to it. To view them, click the Graphs button at the top left of the screen. In the Default Tree, you should see the Display name for your device that you entered in step 4 above. Click it and you’ll see both ethernet interface bandwidth graphs. If they don’t appear immediately, don’t panic. Watch a Youtube video or something to generate some traffic and check it ten minutes later. It’s only polled every 5 minutes or so.

Parts List:

Raspberry Pi Amazon Prime delivers them quickly
Case, Power Supply, Heatsinks are included.
16GB MicroSD card
Startech USB Cat5 adapter

By | 2017-01-01T19:11:56+00:00 December 8th, 2016|computers, internet, tips|0 Comments

Leave A Comment