2010-06-24 36 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

  • 以供將來參考,如果您發佈一些代碼,你會得到更好的反應