跟据nginx日志统计ip访问量排名

很多时候想知道哪些ip访问量最多,所以写了下面的脚本来统计一下,然后找出这些IP来自哪个国家的。

#www.yiyou.org
use strict;
use 5.010;
use Geo::IP;
use Data::Dumper;

my $file=shift;

open(FH,"$file") or die $!; 
our %counter;
my $test=1;

my $gi = Geo::IP->open("GeoLiteCity.dat", GEOIP_STANDARD);

while(<FH>){
	chomp $_;
	if(/((\d{1,3}\.){3}\d{1,3})/){
		#say $1;
		$counter{$1}+=1;
	}

}

foreach (sort{$counter{$b}<=>$counter{$a}} keys %counter){
	next if $counter{$_} < 1000;
	my $record = $gi->record_by_addr($_);
	my $country_code=$record->{country_code};
	print "$country_code $_ => $counter{$_}\n";
}