Kudithipudi.Org

June 23, 2011

HOW TO : for loop in bash

Filed under: Linux,Programming — Vinay @ 11:54 am

Quick post for my own reference down the road. the “for” loop comes in very handy, when you want to perform the same task on multiple items in a bash shell.

For example, I wanted to query the DNS results of a couple of sub domains (blog.gogoair.com, pr.gogoair.com, tracker.gogoair.com), I can do it the normal way (that 99% of us do :) )

 dig blog.gogoair.com

dig pr.gogoair.com

dig tracker.gogoair.com 

Or, I can use the for loop function and do this

 for i in {blog,pr,tracker}.gogoair.com; do echo "$i" ; dig +short "$i"; done 

Got to love technology :) .. Makes you lazy!!..err I meant to say productive.

Thx to Cliff for the inspiration.

January 26, 2011

HOW TO : Combining Perl and Zoho to produce reports

Filed under: HOWTO,Programming,Technology,Web — Vinay @ 11:00 pm

This HOW TO is more for my notes. We had a request at work, where we had to parse some log files and create a graph from the data in the log files.

The log files looked like this

0m0.107s
0m0.022s
0m0.015s
2011-01-05_02_22
0m0.102s
0m0.024s
0m0.014s
2011-01-05_02_23

I wrote the following perl script to get the log file to look as such

| 0m0.107s| 0m0.022s| 0m0.015s| 2011-01-05 | 02:22

| 0m0.102s| 0m0.024s| 0m0.014s| 2011-01-05 | 02:23 

perl script

#!/usr/bin/perl
# Modules to load
# use strict;
use warnings;

# Variables
my $inputFile = 'input.txt';
my $version = 0.1;

my $logFile = 'parsed_input.csv';

# Sub Functions
sub Log($$$);
sub Trim($);

# Clear the screen
system $^O eq 'MSWin32' ? 'cls' : 'clear';

# Open the output log file
open(LOGFILE,"> $logFile") || die "Couldn't open $logFile, exiting $!\n";

# Open the input file
open(INPUTFILE,"< $inputFile") || die "Couldn't open $inputFile, exiting $!\n";

# Process the input file, one line at a time
while (defined ($line = <INPUTFILE>)) {
	chomp $line;
	# Check for blank line
	if ($line =~ /^$/)
		{
			# Start a new line in the output
			print LOGFILE "\n";
		}
	else
		{
			# Split the date and time
			if ($line =~ /2011/)
				{
					@date = split (/_/,$line);
					print LOGFILE "| $date[0] | $date[1]:$date[2]";
				}
			else
				{
					# Write the value to the output
					print LOGFILE "| $line";
				}
		}
	}
I then took the parsed log files and imported them into the cloud based reporting engine provided by Zoho at http://reports.zoho.com

The final result are these reports

SERVER1

SERVER2

Did I say, I love technology? :)

December 31, 2009

HOW TO : Improve Jboss startup times

Filed under: HOWTO,Linux,Programming,Technology,Web — Vinay @ 6:11 pm

We run multiple applications in Jboss at my work and one of the applications used to take an inordinate time to come up. A typical application would take < 1 minute to get deployed and this particular application for some reason was taking ~7-8 minutes. We initially thought it was a bug in the code and gave hell to our development team :) .. But on closer investigation, we found out that a feature we enabled in the Jboss server settings which allows content to be hosted on network storage was causing the issue.

I blogged the feature in Jboss to follow sym links here (http://kudithipudi.org/2008/07/25/howto-configure-jboss-to-follow-symbolic-links/). So essentially when Jboss was started, it was checking all the content in these network path to check for applications to deploy. And traversing a network share with 1000s of directories isn’t fun :) ..

We fixed it by making a simple edit to the start up script. Here’s the psuedo code for the script

  1. Remove soft links to network share
  2. Start Jboss
  3. Put soft links to network share

And now the application starts in less than a minute :) .

I guess there might be other elegant ways to do this. i.e. Configure Jboss to only deploy certain applications, but this did the trick for us :) .

May 22, 2009

HOW TO : Perl subfunction to unmount a partition in Linux

Filed under: Programming,Technology — Vinay @ 10:18 pm

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

Filed under: HOWTO,Linux,Programming,Technology — Vinay @ 3:28 pm

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.

February 19, 2009

HOW TO : Simple perl script to replace lines in file

Filed under: HOWTO,Programming,Technology — Vinay @ 2:11 am

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..

January 24, 2009

SourceForge : Project of the Month

Filed under: Programming,Technology — Vinay @ 5:19 pm

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/

July 25, 2008

HOW TO : Configure JBoss to follow symbolic links

Filed under: HOWTO,Linux,Programming,Technology,Web — Vinay @ 1:42 am

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.

July 17, 2008

Week 6 training and improved charts with Google Charts

Filed under: Programming,Running,Technology,Web — Vinay @ 11:13 pm

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

July 11, 2008

Tracking my runs using Google charts : Part II

Filed under: HOWTO,Programming,Technology,Web — Vinay @ 6:56 pm

Comment from my friend Sri, after looking at my post regd using Google Charts API to track weekly runs.

” Hey V, You can show the value of the bars by using the variable CHM”..

Thx for making me lazy dude :) .. Here’s the upated image

Older Posts »

Powered by WordPress