Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1473127
  • 博文数量: 463
  • 博客积分: 10540
  • 博客等级: 上将
  • 技术积分: 5450
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-12 08:30
文章分类

全部博文(463)

文章存档

2014年(2)

2012年(14)

2011年(42)

2010年(18)

2009年(78)

2008年(35)

2007年(182)

2006年(92)

我的朋友

分类:

2009-12-30 10:05:04

#!/usr/bin/perl -w
use strict;
use vars qw(%format %datetime $flag);

sub usage
{
    print "Usage: Log_analyzers format start_time end_time filename\n";
    exit -1;
}

sub datetime
{
    my $j = 1;
    my $line = shift @_;
    my $string = shift @_;
    my $format_str = shift @_;
    my $time;
    if ($line =~ /$string/) {
        for (my $i = 0; $i < length($format_str); $i++) {
            my $char = substr($format_str, $i, 1);
            $datetime{$char} = eval "\$" . $j;
            #print "$datetime{$char}\n";
            $j++;
        }
        ($time = $datetime{'Y'}) if (exists $datetime{'Y'});
        ($time = $datetime{'y'}) if (exists $datetime{'y'});
        ($time = $time . $datetime{'m'}) if (exists $datetime{'m'});
        ($time = $time . $datetime{'d'}) if (exists $datetime{'d'});
        ($time = $time . $datetime{'H'}) if (exists $datetime{'H'});
        ($time = $time . $datetime{'I'}) if (exists $datetime{'I'});
        ($time = $time . $datetime{'k'}) if (exists $datetime{'k'});
        ($time = $time . $datetime{'l'}) if (exists $datetime{'l'});
        ($time = $time . $datetime{'M'}) if (exists $datetime{'M'});
        ($time = $time . $datetime{'S'}) if (exists $datetime{'S'});
        #print "$time ";
        if ($flag == 1) {
            $time = substr($time, 1, length($time -1));
        }
        return $time;
    } else {
        usage;
    }   
}

my ($date_format, $tmp, $time_start, $time_end, $string, $format_str, $keyword, $key, @filename);
$flag = 0;
%format = ('Y' => '(\d{4})',
           'y' => '(\d{2})',
           'm' => '([0-1]{1}\d{1})',
           'd' => '(\d{2})',
           'H' => '([0-2]{1}\d{1})',
           'M' => '(\d{2})',
           'S' => '(\d{2})',
           'I' => '([0-1]{1}\d{1})',
           'k' => '(\d{1,2})',
           'l' => '(\d{1,2})'
           );

if (@ARGV < 5) {
    usage;
} else {
    $date_format = shift @ARGV;
    usage unless ($date_format =~ /%/);
    $time_start = shift @ARGV;
    $time_end = shift @ARGV;
    while ($tmp = shift @ARGV) {
        if ($tmp =~ /^<(.*)>$/) {
            $key = $1; 
            $key =~ s/\./\\\./g;
            $key =~ s/\*/\\\*/g;
            $key =~ s/\^/\\\^/g;
            $key =~ s/\?/\\\?/g;
            $key =~ s/\$/\\\$/g;
            $key =~ s/\|/\\\|/g;
            $key =~ s/\]/\\\]/g;
            $key =~ s/\[/\\\[/g;
            $key =~ s/\\d/\\\\d/g;
            $key =~ s/\\D/\\\\D/g;
            $key =~ s/\\w/\\\\w/g;
            $key =~ s/\\W/\\\\W/g;
            $key =~ s/\\s/\\\\s/g;
            $key =~ s/\\S/\\\\S/g;
           
            if ($keyword) {
                $keyword = $keyword . '.*' . $key;
            } else {
                $keyword = $key;
            }
           
        } else {
            push(@filename, $tmp);
            #print "$tmp\n";
        }
    }
    #print "$date_format $time_start $time_end \n";
    #print "keyword $keyword \n";
    #print "filename @filename \n";
}

for (my $i = 0; $i < length($date_format); $i++) {
    my $char = substr($date_format, $i, 1);
    if ($char eq '%') {
        $i++;
        $char = substr($date_format, $i, 1);
        $format_str = $format_str . $char;
        #print "$format_str\n";
        if ($char eq 'y') {
            $flag = 1;
        }
        if (exists $format{$char}) {
            $string = $string . $format{$char};
        } else {
            usage;
        }
    } else {
        # if ($char eq '.') {
            # $char = '\.';
        # }
        $char =~ s/\./\\\./g;
        $char =~ s/\*/\\\*/g;
        $char =~ s/\^/\\\^/g;
        $char =~ s/\?/\\\?/g;
        $char =~ s/\$/\\\$/g;
        $char =~ s/\|/\\\|/g;
        $char =~ s/\]/\\\]/g;
        $char =~ s/\[/\\\[/g;
        $string = $string . $char;
    }
}
#print "$string\n";

my $t_start = datetime($time_start, $string, $format_str);
my $t_end = datetime($time_end, $string, $format_str);
#print "$t_start $t_end\n";

while (my $file = shift @filename) {
    unless (-r $file) {
        print "read file $file failed!\n";
        exit -2;
    }
    open (F, "$file") || die "open $file error!";
    while (my $lines = ) {
        if ($lines =~ /$keyword/) {
            my $tmp_time = datetime($lines, $string, $format_str);
            next if ($tmp_time < $t_start);
            last if ($tmp_time > $t_end);
            if ($tmp_time >= $t_start && $tmp_time <= $t_end) {
                print "$lines";
            }
        }
    }
    close(F);
}

阅读(969) | 评论(1) | 转发(0) |
0

上一篇:nginx配置SSL

下一篇:开公司的步骤

给主人留下些什么吧!~~

chinaunix网友2011-05-23 15:29:10

最新修改!!!