2010-06-24 58 views
-1

Perl新手在这里。我有一个日志文件,我需要解析出“备份成功”和任何“错误:”条目。我尝试使用unix cat解析日志文件并将其管道化为grep。我得到了我想要的信息,但是我想在perl中尝试这一点,并且还可以选择传递日期参数,并根据我需要的日期给出行。perl帮助根据时间输入解析日志文件

示例日志文件的输出:(备份成功)日志文件输出的

Wed Jun 09 06:14:25 2010: db2.cal.mil.mad:backup:INFO: flush-logs-time=00:00:00 
Wed Jun 09 06:14:25 2010: db2.cal.mil.mad:backup:INFO: backup-time=06:14:23 
Wed Jun 09 06:14:25 2010: db2.cal.mil.mad:backup:INFO: backup-status=Backup succeeded 
Wed Jun 09 06:14:25 2010: db2.cal.mil.mad:backup:INFO: Backup succeeded 

样品:(错误:)

Wed Jun 09 05:00:03 2010: rip1.mil.mad:backup:ERROR: mysql-zrm appears to be already running for this backupset 
Wed Jun 09 05:00:03 2010: rip1.mil.mad:backup:ERROR: If you are sure mysql-zrm is not running, please remove the file /etc/mysql-zrm/rip1.mail.mad/.mysql-zrm.pid and restart mysql-zrm 

**我想文字和/或电子邮件与此信息。像这样,但可以选择通过我需要的日期。

Wed Jun 09 05:00:03 2010: rip1.mil.mad:backup:ERROR: mysql-zrm appears to be already running for this backupset 
Wed Jun 09 05:00:03 2010: rip1.mil.mad:backup:ERROR: If you are sure mysql-zrm is not running, please remove the file /etc/mysql-zrm/rip1.mail.mad/.mysql-zrm.pid and restart mysql-zrm 
Wed Jun 09 06:14:25 2010: db2.cal.mil.mad:backup:INFO: backup-status=Backup succeeded 

如果您想为我提供一些perl代码和/或想法来开始。我将不胜感激。谢谢。

回答

1
#!/usr/bin/perl 

# usage example: <this script> Jun 09 2010 <logfile> 

use strict; 
use warnings; 

my ($mon,$day,$year) = ($ARGV[0],$ARGV[1],$ARGV[2]); 

open(FH,"< $ARGV[3]") or die "can't open log file $ARGV[3]: $!\n"; 

while (my $line = <FH>) { 
    if ($line =~ /.* $mon $day \d{2}:\d{2}:\d{2} $year:.*(ERROR:|Backup succeeded)/) { 
    print $line; 
} 
} 
+0

完美!这很好。正是我需要的。谢谢你的帮助! – jdamae 2010-06-25 03:02:08

1

这是一个简单的脚本。要扫描的文件名称和目标日期是硬编码的。匹配被打印到STDOUT。

顺便说一下,这段代码完全没有经过测试。我在浏览器中输入了文本框。

use strict; 
use warnings; 

my $logpath = './bar/log'; 
my $target = 'Jun 09 2010'; 

open my $fh, '<', $logpath or die "Error opening $logpath $!\n"; 

while (my $line = <$fh>) { 

    next unless date_match($target, $line); 

    next unless my $result = got_error($line) // got_backup($line);  

    print $result; 
} 

sub got_backup { 
    my $line = shift; 

    return unless $line =~ /backup-status=Backup succeeded/; 

    return $line; 
} 

sub got_error { 
    my $line = shift; 

    return unless $line =~ /:ERROR:/; 

    return $line; 
} 


# Take a line and a target date. Compare the date derived from the line to 
# the target, and returns true if they match. 
# Also always returns true if target is not defined 

sub date_match { 
    my $target = shift; 
    my $line = shift; 

    return 1 unless defined $target; # Always true if target is undefined. 

    # Where did that god-awful date format come from? Yech. 
    my $date = extract_date($line); 

    return $date eq $target; 
} 

# Simple extract of date using split and join with extra variables 
# to make it newbie friendly. 
# IMO, it would be a good idea to switch to using DateTime objects and 
# DateTime::Format::Strptime 

sub extract_date { 
    my $line = shift; 

    my @parts = split /:/, $line; 
    my $date = join ':' @parts[0..2]; 
    @parts = split /\s+/, $date; 

    $date = @parts[1,2,4]; 

    return $date;   
} 
  • 您可以使用Getopt::Long得到一个文件名和目标日期。

  • 这将是一个好主意,使用更强大的日期/时间解析和比较方案。 DateTime和朋友都非常好,功能强大的日期处理模块。去看一下。

  • 如果您正在处理大量数据并需要更高效,则可以通过多种方式避免在任何地方复制$line

  • 以供将来参考,如果您发布一些代码,你会得到更好的反应