Skip to content
 

Easy Way to Watch Your Web Server Log

Based on an idea from John Graham-Cumming regarding watching his log files with single characters, I have written a brief perl program that prints a single character based on the status code logged to my apache log file. Simply change the file in the second line to the apache access log file you’d like to watch.


#!/usr/bin/perl
$in="/path-to/your/web-server-access.log";
open(INFILE,"tail -f $in |") || die "Failed!\n";

my $old_fh = select(STDOUT);
$| = 1;
select($old_fh);

while(my $line = <INFILE>)
{
$temp = $line;
@fields = split(/ /,$line);
$x = $fields[8];
$s=substr($x,0,1);
if ($x eq "200") { $y=".";} #ok
elsif ($x eq "301") { $y=">";} #redirected
elsif ($x eq "403") { $y="F";} #forbidden
elsif ($x eq "206") { $y="P";} #partial
elsif ($x eq "404") { $y="X";} #not found
elsif ($x eq "304") { $y=".";} #ok - not modified
elsif ($x eq "400") { $y="B";} #bad request
elsif ($x eq "302") { $y=">";} #redirected
elsif ($s eq "1") { $y="1";} #Silent response codes
elsif ($s eq "2") { $y="2";} #Successful codes
elsif ($s eq "3") { $y="3";} #redirection
elsif ($s eq "4") { $y="4";} #request is incomplete
elsif ($s eq "5") { $y="5";} #server errors
else { $y="?".$x;} #anything else - should never happen, prints what was actually in the 8th field of the record.
print $y;
}
close(INFILE);

The output on one of my site’s included:

.............................>....>.....>..>FFFFXXX....................

Each period is a valid request; the > is a redirected request; the X is a 404 (file not found); and the F is a forbidden request. It’s been interesting watching traffic flow by. If you have suggestions for this program, please let me know.