VnStat and shttpd

From MyWiki

Jump to: navigation, search

Something that you may want to get and place on your router to see the amount of traffic passing through it.

vnStat is tiny network traffic monitor. Console based, runs on FreeBSD and Linux. shttpd is small HTTP server. Ideal for embedded devices.

So, what I essentially wanted is to know what my ISP knows - how much traffics goes in and out of my house network. Nice to know when you have a cap on the amount of data you can pump through :-)

First, installing vnStat package

# pkg_add -r vnstat

Now I need to create configuration file for vnStat daemon. There is an alternative to run vnstat through cron, but I prefer daemons :-)

# cd /usr/local/etc/
# cp vnstat.conf.sample vnstat.conf
# vi vnstat.conf 

I changed the Default interface directive, replacing eth0 with the name of external interface of my router (tun0)

# vi /usr/local/etc/rc.d/vnstatd 

And created start script for the daemon (for some reason there is none coming with the package):

#!/bin/sh
#
# PROVIDE: vnstatd
# REQUIRE: DAEMON
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable vnstatd:
#
# vnstatd_enable (bool):		Set it to "YES" to enable vnstatd
#										Default is "NO".
# vnstatd_flags (flags):		Set extra flags to vnstatd
#				Default is "-d --sync --config /usr/local/etc/vnstat.conf"
#

. /etc/rc.subr

name=vnstatd
rcvar=${name}_enable

load_rc_config $name

: ${vnstatd_enable="NO"}
: ${vnstatd_flags="-d --sync --config /usr/local/etc/vnstat.conf"}
: ${vnstatd_pidfile="/var/run/vnstat.pid"}

pidfile=${vnstatd_pidfile}
command=/usr/local/sbin/vnstatd

run_rc_command "$1"

Then added vnstatd_enable="YES" into /etc/rc.conf

You need to create database where the statistic figures will be stored. Mind you, the command doesn't create the directory for you, so have to create it myself first:

# mkdir /var/db/vnstat
# vnstat -u -i tun0 --nick wan

Fixing file rights and run the daemon:

# chmod 0555 /usr/local/etc/rc.d/vnstatd
# /usr/local/etc/rc.d/vnstatd start

Now, I want my numbers presented to me in nice looking pictures. That's something vnstati does for me, so I need the following lines in my crontab:

*/5 * * * * /usr/local/bin/vnstati -vs -o /usr/local/www/`hostname`_sum.png -i tun0
*/5 * * * * /usr/local/bin/vnstati -d -o /usr/local/www/`hostname`_daily.png -i tun0
*/5 * * * * /usr/local/bin/vnstati -m -o /usr/local/www/`hostname`_monthly.png -i tun0

All the number crunching is up and running, let's get on to with installation of the HTTP server:

# pkg_add -r shttpd
# vi /usr/local/etc/rc.d/shttpd 

Created start script for the daemon and again for some reason there is none coming with the package:

#!/bin/sh
#
# PROVIDE: shttpd
# REQUIRE: DAEMON
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable shttpd:
#
# shttpd_enable (bool):		Set it to "YES" to enable shttpd
#                                                     Default is "NO".
# shttpd_flags (flags):		Set extra flags to shttpd
#                                             Default is "-root /usr/local/www -acl +10.254.0.0/24 -ports 7878"
#

. /etc/rc.subr

name=shttpd
rcvar=${name}_enable

load_rc_config $name

: ${shttpd_enable="NO"}
: ${shttpd_flags=" -root /usr/local/www -acl +10.254.0.0/24 -ports 7878"}

command=/usr/local/bin/shttpd
command_args=" &"

run_rc_command "$1"

And finally the index.html:

# vi /usr/local/www/index.html 

And adding the following content into it:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
         <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Router's Internet facing device stat</title>
<meta http-equiv="expires" content="-1">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="refresh" content="30">
</head>
<body>
<img src="sum.png" alt="summary" /><br>
<img src="daily.png" alt="daily" /><br>
<img src="monthly.png" alt="monthly" />
</body>
</head>

That's all to it. Now I have the tool to see my traffic in and out. Beware ISP, now I know my figures too ;-).

Personal tools