Saturday, October 10, 2009

Keep track of your network usage using vnstat

Since I started using my mobile connection to connect to Internet (now and then), I felt the need to keep track of my network usage (since I only have 500mb a month, and my provider doesn't seem to provide the usage on a website). And of course, I'm talking Linux OS ;-)

I stumbled upon this nice software, vnstat,, which really suits my needs.

Unfortunately, the version that comes with Ubuntu is quite old, so I was suggested in the forum, to download and compile from sources the latest version from the website.

After unpackaged the tarbal, it's just a matter of running make install (as a superuser)

sudo make install
Installing vnStat...
Installing config to /etc/vnstat.conf
install -d -m 755 /usr/bin /usr/sbin /usr/share/man/man1 /usr/share/man/man5 /var/lib/vnstat
install -s -m 755 src/vnstat /usr/bin
install -s -m 755 src/vnstatd /usr/sbin
install -m 644 man/vnstat.1 /usr/share/man/man1
install -m 644 man/vnstatd.1 /usr/share/man/man1
install -m 644 man/vnstat.conf.5 /usr/share/man/man5
Then, the first time, you need to create the database; since I'm interested in my ppp0 mobile connection, I did
sudo vnstat --force -u -i ppp0
Before starting, I prefered to tweak the configuration a bit, so I created $HOME/.vnstatrc and I added this option
# interface specific limits
# example 8Mbit limit for eth0 (remove # to activate):
#MaxBWeth0 8
MaxBWppp0 8
This seemed to help getting rid of those annoying 4gb spikes. (On a side note, I often use the application that came with my Internet Huawei card, which often, very often, suffer from those 4gb spikes which definitely spoil the statistics of network usage, that's why I had to find a better solution to keep track of that.)

Now you can setup the vnstat daemon so that it starts automatically when the computer starts
sudo cp examples/init.d/ubuntu/vnstat /etc/init.d/
sudo chmod 755 /etc/init.d/vnstat
sudo update-rc.d vnstat defaults
And if you want to start it right now
sudo /etc/init.d/vnstat start
Every 5 minutes the daemon updates the database with the statistics (of course you can configure many things, see the vnstat documentation).

You can see the current statistics
bettini@bettini-laptop:~$ vnstat
Database updated: Sat Oct 10 15:37:54 2009

ppp0 since 04.09.09

rx: 292.90 MiB tx: 104.48 MiB total: 397.38 MiB

rx | tx | total | avg. rate
Sep '09 243.74 MiB | 88.89 MiB | 332.62 MiB | 1.05 kbit/s
Oct '09 49.16 MiB | 15.59 MiB | 64.76 MiB | 0.64 kbit/s
estimated 157 MiB | 48 MiB | 205 MiB |

rx | tx | total | avg. rate
yesterday 329 KiB | 172 KiB | 501 KiB | 0.05 kbit/s
today 7.91 MiB | 2.25 MiB | 10.16 MiB | 1.48 kbit/s
estimated 10 MiB | 3 MiB | 13 MiB |

The daily statistics
bettini@bettini-laptop:~$ vnstat -d

ppp0 / daily

day rx | tx | total | avg. rate
04.09. 8.96 MiB | 6.99 MiB | 15.94 MiB | 1.51 kbit/s
05.09. 12.05 MiB | 4.37 MiB | 16.42 MiB | 1.56 kbit/s
06.09. 9.21 MiB | 4.14 MiB | 13.35 MiB | 1.27 kbit/s
08.09. 25.17 MiB | 9.68 MiB | 34.86 MiB | 3.30 kbit/s
09.09. 6.64 MiB | 2.74 MiB | 9.38 MiB | 0.89 kbit/s
10.09. 22.53 MiB | 7.88 MiB | 30.41 MiB | 2.88 kbit/s
12.09. 10.58 MiB | 4.64 MiB | 15.22 MiB | 1.44 kbit/s
13.09. 5.13 MiB | 2.07 MiB | 7.21 MiB | 0.68 kbit/s
14.09. 7.67 MiB | 2.18 MiB | 9.86 MiB | 0.93 kbit/s
18.09. 12.54 MiB | 2.31 MiB | 14.85 MiB | 1.41 kbit/s
19.09. 16.28 MiB | 5.14 MiB | 21.42 MiB | 2.03 kbit/s
20.09. 4.76 MiB | 1.47 MiB | 6.24 MiB | 0.59 kbit/s
22.09. 11.99 MiB | 4.29 MiB | 16.27 MiB | 1.54 kbit/s
23.09. 17.75 MiB | 8.66 MiB | 26.41 MiB | 2.50 kbit/s
24.09. 8.80 MiB | 2.94 MiB | 11.74 MiB | 1.11 kbit/s
26.09. 15.72 MiB | 4.99 MiB | 20.71 MiB | 1.96 kbit/s
27.09. 383 KiB | 108 KiB | 491 KiB | 0.05 kbit/s
29.09. 23.36 MiB | 6.18 MiB | 29.55 MiB | 2.80 kbit/s
30.09. 24.23 MiB | 8.09 MiB | 32.32 MiB | 3.06 kbit/s
01.10. 9.90 MiB | 5.62 MiB | 15.52 MiB | 1.47 kbit/s
04.10. 22.22 MiB | 5.15 MiB | 27.37 MiB | 2.60 kbit/s
05.10. 8.81 MiB | 2.40 MiB | 11.21 MiB | 1.06 kbit/s
09.10. 329 KiB | 172 KiB | 501 KiB | 0.05 kbit/s
10.10. 7.91 MiB | 2.25 MiB | 10.16 MiB | 1.48 kbit/s
estimated 10 MiB | 3 MiB | 13 MiB |

for months
bettini@bettini-laptop:~$ vnstat -m

ppp0 / monthly

month rx | tx | total | avg. rate
Sep '09 243.74 MiB | 88.89 MiB | 332.62 MiB | 1.05 kbit/s
Oct '09 49.25 MiB | 15.68 MiB | 64.93 MiB | 0.64 kbit/s
estimated 157 MiB | 48 MiB | 205 MiB |

and so on.

Very nice! :-)

Sunday, October 04, 2009

QSource-Highlight - a Qt interface for GNU Source-Highlight

I recently released a new software developed with Qt 4 (this summer I took a chance to use qt and really enjoyed this wonderful framework): QSource-Highlight - a Qt interface for GNU Source-Highlight.

You can highlight your code on the fly, and have the highlighted output in all the formats supported by source-highlight (e.g., HTML, LaTeX, Texinfo, etc.). You can then copy the formatted output and paste it (e.g., in your blog), or save it to a file.

A preview of the highlighted output is available for some output formats (e.g., HTML, XHTML, etc.).

Here are some screenshots:

The preview of highlighting of some selected lines, with line numbers and 4 context lines.

The preview with line numbers (using '-' as padding char).

Latex (with color) output (source, no preview), with "entire doc" option.

The language definition file is automatically selected according to the input file extension, but you can change it manually by using the corresponding combo box. In particular you have three combo boxes:

  • the combo box for the input language definition (e.g., C, C++, Java, etc.); in particular, the combo box refers to the .lang files of Source-Highlight, which, however, should have quite intuitive names.
  • the combo box for the output format (e.g., HTML, LaTeX, etc.); in particular, the combo box refers to the .outlang files of Source-Highlight, which, however, should have quite intuitive names (e.g., htmltable.outlang generates HTML output into an html table).
  • the combo box for the highlighting style (e.g., colors, and formats of the elements of the language); these elements refer to Source-Highlight .style files and to .css files.

All the files named in these combo boxes refer to files shipped with Source-highlight, and they are searched for in the Source-highlight corresponding installation path. In case the combo boxes are empty, then the path where source-highlight searches for these files is wrong: you should then configure the correct path for source-highlight using the settings dialog (Source-Highlight Settings).

The complete documentation is available here.

QSource-Highlight uses the library included in GNU Source-Highlight, since version 3, thus you need that library to build QSource-Highlight; in particular it uses Source-Highlight-Qt (which I've already blogged about) additional library,, which provides highlighting in Qt relying on GNU Source-Highlight. Thus, you need to install Source-Highlight-Qt as well. we refer to GNU Source-Highlight's and Source-Highlight-Qt's websites for further information about the library and its installation (you may want to check whether your distribution already provides packages for these two libraries).

Thursday, October 01, 2009

Compiling Boost Regex in Linux

Boost libraries provide many interesting C++ libraries with cool functionalities. I've been using for many years now boost shared pointers and boost regex library (for regular expression). In particular, I'm using them in my software GNU Source-Highlight. These libraries are usually available in LInux distributions, but if they are not, and you need to build them (compile them) from sources... well... here comes trouble :-)

The first time, it is actually not easy to build boost libraries from sources, even though you're used to build programs from sources following the usual "./configure && make && make install"; in particular, boost use their own build system based on bjam. Indeed, I've had many complaints from source-highlight users when it comes to build source-highlight and the boost libraries are not available in their system.

I hope this post sheds some light for building boost libraries from source. In particular, I'll concentrate on regex library (in fact, you're not required to build all the libraries: you can simply select the ones you're interested in). Note that some boost libraries come only in the shape of header files (like shared pointers) and others require also a lib file (like regex). Note, however, that all the header files will be installed anyway (they basically do not require any compilation), while library files require compilation, and compiling all the libraries in boost may require some time; thus, having the chance to select only some libraries is quite useful.

In this post I'll assume that you want to build only the regex library and to install all the built files into $HOME/opt/boost (so that you will not need root access rights).

Once you unpacked the sources into a directory, say ~boost

tar xjvf ~/Downloads/boost_1_39_0.tar.bz2
you get into that directory
cd ~boost
and run the booststrap command
./ \
--prefix=$HOME/opt/boost \
--libdir=$HOME/opt/boost/lib \
Hopefully, you will get no error, and then you're ready to build and install the boost headers and the regex library with the following command:
./bjam --build-dir=build --layout=system install
This will install the directories include and lib into $HOME/opt/boost.

You can now test your installation using the example program you find in the boost website, example.cpp:

#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main()
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );

while (std::cin)
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;

Since you installed boost in a directory which is probably not in the search path of your compiler, you will need to specify the path for header inclusion and the path for library, as follows:
g++ -I$HOME/opt/boost/include example.cpp -o example \
-L$HOME/opt/boost/lib -lboost_regex
If everything went fine, you'll have your program to test.

If you want to run your program, remember that the boost regex library you built is a dynamic library, thus, again, since it is not installed in a system path where the loader searches for libraries to load at run-time, you'll need to specify that path with the environment variable LD_LIBRARY_PATH, e.g.,
LD_LIBRARY_PATH=$HOME/opt/boost/lib ./example
As I said before, you could have install the library and headers in system path, e.g., /usr; all you have to do is to specify a different bootstrap command
./ \
--prefix=/usr \
--libdir=/usr/lib \
and then run bjam as before (but with root privileges).

Hope this helps :-)