Entries (RSS)  |  Comments (RSS)

Stealing log files from other processes.

It occurs to me that there’s hundreds of scripts, programs, and code snippets that I use on a daily basis that have just never seen the light of day.Time to change that. I’m not going to provide you with a pretty library like jwz has, but I’ll do what I can.At my day job, we had a piece of proprietary software (ATG Dynamo) that would only issue a thread dump if we asked nicely, and even then, it would write that data out to it’s own file with no timestamps or indication of which process the dump was coming from. The trick here, was to steal the data while it was entering the log file so we could post process it and tag it with the appropriate server name and timestamp.

Here’s my perl code that makes this work, which may be of interested to anyone using java application servers.

# Find all running DRPs and request a thread dump, pulling the
# thread dump out of ATG’s log and making a private copy for ourselves.
#

my $ATGLOGDIR=”/gl/atg/ATG7.1/home/logs”;
my $ATGOUTDIR=”/gl/atg/threaddump_logs”;

open(P,”ps -auxwww –forest|”);

# prep output
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);

my $tm = sprintf(“%d%02d%02d-%02d:%02d:%02d”,$year+1900,$mon,$mday,$hour,$min,$sec);

open (W,”>$ATGOUTDIR/$tm-threaddump.log”) || die “cannot open log output file”;

while(

) {
if (/startDynamo/) {
# we know that next line will be java
$_ =

;

# ps regexp
if (/\w+[ ]+([0-9]+).* .*\/servers\/(.*)\/logs/) {
print W “Java PPID = $1 name = $2\n”;
$SVR{$1}->{pid} = $1;
$SVR{$1}->{name} = $2;

}

}
}

close(P);

foreach $k (sort keys %SVR) {
# before we do this, we need to open the other file
my $CMD=”ls -lat /gl/atg/ATG7.1/home/logs/ | grep ” . $SVR{$k}->{name} . “_threadDump | awk ‘{ print \$NF }’”;
my $LOGFILE=`$CMD`;

open (R,”<$ATGLOGDIR/$LOGFILE");
seek(R,0,2); # seek EOF

system("kill -3 $k");

# wait for buffer to accumulate
sleep 1;

while() {
print W localtime(time) . ” [" . $SVR{$k}->{name} . "]: ” . $_;
}

close(R);
}
close(W);

This entry was posted on Monday, February 4th, 2008 at 3:59 pm and is filed under perl, systems administration. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

blog comments powered by Disqus