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

#!/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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值