Files
oddmuse/scripts/leech-detector
2015-04-03 10:39:22 +02:00

50 lines
1.8 KiB
Perl
Executable File

#!/usr/bin/perl
# leech-detector -- analyze logs on stdin and print a summary on stdout
# Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.
# 59 Temple Place, Suite 330
# Boston, MA 02111-1307 USA
#
# $Id: leech-detector,v 1.1 2004/07/09 06:39:32 as Exp $</p>'
use Time::ParseDate;
while (<STDIN>) {
m/^(\S+) \S+ \S+ \[(.*?)\] ".*?" (\d+)/ or die "Cannot parse:\n$_";
$ip = $1;
$code = $3;
$time = parsedate($2);
$count{$ip}++;
$first{$ip} = $time unless $first{$ip};
$last{$ip} = $time;
$status{$ip} = () unless exists $status{$ip};
$status{$ip}{$code}++;
$total++;
}
@result = sort {$count{$b} <=> $count{$a}} keys %count;
foreach $ip (@result) {
$avg = 0;
if ($first{$ip} and $last{$ip} and $count{$ip} > 1) {
$avg = ($last{$ip} - $first{$ip}) / ($count{$ip} -1);
}
printf "%20s %10d %3d%% %7s %s\n", $ip, $count{$ip},
100 * $count{$ip} / $total,
$avg ? sprintf('%.1fs', $avg) : '',
join(', ', map { sprintf("%3d (%d%%)",
$_,
100 * $status{$ip}{$_} / $count{$ip})
} sort { $status{$ip}{$b} <=> $status{$ip}{$a} } keys %{$status{$ip}});
}