Skip to content

Instantly share code, notes, and snippets.

@zouguangxian
Created July 6, 2012 06:29
Show Gist options
  • Save zouguangxian/3058401 to your computer and use it in GitHub Desktop.
Save zouguangxian/3058401 to your computer and use it in GitHub Desktop.
analyze access.log to get bandwidth statistics, perl version
#!/usr/bin/env perl
use Date::Parse;
use Date::Format;
use List::Util qw[min max];
$\="\n";
my %slot=();
my $STEP = 60;
while ($line=<>) {
chomp($line);
if(not $line =~ /\[(.*?)\] ".*?" (\d*) (\d*) (\d*) (\d*)/) {
next;
}
my $t = str2time($1);
my $bytes = $4;
my $duration = $5;
if ($duration == 0) {
$slot{$t} += $bytes;
next;
}
$s = 1.0*$bytes/$duration;
for(my $i = $t; $i < $t + $duration; $i +=1) {
$slot{$i} += $s;
}
};
$_last = 0;
$_max = 0;
for my $key (sort keys %slot) {
my $ds = int($key / $STEP) * $STEP;
if($ds == $_last) {
$_max = max($_max, $slot{$key});
} else {
if ($_last == 0) {
$_last = $ds;
$_max = $slot{$key};
} else {
@lt = localtime($_last);
print join("\t", strftime("%d/%b/%YT%H:%M:%S",@lt), sprintf("%f",$_max));
$_last = $ds;
$_max = $slot{$key};
}
}
}
if ($_last != 0) {
@lt = localtime($_last);
print join("\t", strftime("%d/%b/%YT%H:%M:%S",@lt), sprintf("%f",$_max));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment