Skip to content

Instantly share code, notes, and snippets.

@coldfire-x
Created June 6, 2013 10:20
Show Gist options
  • Save coldfire-x/5720581 to your computer and use it in GitHub Desktop.
Save coldfire-x/5720581 to your computer and use it in GitHub Desktop.
get ip percentage from nginx log
#!/usr/bin/perl
use NetAddr::IP;
unless (@ARGV) {
print "usage: $0 file1 file2 file3\n";
exit;
}
my $mini = 0;
my $total = 0;
# miniserver subnet infomation
my @mini_server_subnet = qw(
xxx.xxx.xxx.xxx/23
xxx.xx.xx.xx/10
);
# create mini server subnet objects
my @mini_servers;
for my $subnet (@mini_server_subnet) {
my $network = NetAddr::IP->new($subnet);
push @mini_servers, $network;
}
sub is_mini {
my $ip = shift;
$ip = NetAddr::IP->new($ip);
for my $mini_subnet (@mini_servers) {
return 1 if $ip->within($mini_subnet);
}
return 0;
}
for my $file (@ARGV) {
next if !-e $file;
print "\n Handling $file .....\n";
open my $nav_log, '<', $file or die "can not open $file: $!";
for(<$nav_log>) {
$total++;
chomp;
# check if this ip is from mini server
if (/^\[(.*?)\]/) {
my $ip = $1;
$mini++ if is_mini($ip);
}
# print info for ervery 100000 lines
if ($total%100000==0) {
print "processed $total lines\n";
print "mini percentage: ", $mini/$total, "\n";
}
}
print "processed $total lines\n";
print "mini percentage: ", $mini/$total, "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment