leech-detector 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/perl
  2. # leech-detector -- analyze logs on stdin and print a summary on stdout
  3. # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. use Time::ParseDate;
  18. while (<STDIN>) {
  19. m/^(\S+) \S+ \S+ \[(.*?)\] ".*?" (\d+)/ or die "Cannot parse:\n$_";
  20. $ip = $1;
  21. $code = $3;
  22. $time = parsedate($2);
  23. $count{$ip}++;
  24. $first{$ip} = $time unless $first{$ip};
  25. $last{$ip} = $time;
  26. $status{$ip} = () unless exists $status{$ip};
  27. $status{$ip}{$code}++;
  28. $total++;
  29. }
  30. @result = sort {$count{$b} <=> $count{$a}} keys %count;
  31. foreach $ip (@result) {
  32. $avg = 0;
  33. if ($first{$ip} and $last{$ip} and $count{$ip} > 1) {
  34. $avg = ($last{$ip} - $first{$ip}) / ($count{$ip} -1);
  35. }
  36. printf "%20s %10d %3d%% %7s %s\n", $ip, $count{$ip},
  37. 100 * $count{$ip} / $total,
  38. $avg ? sprintf('%.1fs', $avg) : '',
  39. join(', ', map { sprintf("%3d (%d%%)",
  40. $_,
  41. 100 * $status{$ip}{$_} / $count{$ip})
  42. } sort { $status{$ip}{$b} <=> $status{$ip}{$a} } keys %{$status{$ip}});
  43. }