Monitoring your monthly bandwidth usage

After getting my new USB modem and struggling to make it work on Ubuntu 10.04 beta (at that time), I wanted to monitor my monthly bandwidth usage. I was thinking at first to create my own shell script, but I was lucky enough to know about vnstat. it’s a simple package you can install simply using

sudo aptitude install vnstat

you can use vnstat to monitor any network interface you want using the following steps except that you will have to use the name of the network interface you want to monitor.

Now I will tell vnstat to watch my USB modems bandwidth usage by making it watch the ppp0 interface.

sudo vnstat -u -i ppp0

this will create a file in /var/lib/vnstat with the name of your interface, so this will create a file named ppp0.

Now, just start the vnstat service. you will do it this time manually, but It will start automatically every time you start your machine.

sudo service vnstat start

Your ppp0 interface is being monitored now and you will be able to see its statistics by running

sudo vnstat -i ppp0

The output will be something like that

Database updated: Fri Aug 13 01:08:03 2010

          rx:  17.36 MiB      tx:  2.79 MiB      total:  20.15 MiB

   monthly
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       Aug '10     17.36 MiB |    2.79 MiB |   20.15 MiB |    0.16 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated        43 MiB |       5 MiB |      48 MiB |

   daily
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     yesterday      8.54 MiB |    1.73 MiB |   10.27 MiB |    0.97 kbit/s
         today      8.82 MiB |    1.06 MiB |    9.88 MiB |   19.82 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated       169 MiB |      21 MiB |     190 MiB |

rx is your received data, while tx is your sent data, or you can just view the total.

iconv misunderstands UTF-16 strings with no BOM

I had a problem last week with converting UTF-16 encoded strings to UTF-8 using PHP’s iconv library on a Linux server. my code worked fine on my machine but the same code resulted in a rubbish unreadable characters on our production server.

Let me take you to the beginning of the problem. I had a Hexadecimal representation of a UTF-16 string like that

0635 0628 0627 062D 0020 0627 0644 062E 064A 0631

this is the equivalent of “Good morning” in Arabic “صباح الخير” . I had some lines of code that will convert this into a normal stream of UTF-16 bytes so I can be able to use iconv to convert the string to UTF-8. maybe you noticed that there is no BOM at the beginning of Hexadecimal representation of the string. so let me quote what is written on Unicode’s BOM FAQ page about this.

Q: Why do some of the UTFs have a BE or LE in their label, such as UTF-16LE?

A: UTF-16 and UTF-32 use code units that are two and four bytes long respectively. For these UTFs, there are three sub-flavors: BE, LE and unmarked. The BE form uses big-endian byte serialization (most significant byte first), the LE form uses little-endian byte serialization (least significant byte first) and the unmarked form uses big-endian byte serialization by default, but may include a byte order mark at the beginning to indicate the actual byte serialization used.

so when there is no BOM, the string should be treated as big-endian. libiconv has a different opinion about this and will try to guess if it should use big-endian or little-endian depending on the operating system. so you will get different results on different machines.

The simple solution to this problem (after a long time trying to identify it), is to just tell iconv that I’m converting from UTF-16BE (big-endian) so it won’t try to guess the endianess of the bytes. so in php it will be like that

$result = iconv('UTF-16BE', 'UTF-8', $str);

or better, I can check the BOM before converting the Hexadecimal codes to a stream of bytes and taking the decision of converting from UTF-16BE or UTF-16LE depending on if it begins with FEFF or FFFE.

Irssi as your IRC client with notification using shnotify.pl

After spending the last years trying many IRC clients, I found that all GUI clients suck. so I decided to use a terminal based client and all recommendations pointed me to use Irssi. It took me around 2 hours to get used to how it works, just some commands and installing some of its scripts got me to do what I want. maybe you will love using it too if you are spending your day in front of terminals or if you just believe that GUI clients aren’t your best choice.

Installing it on Ubuntu wasn’t a problem, just like what you always do

sudo aptitude install irssi

now run irssi in your terminal to start it. now you can connect to any IRC server by typing this

/connect irc.freenode.net

Irssi won’t remember any settings and auto start it till you tell it to do so, and I wanted to make Irssi auto-connect to freenode.net on startup

/SERVER ADD -auto -network freenode irc.freenode.net

also, I didn’t want to type my password every time I connect to this server

/NETWORK ADD -autosendcmd "/msg nickserv identify mypassword;wait 2000" freenode

this will run the IDENTIFY command with my password after connecting to the freenode server and will wait for 2 seconds before doing any thing else.

now I can join any channel the normal IRC way by typing

/join #channelName

and I can jump between my opened channels using CTRL+n and CTRL+p , or by pressing ALT+ window number . if you are using gnome-terminal with many opened tabs, maybe you will have problems with moving between channels using ALT+number as this shortcut is used by gnome-terminal to jump between tabs.

Also, I found some nice scripts for Irssi like
nickcolor.pl , this script will give each user in your opened channels a separate color.

trackbar.pl , this one will put a separator (one line) after the last message appeared in every channel before you leave it.

installing these script is easy, you can put these script in ~/.irssi/scripts/ , then in irssi load them using

/run scriptname.pl

if you like a script and want to auto-load it on start, just create another directory “~/.irssi/scripts/autorun/” and create links in this directory pointing to your scripts that should be auto-loaded.

I wanted Irssi to notify me when anybody mention my name by showing me a notification bubble. I found here a script “mumbles.pl” written be Matthew Weier O’Phinney our Zend Framework warrior. but for my bad luck I didn’t know how to get the mumbles notification server specially as its website is down. so I tried to play with that script to make it show notifications using zenity. you will find the code here, you can create a new file “shnotify.pl” in your ~/.irssi/scripts directory and put that code in it. this will show you a notification bubble whenever your nickname or any other word you tell Irssi to highlight using the /hilight yourword command is mentioned.

use strict;
use Irssi;
use Irssi::Irc;
use vars qw($VERSION %IRSSI);

$VERSION = '0.1.0';
%IRSSI = (
 authors     => "Ahmed Shreef",
 contact     => 'ahmed@shreef.com',
 name        => 'notification bubble for Irssi',
 description => 'This script will show a notificatin bubble near the systray when your nickname is mentioned',
 license     => 'New BSD',
 changed     => "2010-6-9"
);

sub shnotify {
 my ($dest, $text, $stripped) = @_;

 if (($dest->{level} & (MSGLEVEL_HILIGHT|MSGLEVEL_MSGS)) && ($dest->{level} & MSGLEVEL_NOHILIGHT) == 0)
 {
 if ($dest->{level} & MSGLEVEL_PUBLIC)
 {
 my $message = $dest->{target} .": " . $stripped;
 `echo "message:$message" | zenity --notification --listen --window-icon=null`;
 }
 }
}

Irssi::signal_add({
 'print text'    => \&shnotify
});

tell me if you like it or if you need some help.

GNU/Linux Install Fest [April 3, 2010]

Next Saturday April 3, 2010 in Culture Wheel [Sakiet El-Sawy], there will be a Gnu/Linux Install fest.  in that day, EGLUG volunteers will give sessions about installing, using and programing [web/desktop] applications over Linux. if this is your first time to hear about Gnu/Linux and want to install it on your PC or Notebook or you already installed it but you are still having some problems with some programs or you don’t know the best alternatives for your old MS Windows/Mac programs over Linux, you can come to the install fest and you will find the tech support team standing there to give help and support to you.

some of the topics will presented in the sessions will be :

Basic:
- Installing GNU/Linux
- Alternatives for your Windows/Mac programs
- Desktop usage under Linux

Advanced & Programming:
- Installing Web Servers
- Desktop programming under Gnu/Linux
- Python
- Drupal
- Security

Graphics and Design:
- GIMP
- Inkscape

General:
- Free Culture
- Wikipedia

the day will start around 9:00am in the River Hall in Culture Wheel. the sessions will be working in parallel all day so whenever you show up there before 5:00pm , you will find interesting sessions to attend.

That day is going to be totally Free ;-)

and wait for my move to a new blog on my own host soon -isA-.

Software Freedom Day @ Alexandria University

N98WBG78TR28

I knew about this event 2 days ago by coincidence. A group of programmers and software and knowledge freedom advocates are organizing an event in Alexandria University’s Faculty of Engineering to educate people about some targeted topics from the 1st to 15th of September 2009.

from their sessions schedule. I can see that they concentrate on topics related to
- programming under Linux
- Open Solaris
- Security
- Wikipedia

I had an interest in attending this event as I didn’t attend any offline event for a long time, maybe that last one I attended was .NetWork‘s 7th gathering. the fact that I’m a night programmer, makes trying to wake up early seem impossible. I go to bed every day at 7:00 am and I can’t succeed to sleep before that time. but today I did it somehow and slept for 2 hours then went to the event.

The first session was a beginners level session about Linux Ubuntu and the presenter did well at introducing the audience to what is Linux, but the only thing I hated, is that way he described windows as evil or bad operating system compared to the great Linux system. he wasn’t fair enough, but this is how most of the my operating system rocks people do (whatever if they are using Mac, Windows or Linux). in total, the session was good but I was seeking for a more advanced session, maybe I didn’t have to attend that session .

The second session was about Wikipedia. a number of the Arabic Wikipedia contributors were there trying to get some of the audience into writing for Wikipedia or even contributing suggestions. between the sessions they held a workshop to educate the interested audience on how to add/edit pages on Wikipedia.

I noticed something interesting. The Wikipedia presenter in the second session had a notebook with Windows 7 installed. I thought that he will be using Linux but it seems like those Wikipedia guys/gals don’t belong to the same geeky class as the other organizers of the event, but they share the same vision and target.

the day is rated good and I wish to attend the next sessions till the end of the event. if you are in or near Alexandria or passing by and interested,  join the sessions at lectures hall M3 in the preparatory building in faculty of Engineering Alexandria.

the next  2 sessions will be on 7-9-2009, these will be about security. check the the full events schedule and the Facebook event page .