Traffic Monitoring with vnStat

Interested in how much bandwidth you are using? vnStat is a simple command line utility to track network traffic on a Linux computer. vnStat is easy to set up and use on Ubuntu, and works well on both server and desktop systems.

Run these three commands to install vnStat and set up permissions so every user can run it:

sudo apt-get install vnstat
sudo chmod o+x /usr/bin/vnstat
sudo chmod o+wx /var/lib/vnstat/

You need to find the name of the network interface you want to monitor. Most of the time it will be called eth0, run

ifconfig

to check if you are not sure. Then run this command, replacing eth0 with the name of your network interface:

vnstat -u -i eth0

That command creates a cron job (scheduled task) that will run every few minutes and collect traffic data for vnStat.

Check the collected data by running vnstat on a command line. It will bring up the daily usage information:

Database updated: Thu Aug 30 13:40:01 2007

        eth0

           received:          90.65 MB (11.9%)
        transmitted:         666.12 MB (88.1%)
              total:         756.77 MB

                        rx     |     tx     |  total
        -----------------------+------------+-----------
        yesterday     49.94 MB |  378.44 MB |  428.38 MB
            today     17.27 MB |  125.30 MB |  142.57 MB
        -----------------------+------------+-----------
        estimated        29 MB |     218 MB |     247 MB

Run vnstat -h to see an hourly graph:

 eth0                                                                     13:40
  ^   t
  |   t
  |   t
  |   t
  |   t           t                                                     t
  |   t  t  t     t        t                                t  t     t  t
  |   t  t  t  t  t  t  t  t  t  t                 t  t     t  t     t  t
  |   t  t  t  t  t  t  t  t  t  t  t        t  t  t  t  t  t  t  t  t  t  t
  |   t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t
  |  rt  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t  t
 -+--------------------------------------------------------------------------->
  |  14 15 16 17 18 19 20 21 22 23 00 01 02 03 04 05 06 07 08 09 10 11 12 13    

 h   rx (kB)    tx (kB)      h   rx (kB)    tx (kB)      h   rx (kB)    tx (kB)
14       2543      21517    22       1286       9950    06       1304       9431
15       1628      11561    23       1406       9223    07       1147       8576
16       1770      12431    00        999       7849    08       1775      12656
17       1388       9518    01        673       5071    09       1485      11738
18       1870      13693    02        932       6089    10       1063       7514
19       1392       9417    03        882       7620    11       1530      11656
20       1522      10194    04       1024       7629    12       2118      14688
21       1838      11744    05       1429       9662    13       1330       8130

vnStat has many more options as well. Run

vnstat –help

to more ways to look at the data. Also check out the screenshots section of the vnStat website for examples.
Start and test vnstat:
Note which interfaces are active on your PC.

vnstat -u -i eth0
vnstat -u -i eth1

Now you can generate text reports of your monthly bandwidth usage. I use eth1 in the following examples, but you may be using eth0 or both eth0 and eth1.
Daily bandwidth report.

vnstat -i eth1 -d

Hourly bandwidth report.

vnstat -i eth1 -h

Monthly bandwidth report.

vnstat -i eth1 -m

Real time speed report.

vnstat -i eth1 -l

Installing Ant under Ubuntu

Java version of Make that uses XML descriptor files is generally very easy. It is packaged by Debian so installing it is as simple as “apt-get -u install ant”. The great thing about ant is that it is quick and easy to extend it’s functionality by adding extra tasks. If you use Tomcat, for instance, there are tasks that allow you to start, stop and redeploy web applications. To use these ant tasks simply link the ant jar found in the tomcat /server/lib directory (called catalina-ant.jar) into the ant lib directory. For instance:

ln -n /usr/local/tomcat/server/lib/catalina-ant.jar /usr/share/ant/lib

Installing Tomcat 6 on Ubuntu

If you are running Ubuntu and want to use the Tomcat servlet container, you should not use the version from the repositories as it just doesn’t work correctly. Instead you’ll need to use the manual installation process that I’m outlining here.

Before you install Tomcat you’ll want to make sure that you’ve installed Java. I would assume if you are trying to install Tomcat you’ve already installed java, but if you aren’t sure you can check with the dpkg command like so:

dpkg –get-selections | grep sun-java

This should give you this output if you already installed java:

sun-java6-bin                                   install
sun-java6-jdk                                   install
sun-java6-jre                                   install

If that command has no results, you’ll want to install the latest version with this command:

sudo apt-get install sun-java6-jdk

Installation

Now we’ll download and extract Tomcat from the apache site. You should check to make sure there’s not another version and adjust accordingly.

wget  http://apache.hoxt.com/tomcat/tomcat-6/v6.0.14/bin/apache-tomcat-6.0.14.tar.gz
tar xvzf apache-tomcat-6.0.14.tar.gz

The best thing to do is move the tomcat folder to a permanent location. I chose /usr/local/tomcat, but you could move it somewhere else if you wanted to.

sudo mv apache-tomcat-6.0.14 /usr/local/tomcat

Tomcat requires setting the JAVA_HOME variable. The best way to do this is to set it in your .bashrc file. You could also edit your startup.sh file if you so chose.

The better method is editing your .bashrc file and adding the bolded line there. You’ll have to logout of the shell for the change to take effect.

vi ~/.bashrc

Add the following line:

export JAVA_HOME=/usr/lib/jvm/java-6-sun

At this point you can start tomcat by just executing the startup.sh script in the tomcat/bin folder.

Automatic Starting

To make tomcat automatically start when we boot up the computer, you can add a script to make it auto-start and shutdown.

sudo vi /etc/init.d/tomcat

Now paste in the following:

# Tomcat auto-start
#
# description: Auto-starts tomcat
#  processname: tomcat
# pidfile: /var/run/tomcat.pid

export JAVA_HOME=/usr/lib/jvm/java-6-sun

case $1 in
start)
sh /usr/local/tomcat/bin/startup.sh
;;
stop)
sh /usr/local/tomcat/bin/shutdown.sh
;;
restart)
sh /usr/local/tomcat/bin/shutdown.sh
sh /usr/local/tomcat/bin/startup.sh
;;
esac
exit 0

You’ll need to make the script executable by running the chmod command:

sudo chmod 755 /etc/init.d/tomcat

The last step is actually linking this script to the startup folders with a symbolic link. Execute these two commands and we should be on our way.

sudo ln -s /etc/init.d/tomcat /etc/rc1.d/K99tomcat
sudo ln -s  /etc/init.d/tomcat /etc/rc2.d/S99tomcat

Tomcat should now be fully installed and operational. Enjoy!