Monday, October 13, 2008

Java code to send message to your gtalk friends

Have used a library called Smack (

import java.util.Collection;

import org.jivesoftware.smack.*;

public class GtalkClient {

public static void main(String[] args) throws XMPPException {
ConnectionConfiguration config = new ConnectionConfiguration("", 5222, "");
XMPPConnection connection = new XMPPConnection(config);
connection.login("< username >","< password >");

// Below is the code to get the users

// Roster roster = connection.getRoster();
// Collection rosterEntries = roster.getEntries();
// System.out.println("\n\n" + rosterEntries.size() + " friend(s):");
// for(RosterEntry rosterEntry:rosterEntries)
// {
// System.out.println(rosterEntry.getUser());
// }

// Here is a code to send message to a friend

MessageListener messageListener = null;
Chat chat = connection.getChatManager().createChat("",messageListener);
chat.sendMessage("Hello this is a ping from a java program");

Tuesday, September 30, 2008

Sending email using GMAIL using Java Mail Api

Note: Search for changeme and replace with appropriate terms.

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class MailSender {
final String senderEmailID = "changeme @ changeme .com";
final String senderPassword = "changeme";
final String emailSMTPserver = "";
final String emailServerPort = "465";
String receiverEmailID = null;
String emailSubject = null;
String emailBody = null;

public MailSender(String receiverEmailID, String emailSubject, String emailBody) {

Properties props = new Properties();
props.put("", emailSMTPserver);
props.put("mail.smtp.port", emailServerPort);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
// props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", emailServerPort);
props.put("mail.smtp.socketFactory.fallback", "false");

SecurityManager security = System.getSecurityManager();

try {
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
// session.setDebug(true);

MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(senderEmailID));
new InternetAddress(receiverEmailID));
} catch (Exception mex) {

private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(senderEmailID, senderPassword);
* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub
MailSender mailSender=new MailSender("changeme @ changeme .com","Test Mail from Puretechie","Here goes the body");


Saturday, September 27, 2008

Power Vs (LVM Vs VxVm). Score 2-0. Power Wins :(

we had an unplanned/unscheduled power outage recently and as a result of that, some servers went down, some storage controllers went down, some switches were up and some local disks went bad.

chaos, you may think.
it was actually much more challenging than that. and every such situation in office gives a good learning experience. once the storage controllers, switches and power to serves were restored, we had this problem.

there is one each imaging server for HP-UX, Solaris and AIX environments respectively(NOT imaging as in picture/photos imaging, this is a hard disk imaging environment). we can also call it as network based installation technologies for each h/w architecture type. All these servers store the disk images in a centralised archive server(which was, in our case, the same as the imaging server for hp-ux). the storage came from a MSA with one LUN of 2 TB presented to this host.

the server wudnt boot up after the power outage from the local disk (which was mirrored). the kernel wud just dump core and reboot infinitely. I and my colleagues were not great hp-ux kernel debuggers, so we took the alternate steps:

1. Boot from mirrored disk (although we know that a mirrored disk doesnt give data redundancy cos if data on one disk is corrupted, it wud almost simultaneously corrupt data on the other disk) - FAILED. Problem cud be hardware (memory, cpu ... although leds were all green) or OS related.
2. since the server (rp*) had 3 slots for scsi disks, we attached a disk from another server on which hp-ux (version immaterial at this point) was installed and booted the failed server from this disk. System Booted: h/w is OK. what could be so wrong with the OS that it dumps so bad??? and a power outage caused this?!?
3. we dint know the OS environment on the disk that had gone bad(version, file system type, partition information).

core options we had were
hp-ux 11.23 or 11.31
LVM or VxVm.

thats a combo of four.

and if u screw up with the metadata(fs data), u r totally lost. not just the 2TB data, configs and other data from the local disk(which had no backup ;) ) wud simply be wiped out. so, the strategy was to recover the local disk data(somehow) and then to recover the data on MSA.

a quick look at docs said LVM and VxVm totally co-exist and are "aware" of each other. good news, also bad. good news, cos they are aware of each other and hence wud _probably_ not tamper with the meta-data wen u try to access them. bad news, cos they co-exist, and hence our number of combinations to be tried out is still 4.

with fingers crossed, and since the third disk which had currently booted the server had 11.31 with lvm, i thought i will try the lvm method.

run sam, go to disks and filesystems menu, locate ur disk. note down the hardware path.
go back to volume groups menu, and say "import volume group".
u get a msg quickly saying that LVM data is not found on disk. phew.
this could mean disk corruption OR that it is a vxvm disk.

try the command line tools that sam just used with the force options vgscan, vgimport and u get same error.
OK. so its a vxvm disk.(trying to be optimistic)

apparently, vxvm software is installed as part of every hp-ux installation(may be only internally for us, cos vxvm is a licensed tool) but not activated unless you use it for root dg(disk group). vxvm and lvm have different terminologies.

LVM / VxVm
Physical Volume / VxVM Disk
Logical Volume / Volume
Volume Group / Disk Group
Physical Extent / Subdisk
LVM metadata / Private Region
Unused Physical / Free Space

LVM, as u see, is what we are mostly exposed to(due to linux and traditinal unix fs). so u know the terminologies and the equivalents. now, u need to know how to use the software itself (vxvm).
its quite easy. just run vxinstall. and use all default options. vxvm has a web based client for a front-end. the tool is called "vea" (Veritas Enterprise Administrator). vea just showed the physical disks. nothing else. all other entities(DGs, volumes) were empty. right-click on ur disk and say "recover disk" (with crossed fingers, ofcourse). and voila, the disk groups were imported. it was, a vxvm disk. root, swap and stand volumes were shows as detected, but corrupted. okay. so you run a fsck on the disk, correct all vxfs errors. and mount the root volume. ur config data and other data is there.

now for the data on the MSA. right click on msa disk and said "recover disk", just like earlier. and bam! i got a disk group that showed as 2TB free space!!! where is all the data!

here is where some concepts learnt in college helps. u most certainly know that u have only screwed up the meta-data and not yet the actual fs data. cos a disk was only initialised to be of some type(vxvm disk) and not formatted.

u go back to ur root disk and search for /mount-point/etc/fstab (damn, why dint i look into it before!!!) and it said, very promptly, that the disk was of LVM type (device names of vxvm disks are /dev/vx/dg-name/volumes, where as lvm will have it as /dev/vg-name/lvol-names). who wud think that the msa will be configured for lvm type and the root disk will be of vxvm type??? confirm it by running "strings /mount-point/etc/lvmtab" , the disk was sitting pretty there with the vg data.

ok, now, deactivate the disk using vea and delete all vxvm data. go back to sam and say "import volume group". as expected, lvm data was lost, so sam says, do u want to create a new volume group instead??? (hell, no!)

since i had access to the root disk, navigate to /mount-point/etc/lvmconfig/ and search for .conf file that will have the vg data.
using that, vgcfgrestore and vgimport commands, we cud restore the vg config. but the superblock was also corrupted(fsck said so). so had to dump the superblock from 8192 and successive(forgot the block count) blocks. fsck again and mount the lvol. phew, the data was all there!

now for the anti-climax:
even though we cud recover all data, we cudnt boot off the local disk that was dumping core. vxvm used to hang during boot-up right after initialising the disks. we had to re-setup the imaging server(we also tried using the "last_install" kernel and that wudnt boot either). a similar thing happened on an x86 VM (running on ESX 3.5)recently, which was also a critical server for us. even though fsck gave no errors and we cud access all data when mounted as an external disk on another VM, we could not boot from that disk. and thats when i give up and create the setup once again from scratch :( using the data recovered externally. and to top it all, what has power outage got to do with this???

PS: most of this might not be clear due to the language and the sequence in which i have put it. leave ur quetions as comments.

Monday, September 22, 2008

Common bad practice in database calculations

create table x ( i int, j varchar(10))
insert into x values (1,'a')
insert into `x`(`i`,`j`) values ( '2',NULL)

select count(*) from x
Select count(j) from x

Note the difference when the count(col_name) is used.

count(col_name) is used under the impression that it is faster than count(*) , however quite the opposite is true.

MYISAM table MySQL has cached number of rows in the table. Thats the reason why MYISM is able to instantly answer COUNT(*) query, but not COUNT(col_name).

Why ? Because say if col_name column is not defined as NOT NULL there can be some NULL values in it and so MySQL have to perform table scan to find out. This is also why result is different for the second query.

Using count(*) instead of count(col_name) falls in the best practice category.

Wednesday, September 03, 2008


Google Chrome

feels different.

Thursday, July 10, 2008

Hard disks and Elevators

Now, that's a weird title. What follows may sound absurd to some people. But having studied computer science, it makes sense to me.

Many of us work in high rise buildings and more often than not, we take elevators to get to our places of work. My office building has 6 elevators, and 2 control switches - each switch, when pressed, sends request to 3 elevators and one of them eventually services the request.

Recalling from the operating systems course in engineering, the disk request scheduling algorithm heavily borrows from the algorithm used by elevators to service requests. Most of us computer literate people should know that the disk request scheduling algorithm tries to minimize the number of seeks and the distance per seek. The elevator behaves somewhat similarly. For any request, among many elevators receiving the request, the one closest to the origin-place of request services it. Also, if an elevator is already moving in the direction of the origin-place of the request, it handles the request instead of an idle elevator at the same level.

Now, if there was only one elevator in question, then its guaranteed that it would handle requests optimally. But with many elevators and more than one control switch to summon the elevators, the usage could become non-optimal, depending on how people use them. Some of the common things that people do that results in non-optimal usage are:

1) Summoning more than one elevator (activating more than one control switch) for one request
2) Summoning a elevator(s) which are farther away from the origin-place of the request

Think of the elevators in your building as disks, all of them having the same data in them. The amount of distance the elevators need to travel before servicing a request as the seek of a disk. Since there is more than one elevator to choose from, we could choose one (and only one, not more than one! Refer point 1 above) which is closest to the place from which the request is being made.

1) Avoids non-optimal usage - saves power! (GO GREEN!)
2) Provides quicker service!

1) You could lose 5 seconds in looking at positions of all elevators before making a decision to summon a particular elevator - This could be offset by point 2 in advantages. Also, is 5 seconds too much, considering the slightly more weighed advantages?

There are many places like these where we can apply knowledge of computer science, in our daily lives. Its surprising that most of us do not.

Saturday, June 21, 2008

Ways to manage RSS feeds

RSS feeds aim to manage the information load.

I for example blog at these places :

a) --- my Personal blog
b) --- were a group of like minded tech enthusiasts post about topics of their interest.

Ideally I would like my friends to have be updated with my thoughts on either blogs. Currently they have to refer to the individual rss feeds separately.

To ease matters, I have used Yahoo Pipes to create a combined RSS feed for the consumers as a single combined RSS feed.

Click here for the new combined RSS link

Now this essentially is simple.

We can develop on this use case extensively.

a) Now in the tech blog: there are many other authors. With the above URL , the consumers will also get the updates from these other authors. There needs to be filtering mechanism based on Author.

Adding more complexity,
b) Suppose you like all my articles about the puzzles and are not interested in any other topics. This is where the whole thing gets tricky. Using filtering its possible, but I have to explore the capabilities of customizing to individual consumers.

Tuesday, May 27, 2008

Processing RSS feeds --the Python way

[shantanu@myjunkyard rss]$ cat
#!/usr/bin/env python
import feedparser
import sys
d = feedparser.parse(sys.argv[1])
for i in d['entries']:
for j in i['links']:
print i['title'] + "######" + j['href']

[shantanu@myjunkyard rss]$ cat rssFeed.list

[shantanu@myjunkyard rss]$ for i in `cat rssFeed.list `; do ./ $i; done

See it Yourself :)

Thursday, May 15, 2008

Ruby on Rails --- it time

After many futile attempts RoR finally is happening this year. Though I tried my best to use Java for the project(as usual , cox thats a comfort zone) , somehow winds changed direction.

For statistics The TIOBE Programming Community index which gives an indication of the popularity of programming languages, ranks Ruby 10th in popularity with 2.66% of programmers.

My initial thoughts

1) There is a need to think in a language, understand its philosophy. An interesting story to share from RoR for dummies book about the name Ruby on Rails. Since the year 2000, teams of Java programmers have been using a framework named Struts. But the word strut means something in the construction industry. (A strut is a horizontal brace, and a sturdy one at that.) Well, a rail is also a kind of horizontal brace.
And like Ruby, the word Rail begins with the letter R. Thus the name Ruby on Rails.

2) Understanding MVC is ingrained

Best part of the language is that it forces MVC , similar to Java forcing OO.

These are still baby steps , but has been exciting.

Tuesday, May 13, 2008

Powerset is released today

Powerset is a first of its kind .... a context sensitive natural language based search engine.
Currently powered by Wikipedia, this is a potentially disruptive technology.

Simple queries like

Where is PESIT?
vice chancellor of Visvesvaraya Technological University

Interesting results!!!!

Friday, April 11, 2008

Punishment for 3 days.

For 3 days I was not able to access my system with my ID.

I kinda circumvented the problem by allowing remote root ssh to my machine(which is criminal).

The problem was this:
root@shantanu>ssh sg202516@shantanu
Last login: Fri Apr 11 17:19:51 2008 from shantanu
NO LOGINS: System going down in 30 seconds
Connection to shantanu closed.

Untill today I was ok with it, but I could not access my screen sessions remotely.

So finally decided to debug it.

The issue was the presence of a file called /etc/nologin

root@shantanu>cat /etc/nologin
NO LOGINS: System going down in 30 seconds

Removing the file solves the crap.

With a feeling of achievement I told my neighbor about this.

The reply came back "Check the date of the creation of the file. It should be Apr 8th. You had not locked the system!!!"

Related Old link: