OpenWRT and Traffic Monitor

A nice way of keeping track of your traffic is vnstat. This beauty is found in the repository of white russian. On the website of the author of vnstat you can find a complete reference of the possibilities of vnstat.

Installing the package

You can get this fine piece of software on your router by installing the following package provided by OpenWrt, vnstat_1.4-1_mipsel.ipk. On the OpenWrt forum a guy called arteqw made a impressive setup to be used with the x-wrt webif^2. I will add his setup in the next paragraphs.

Prepare OpenWrt

After installing the package, we need to create the “database” to collect our data. First, we need to create a directory to hold the database.

mkdir /var/lib/vnstat

Now we create the database in this directory. So first change to this directory before executing the command. We will be creating a database on the WAN device of the router.

vnstat -u -i vlan1

We also want this setup to survive a reboot, so in the /etc/init.d directory we edit the file S95custom-user-startup.

mkdir -p /var/lib/vnstat
vnstat -u -i vlan1

Note: The script will be discussed later, for now, we just add this command.

Now we need to get this database to be updated on a regular interval, here cron is helpful. The crontab can be edited by the command crontab -e. In the example below, we update the database every 5 minutes.

*/5 **** vnstat -u -i vlan1


The following script must be placed in the directory /usr/sbin and will write the status of the data in the database to a file in the /tmp directory called This file will be picked up by the web-interface to display the values within the webif^2 interface of OpenWrt.

IFACE_WAN=$(nvram get wan_ifname)
IFACE_LAN=$(nvram get lan_ifname)
IFACE_WLAN=$(nvram get wl0_ifname)

rm /tmp/

echo "<br ><center>" >> /tmp/
vnstat -tr -i $IFACE_WAN | grep -v seconds >> /tmp/
echo "</center><br ><b ><th>Hourly at $IFACE_WAN[WAN]</th></b ><br ><center>" >> /tmp/

vnstat -h -i $IFACE_WAN | grep -v $IFACE_WAN >> /tmp/
echo "</center><br ><b ><th>Daily at $IFACE_WAN[WAN]</th></b ><br ><center>" >> /tmp/

vnstat -d -i $IFACE_WAN | grep -v $IFACE_WAN >> /tmp/
echo "</center><br ><b ><th>Weekly at $IFACE_WAN[WAN]</th></b ><br ><center>" >> /tmp/

vnstat -w -i $IFACE_WAN | grep -v $IFACE_WAN >> /tmp/
echo "</center><br ><b ><th>Monthly at $IFACE_WAN[WAN]</th></b ><br ><center>" >> /tmp/

vnstat -m -i $IFACE_WAN | grep -v $IFACE_WAN >> /tmp/
echo "</center>" >> /tmp/

Last thing we need to do is to add an entry in the crontab (crontab -e). This entry will run the every 5 minutes, so that the file /tmp/ will be updated.

*/5 * * * *

Web interface add-on

Finally, we need to extend the web interface, so we can see the traffic stats in our browser. To do so, we need to place the file in the directory /www/cgi-bin/webif. This file will pick up the file /tmp/

. /usr/lib/webif/
header "Status" "Traffic Statistic" "@TR<<Traffic Statistic>>"
<pre><? cat /tmp/ ?></pre>
<? footer ?>
##WEBIF:name:Status:5:Traffic Statistic

Email stats

I wanted to receive the stats of my router hourly by email. So, I looked at a package called mini-sendmail_1.3.5-1_mipsel.ipk which gives the ability to send emails. In the link section at the top I included the manpage for mini-sendmail. So to get the stats by email, I added the following line to the crontab.

0 9-16 * * 1-5 cat /tmp/traffic\ | mini\_sendmail

This crontab entry will email the contents of /tmp/ to the address It will be emailed every full hour between 09:00 and 16:00 from Monday to Friday.