I'm writing a Perl script and I need to capture some lines from a garbage collection log and write them to a file.
The log is located on a remote host and I'm connecting using the Net::OpenSSH
module.
I need to read the latest log file available.
In the shell I can locate the latest log with the following commands:
cd builds/5.7.1/5.7.1.126WRF_B/jboss-4.2.3/bin
ls -lat | grep '.log$' | tail -1
Which will return the latest log:
-rw-r--r-- 1 load other 2406173 Jul 11 11:53 18156.stdout.log
So in Perl I'd like to be able write something that locates and opens that log for reading.
When I have that log file, I want to print all lines that have a timestamp greater than a specified time. The specified timestamp is a $Runtime
variable subtracted from the latest log message time.
Here are the last messages of the garbage collection log:
...
73868.629: [GC [PSYoungGen: 941984K->14720K(985216K)] 2118109K->1191269K(3065984K), 0.2593295 secs] [Times: user=0.62 sys=0.00, real=0.26 secs]
73873.053: [GC [PSYoungGen: 945582K->12162K(989248K)] 2122231K->1189934K(3070016K), 0.2329005 secs] [Times: user=0.60 sys=0.01, real=0.23 secs]
So if $Runtime
had a value of 120 seconds, I would need to print all the lines from timestamp (73873.053 - 120) seconds up.
In the end my script would look something like this...
open GARB, ">", "./report/archive/test-$now/GC.txt" or die "Unable to create file: $!";
my $ssh2 = Net::OpenSSH->(
$pathHost,
user => $pathUser,
password => $pathPassword
);
$ssh2->error and die "Couldn't establish SSH connection: ". $ssh2->error;
# Something to find and open the log file.
print GARB #Something to return certain lines.
close GARB;
I realize this is somewhat similar to this question, but I can't think of a way to tailor it to what I'm looking for. Any help is greatly appreciated!