Programming

HOW TO : Perl subfunction to unmount a partition in Linux

For my record…here’s a snippet of perl code that can be called as a sub function to unmount a partition in Linux.  The magic is in the line “grep m{$mountPoint}, qx{/bin/mount}”, which essentially lets you check if the partition is already mounted or not.

sub UnMountVolume($)
{
    my $mountPoint = $_[0];

    print "Unmounting $mountPoint\n";
	# Check if the mount point exists
	if ( grep m{$mountPoint}, qx{/bin/mount} )
	{
		#Let's try to unmount it
		system("/bin/umount $mountPoint");
	}
	else
	{
		print "$mountPoint is not mounted, so didn't have to do anything\n";
	}
}

As with any perl code, I am sure there are a tons of ways to do this in a more efficient and “cool” way.

HOW TO : Find which interface a particular IP address is configured on

There are a ton of scripts to find how many IP addresses  are configured on a system, but I could not find one, whic would show me which particular network interface an IP address was configured on. Here is a one liner, that will give you this information in Linux

/sbin/ifconfig | grep -B1 10.10.10.10 | awk '{if (NR==1) print $1}'

The same script can be changes a bit to support other operating systems too. Essentially, I am doing a grep (search) of the output of ifconfig, which shows all the network information on the system for a particular IP. At the same time, I am using the -B1 option, which will show the line above the matching line. Finally, I am piping this to awk and printing the first row in the first column.

HOW TO : Simple perl script to replace lines in file

Nothing fancy.. but here is a simple perl script to open a file, search for specific content in the a line and replace it with some other content.

open (SOURCE, "< source.xml") or die "Could not open file source.xml: $!\n"; open (DESTINATION, ">modfile.xml")
or die "Could not open file modfile.xml: $!\n";

while (defined($line =)) {
if ($line =~ m/YYYYYYYY/i) {
$line = "XXXXXXXXXXXXXXXXXXX\n";
}
print DESTINATION "$line";
}

close (SOURCE);
close (DESTINATION);

You are opening a file named source.xml, reading every line and if there is some text that matches “YYYYYYYY”, you are replacing the whole line with “XXXXXXXXXXXXXXXXXXX”. I am sure there are more elegant ways to write this :).. but this will do the trick too..

SourceForge : Project of the Month

SourceForge.net is an online community supporting open source projects by providing hosting, distribution and subversion services. They choose a project every month from the hundreds of thousands of projects that are hosted on SourceForge based on popularity and activity. Most of the projects are the who is who of the Open source community. This is a good link to bookmark.. http://sourceforge.net/community/index.php/potm/

HOW TO : Configure JBoss to follow symbolic links

Jboss, in addition to being an application server also serves static content. We recently ran into an issue where some static content was not displayed when users hit the link. The JBoss server kept spewing 404 errors, stating that the content was not found. On some hair pulling research, we figured out that JBoss, like Apache, does not follow/allow content mapped to a symbolic link.

So for example in your web app, you added a sym link to another directory, where most of your content is stored, Jboss would not show the content, when you go to the link.

Here’s a quick guide to fix this.

  1. Go to the deploy folder of the context you want to configure. For example, if you used the default context, you go to $JBOSS_HOME/server/default/deploy/jboss-web.deployer
  2. Edit the context.xml file and add allowLinking=”true” in the Context (NOTE : This allows Jboss to publish symbolic links on all apps in your application server, if you want to restrict it to just one particular app, you have to edit the context.xml in your specific application folder). Upon adding the option, our context.xml file looked as such



   

   
   org.jboss.web.tomcat.security.RunAsListener

More information on options for the context.xml can be found here

http://www.jboss.org/file-access/default/members/jbossweb/freezone/docs/2.1.0/config/context.html

For the record, Jboss configuration and accompanying documentation is what I call black magic :).. Too many options, too many ways to do the same thing.

Week 6 training and improved charts with Google Charts

Week 6 of the Chicago marathon training and I was again not able to keep up with my mid week runs. Although I did manage to improve the charts displaying the training program :).. I added a new column to compare my actual milege to the recommended milege.

Here are the parameters I used

cht=bvg
chs=400×250
chtt=week+6+Training
chdl=Recommended+Miles|Actual+Miles
chco=ff0000,00ff00
chxt=x,y
chxr=1,0,15
chxl=0:|Mon|Tue|Wed|Thu|Fri|Sat|Sun|
chd=t:0,3,4,3,0,7,0|0,2,0,3,0,0,7
chbh=10

Tracking my runs using Google charts

I wrote a brief blurb, when Google came out with their Google Charts API. The API essentially allows you to represent your data visually by just generating a URL.. I have been wanting to use this in real life for quite some time now and think I finally found a need :). I am going to be tracking my weekly runs with using this API  moving forward.

Here’s a graphical representation of my runs for Week 5 of my traning for the Chicago marathon

Week 5

As you can see, I have been slacking off on my mid-week runs.

I used the following parameters to generate the graph

Type = cht (vertical bars)

Legend = chtt (Week 5 training)

Axis Values = chxl (Mon, Tue, Wed, Thur, Fri, Sat, Sun)

The URL I used to generate the image is http://chart.apis.google.com/chart?chtt=Week+5+Training&chts=FF0000,20&chdl=Miles&cht=bvs&chxt=x,y&chxl=0:|Mon|Tue|Wed|Thu|Fri|Sat|Sun|&chs=300×150&chd=t:0,0,3,0,0,0,10&chco=4d89f9,c6d9fd&chbh=20

I still need to figure out, how to show the values in the image and play with the ratio of the axis.

You can get all the information you need about this API by visiting this link http://code.google.com/apis/chart/

HOW TO : Select column in Notepad++

Notepad++, is a great open source editor similar to EditPlus and UltraEdit. One of the features that I really appreciated in EditPlus, was the capability to select a column of data. I checked if NotePad++ has a similar feature and found the following hotkey that can be used to do this..

“Alt + Left Mouse Click” puts you in Column Mode Select

For a complete list of hotkeys that can be used with Notepad++, check out this link

http://notepad-plus.sourceforge.net/uk/shortcuts.php

Tools of the trade : Java application management

We recently launched a new product at work and had to optimize a Java web application hosted in a JBoss application server for performance. The following tools came in rather handy to troubleshoot and analyze the application.

  • Java core dump memory analyzer from SAP. This tool is better than the standard run of the mill heap analyzers since it can handle larger core dumps. The tool is available here.
  • Messadmin : Session information tool by Cedrik Lime. This tool helps you to analyze the sessions on the application server in real time. We were able to install it on the application server without making any changes in our application, other than adding a couple of listners.. In essence, we copied the jar/war files to the application server and edited our application web.xml file to add the following listners
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/applicationContext*.xml,classpath:applicationContext*.xml</param-value>
</context-param>
<filter>
	<!-- MessAdmin Servlet Filter -->
	<filter-name>MessAdminFilter</filter-name>
	<filter-class>clime.messadmin.filter.MessAdminFilter</filter-class>
</filter>
	<filter-mapping>
	<filter-name>MessAdminFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
	<!-- MessAdmin listener -->
	<listener-class>clime.messadmin.core.MessAdminListener</listener-class>
</listener>