【发布时间】:2012-07-13 15:25:18
【问题描述】:
我正在编写一个 Perl 脚本,我需要从垃圾收集日志中捕获一些行并将它们写入文件。
日志位于远程主机上,我正在使用Net::OpenSSH 模块进行连接。
我需要阅读最新的可用日志文件。
在 shell 中,我可以使用以下命令找到最新的日志:
cd builds/5.7.1/5.7.1.126WRF_B/jboss-4.2.3/bin
ls -lat | grep '.log$' | tail -1
这将返回最新的日志:
-rw-r--r-- 1 load other 2406173 Jul 11 11:53 18156.stdout.log
所以在 Perl 中,我希望能够编写一些东西来定位并打开该日志以供阅读。
当我拥有该日志文件时,我想打印时间戳大于指定时间的所有行。指定的时间戳是从最新的日志消息时间中减去的$Runtime 变量。
这是垃圾收集日志的最后一条消息:
...
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]
因此,如果 $Runtime 的值为 120 秒,我需要打印从时间戳 (73873.053 - 120) 秒开始的所有行。
最后我的脚本看起来像这样......
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;
我意识到这有点类似于this 问题,但我想不出一种方法来定制它以适应我正在寻找的内容。非常感谢任何帮助!
【问题讨论】:
标签: perl logging ssh garbage-collection