通过监听文件日志统计连接情况的perl脚本

数据库 waitig 493℃ 百度已收录 0评论
#!/usr/bin/perl
my $timestamp;
my $file_name;
my $listHost=false;
if ($#ARGV<2 || $#ARGV > 4){
	print "Usage: -t 'time rexge' [-h] filename\n"
}else{
	while ($ARGV = shift) {
		if ($ARGV =~ m/-t/i) {
			$timestamp = shift;
		} elsif ($ARGV =~ /-h/i) {
			$listHost=true;
		} else{
			$file_name = $ARGV;
		}
	}
}
my %res ;
my %tol;

open FILE_HD, "<", "$file_name";

while (defined($line = <FILE_HD>)) {
	    if ($line =~ /^($timestamp)[:print:]*/) {
			      my $time = $1 ;
				        $tol{$time}++;
						      if ( $line =~ /\(HOST=(\d+\.\d+\.\d+\.\d+)\)/ ) {                                                                                       
								           my $host_client = $1 ;
										            $res{$time}{$host_client}++ ;
													      }
														      }
}

my @res_key =sort by_liter keys %res;

foreach $loop (@res_key) {
	    if ( $loop == '' )
			      { print "No time.\n";}
				      else
						        { print "$loop => TOTAL:$tol{$loop}\n" ;}
								if($listHost =~ /true/){
									    my $host_res = $res{$loop} ;
										    while ( ($key, $value) = each %$host_res ) {
												        print "\t$key \t=>\t $value\n";
														        }
																    }
}
sub by_liter {
	     $a cmp $b ;                 # ASCIIbetically by name
		     }

样例:

 ./1.pl -h -t ’24-MAY-2016 16:[4-5]+’  test.log
24-MAY-2016 16:4 => TOTAL:218
10.11.108.30 =>
1
10.37.232.51 =>
4
10.33.241.30 =>
19
10.21.0.120 =>
3
10.33.241.171
=> 13
10.11.100.100
=> 37
10.11.100.191
=> 10
10.21.0.116 =>
1
10.33.31.146 =>
8
10.33.241.28 =>
21
10.33.241.163
=> 6


本文由【waitig】发表在等英博客
本文固定链接:通过监听文件日志统计连接情况的perl脚本
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)