dereenigne.org

reverse engineered

Pachube Bandwidth Monitor

I recently discovered Pachube. Pachube is basically the YouTube of the graphing world. You create the data, and then send it up to Pachube to be plotted.

I use both DDWRT and OpenWrt firmwares on my routers around the house. They both are Linux based, running the 2.6 Kernel. I wanted to log the bandwidth use in the the house on an hourly basis, but neither firmware provides an easy way to measure it. I wrote this script to parse /proc/net/dev for the data for the appropriate interface and send this to Pachube. This script averages the difference between two interval across the whole interval. I may rewrite this using environmental variables so that it can be used with cron, but at the moment it works for me and I’m happy with it. I’m also aware this could be done more efficiently in Perl, but I kept it in BASH because these devices only have 4MB of ROM, and do not have space for the Perl binaries.

Setup Instructions:

  • Create a new Pachube feed with two parameters; ID 0 being upload and ID 1 being Download.
  • Personalise the script below and upload to your router as bandwidth.sh
  • Grant the script permission to execute:

    chmod +x bandwidth.sh

  • Run the script in the background:

    sh bandwidth.sh &

#!/bin/bash

# variables

#sleeptime in seconds
#dont set too low or you may exceed the pachube api rate limit
sleep=60

#wan interface varies from device from device
#ifconfig should show you
net_iface=vlan2

#pachube details
pachube_api_key="API_KEY"
pachube_feed="0000"

eval `cat /proc/net/dev | grep $net_iface | cut -d ':' -f 2 | awk '{print "rx_old="$1";tx_old="$9}'`

while [ 1 ]
do
        sleep $sleep
        eval `cat /proc/net/dev | grep $net_iface | cut -d ':' -f 2 | awk '{print "rx="$1";tx="$9}'`
        let tx_diff=$(((((($tx-$tx_old))/1024))/$sleep))
        let rx_diff=$(((((($rx-$rx_old))/1024))/$sleep))
        curl --request PUT --header "X-PachubeApiKey: "$pachube_api_key --data $tx_diff,$rx_diff "http://www.pachube.com/api/"$pachube_feed".csv"
        tx_old=$tx
        rx_old=$rx
done

Live graphs from my router at home:


comments powered by Disqus