sudo apt-get update
Now, we need to install vnstat, vnstati and php5-gd:
sudo apt-get install vnstat vnstati php5-gd
Now, after 10 minutes or so, you can type vnstat in terminal and you should get some data. We can use vnstati to generate PNG images of this data for viewing in a web page. Lets create a directory in your web root to store the files:
sudo mkdir /var/www/vnstati
Now, we can make a shell script to generate the images. Make this outside the web directory somewhere, such as your home directory as we are doing here:
Now paste in the following. Change eth0 to the device you wish to monitor if necessary:
#!/bin/bash vnstati -s -i eth0 -o /var/www/vnstati/summary.png vnstati -h -i eth0 -o /var/www/vnstati/hourly.png vnstati -d -i eth0 -o /var/www/vnstati/daily.png vnstati -t -i eth0 -o /var/www/vnstati/top10.png vnstati -m -i eth0 -o /var/www/vnstati/monthly.png
Now hold CTRL + X, press Y and then ENTER to save the file. Next, we need to make vnstati.sh executable:
chmod +x ~/vnstati.sh
Now we can set up a Cron job to run our script every so often. Note if you use the default Apache document root of /var/www/ then you must add the Cron job as root. So, do:
sudo crontab -e
and add this line (edit path to vnstati.sh to suit). I use nice to give it a low priority, not that its a heavy process.
For updating every 5 minutes:
*/5 * * * * nice /home/pi/vnstati.sh >/dev/null 2>&1
Or for once an hour:
0 * * * * nice /home/pi/vnstati.sh >/dev/null 2>&1
Now hold CTRL + X, press Y and then ENTER to save the crontab. You should see:
crontab: installing new crontab
You should now have the image files being generated every 5 minutes (or every hour) in the location you chose. You can now make a html page to display the images and put that in the same folder as them. Feel free to use this basic html template I made here: template.html.
To use this template, we grab the template.html file from here and save it as index.html in /var/www/vnstati/ alongside our images:
sudo wget http://j0hn.uk/vnstati/template.html -O /var/www/vnstati/index.html
You should now be able to see the page at http://youripaddress/vnstati
More info on vnstati and its commands can be found here.
Optional stuff to do... You can customise the colours of the graphs if you like. This website is helpful. You can do this by editing vnstat.conf
sudo nano /etc/vnstat.conf
go down to the bottom and edit the colors as you wish. Here are mine:
# image colors CBackground "FFFFFF" CEdge "AEAEAE" CHeader "705100" CHeaderTitle "FFFFFF" CHeaderDate "FFFFFF" CText "000000" CLine "B0B0B0" CLineL "-" CRx "1A6AFF" CTx "FF0000" CRxD "-" CTxD "-"
I also edited the locale setting which is at the top of vnstat.conf as by default its set to:
This was outputting the date in US format (M/D/Y) which is just weird (sorry fellow Americans!) You can check what your machine is using by doing 'locale' at the terminal. I changed it to the European format (D/M/Y) by changing this setting to:
Now hold CTRL + X, press Y and then ENTER to save the configuration file.
If you found this page helpful, please leave a comment in my guestbook. A link to your stats page would be good too!