Skip to content

The Digg effect, on my blog

July 3, 2009

My blog was stumbled a few days ago, and this is the result:


I always heard about such spikes in visitors / page views, but I never thought it would happen to me 🙂 . Five times more visitors than the average, in one day.


The new Firefox 3.5 icon

July 1, 2009

I simply love it 🙂

Firefox 3.5 icon

Firefox 3.5 icon

Add figure or table that spans multiple columns

June 21, 2009

If your Latex document has two or more columns, if you add a figure or table, it will span one column and then overlap with the text from the second one. To fix this and let the object take up space in both columns, just add a star after the object name, like this:

\caption{Table Caption}
\begin{tabular} …

or, for a figure:

\caption{ Figure Caption}

How to get CPU usage in Linux from Java

May 15, 2009
tags: ,

Technorati Profile

The easiest method to get the CPU usage from a Java class in Linux is to parse the output of the top command. Check out the following code.

public class SystemData
    static String cmdTop = "top -n 2 -b -d 0.2";
    // returns user cpu usage 
    public static double getCpu()
        double cpu = -1;
            // start up the command in child process
            String cmd = cmdTop;
            Process child = Runtime.getRuntime().exec(cmd);

            // hook up child process output to parent
            InputStream lsOut = child.getInputStream();
            InputStreamReader r = new InputStreamReader(lsOut);
            BufferedReader in = new BufferedReader(r);

            // read the child process' output
            String line;
            int emptyLines = 0;
                line = in.readLine();
                if (line.length()<1) emptyLines++;
            line = in.readLine();
            System.out.println("Parsing line "+ line);
            String delims = "%";
            String[] parts = line.split(delims);
            System.out.println("Parsing fragment " + parts[0]);
            delims =" ";

            parts = parts[0].split(delims);
            cpu = Double.parseDouble(parts[parts.length-1]);
        catch (Exception e)
        { // exception thrown
            System.out.println("Command failed!");
        return cpu;

The parameters from the top command line mean: -b = batch mode, –d 0.2 = take the readings at 0.2 seconds intervals, -n 2 = take 2 readings (the first reading is not correct, from my experience, so we take two).
This code fragment only gets the user CPU percentage. To get the whole CPU percentage, you have to add this value with the system CPU. These values are easy to obtain by changing a few numeric values in the above code. This is left as an exercise for the user. Note that although in general the system CPU and nice CPU percentages are very small, this is not always the case and it is safer to add these values together than to approximate the CPU usage as user CPU only.

How to surf the Intranet (local network sites) using ssh

May 11, 2009

If the company you work for has restrictions that allow visiting certain company websites only from within the company, you can bypass this restriction using ssh access.

From your local machine, connect to the ssh server of your company and add a few options, like this:

ssh -C2qTnN -D 8090 username@servername

This command will not finish, this is expected and desired. The port (in my case 8090) can be any port you want (but over 1024 if you are not root).

You have created a ssh tunnel that ca be used to tunnel the web browser traffic, so that you can see the sites that are visible from you ssh server. To do this using Firefox, go to Preferences->Advanced->Network. The window will look like the image below.

Firefox Network Options window.

Firefox Network Options window.

Click on the “Settings” button and a configuration screen will pop up. Complete it like in the following screenshot.

Firefox Connections Settings Window. Here we direct the browser to use the ssh tunnel we created.

Firefox Connections Settings Window. Here we direct the browser to use the ssh tunnel we created.

This is all you need to do,  you can now surf on local sites from the internet by entering the url you want in the URL bar in Firefox. Don’t forget to set “no proxy” option in the same configuration screen when you want to close the ssh window.

Using ssh tunnels is also helpful if you are on a network that you don’t trust and use websites that do not have strong cryptography. SSH traffic is encrypted and as such, the security risk will be less.

Remove file from Subversion after svn add, but before svn commit

May 6, 2009
Subversion code versioning system Logo

Subversion code versioning system Logo

If you do not want to add a certain file to your subversion repository, but it is already marked to be committed (by an already issued  svn add command) you can just type:

svn revert file.ext

to undo the add command.

If you remember to do it before the corresponding svn commit, doing this helps keeping the repository to a decent size. If you already commited the changes, you can remove it using svn delete (and an extra –force argument, if needed), but the space is wasted.

Linux ssh forwarding / tunneling / duble ssh using the console

April 26, 2009
tags: , ,

Double ssh (ssh on one machine, then on another) is a viable option for using a workstation from over the internet. Administrators prefer this method because it allows them to expose a single machine to access from the internet  (and manage the security on this machine only). For the user, double ssh, also called, ssh tunneling or ssh forwarding means that, instead of connection to the machine they want to work on (located in the local network of the corporation), they have to ssh to the corporation network gateway, then ss again on their workstation.

The network configuration is this :

Network configuration for SSH Tunneling

Network configuration for SSH Tunneling (taken from this tutorial):

In Windows, programs like WinScp allow users to easily SSH on the workstation from a GUI. In Linux, using console command line is required, and the commands are not as easy as others. I will offer an example based on my configuration, all you have to do is adjust the hostnames for your own case.

drawing13The commands you have to enter are these:

1. Announce the local ssh and the gateway that you want ssh connections on a local port to be forwarded to the gateway and marked  as “tunnel for workstation”:

ssh -l picobit -L cat –

You will be required to enter your account password for the gateway (in my case, the password for the picobit username)

This command will not end. You have to end it manually after finishing your ssh session that you will open in step 2.

2. In a new terminal (the old one is blocked with the command from step 1):

ssh -p 7777 picobit@localhost

You will be required to enter the password for the workstation account.

If you get an error on step 2, you might want to add the -v parameter on the command from step 1, to enable verbose mode, it will help you debug the problem more efficiently.