leech-detector 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 2 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, write to the
  17. # Free Software Foundation, Inc.
  18. # 59 Temple Place, Suite 330
  19. # Boston, MA 02111-1307 USA
  20. #
  21. # $Id: leech-detector,v 1.1 2004/07/09 06:39:32 as Exp $</p>'
  22. use Time::ParseDate;
  23. while (<STDIN>) {
  24. m/^(\S+) \S+ \S+ \[(.*?)\] ".*?" (\d+)/ or die "Cannot parse:\n$_";
  25. $ip = $1;
  26. $code = $3;
  27. $time = parsedate($2);
  28. $count{$ip}++;
  29. $first{$ip} = $time unless $first{$ip};
  30. $last{$ip} = $time;
  31. $status{$ip} = () unless exists $status{$ip};
  32. $status{$ip}{$code}++;
  33. $total++;
  34. }
  35. @result = sort {$count{$b} <=> $count{$a}} keys %count;
  36. foreach $ip (@result) {
  37. $avg = 0;
  38. if ($first{$ip} and $last{$ip} and $count{$ip} > 1) {
  39. $avg = ($last{$ip} - $first{$ip}) / ($count{$ip} -1);
  40. }
  41. printf "%20s %10d %3d%% %7s %s\n", $ip, $count{$ip},
  42. 100 * $count{$ip} / $total,
  43. $avg ? sprintf('%.1fs', $avg) : '',
  44. join(', ', map { sprintf("%3d (%d%%)",
  45. $_,
  46. 100 * $status{$ip}{$_} / $count{$ip})
  47. } sort { $status{$ip}{$b} <=> $status{$ip}{$a} } keys %{$status{$ip}});
  48. }