#!/usr/bin/perl use strict; use warnings; use Geo::IP; use YAML; my $gi = Geo::IP->open("/usr/share/GeoIP/GeoIP.dat", GEOIP_MEMORY_CACHE); my $total_bandwidth = 560 * 1024 * 1024; my $total_bytes = 0; my %country_bytes; while (my $record = <>) { if ($record =~ /^\d+\.\d+\s+\S+\s+".*"\s+(\d+\.\d+\.\d+\.\d+)\s+".*"\s+(\d+)$/) { my $ip = $1; my $bytes = $2; my $country = $gi->country_code_by_addr($ip); if (defined($country) && $country ne "A1" && $country ne "A2" && $country ne "01" && $country ne "--") { $country_bytes{$country} += $bytes; } $total_bytes += $bytes; } else { warn $record; } } my %country_bandwidth; while (my($country,$bytes) = each %country_bytes) { $country_bandwidth{$country} = $bytes * $total_bandwidth / $total_bytes; } print Dump(\%country_bandwidth); exit 0;