#!/usr/bin/perl # # List the most frequent messages in the apache error log file # # Usage: errstats.pl error_log # # Earl Fogel, February 2000 $mincount = 5; # ignore errors which occur fewer times than this $minpercent = 0.25; # ignore errors which account for less than this # percent of the total $skipimages = 0; # include gif,jpg,ico... files in report $DocumentRoot = "/var/www/html"; while ($_ = $ARGV[0], /^-/) { shift; last if /^--$/; /^-image/ && ($skipimages = 1) && next; /^-mincount=(.*)/ && ($mincount=$1) && next; /^-minpercent=(.*)/ && ($minpercent=$1) && next; &usage; } while (<>) { next if /\[notice\]/; # we're only interested in errors chop; s/^\[.*?\] *//; # throw away [date] s/^\[.*?\] *//; # throw away [warning|error|...] my $client = $1 if s/^\[client ([^\]]+)\] *//; s#$DocumentRoot##o; # throw away Document Root # # some common types of errors # if (/^access to (.*) failed for .* reason: (.* does not exist)/ || /^File does not exist: (.*)/) { my $file = $1; next if $skipimages && $file =~ /\.(gif|jpg|jpeg|ico)$/i; $badfile{$file}++; # file does not exist } elsif (/^access to (.*) failed for .* reason: (script not found)/ || /^script not found or unable to stat: (.*)/) { $badscript{"$1"}++; # script does not exist } elsif (/^access to (.*) failed for .* reason: (malformed .* script)/) { $errscript{"$1"}++; # bad header from script } elsif (/^access to (.*) failed for .* reason: krb[^:]*: (.*)/) { $badperms{"$1 Kerberos $2"}++; # kerberos permission problems } elsif (/^.*Permission denied: (.*)/) { $badperms{"$1"}++; # permission problems } else { s/ for \d+\.\d+\.\d+\.\d+//; s/^\d+\.\d+\.\d+\.\d+ +//; s/(lost connection to client).*/$1/; s/, referer: .*//; $err{"$_"}++; } } &DisplayTop(*badfile,"File does not exist"); &DisplayTop(*badscript,"Script not found or unable to stat"); &DisplayTop(*errscript,"Malformed header from script"); &DisplayTop(*badperms,"Bad permissions"); &DisplayTop(*err,"Other errors"); sub DisplayTop { local(*hash,$heading) = @_; my($n,$limit,$msg,$total); if (defined(%hash)) { $n = keys(%hash); foreach $msg (keys(%hash)) { $total += $hash{$msg}; delete $hash{$msg} if $hash{$msg} < $mincount; } foreach $msg (keys(%hash)) { delete $hash{$msg} if $hash{$msg} < $total * ($minpercent / 100); } $limit = keys(%hash); return unless $limit; print "\n$heading (displaying top $limit of $n lines):\n"; foreach $msg (sort {$hash{$b}-$hash{$a}} keys(%hash)) { printf("%5d %s\n",$hash{$msg},$msg); } } }