#! /usr/bin/perl -w # apache log colouriser by andrew wales # http://www.meow.org.uk/stan/pet_projects/ # # released under the zlib license # http://www.opensource.org/licenses/zlib-license.html use strict; my %attr = ( 'reset' => 0, 'bright' => 1, 'dim' => 2, 'underscore' => 4, 'blink' => 5, 'reverse' => 7, 'hidden' => 8, 'fg' => '3', 'bg' => '4', ); my %color = ( 'black' => 0, 'red' => 1, 'green' => 2, 'yellow' => 3, 'blue' => 4, 'magenta' => 5, 'cyan' => 6, 'white' => 7, ); $|=1; $SIG{'INT'} = \&exit_handler; $SIG{'QUIT'} = \&exit_handler; my $mailmsg = ""; my $mailfilesize = my $mailcheckinterval = 0; if (defined $ENV{"MAIL"}) { my @stat = stat($ENV{"MAIL"}); $mailfilesize = $stat[7]; $SIG{"ALRM"} = \&mailcheck; $mailcheckinterval = 60; alarm $mailcheckinterval; } while (defined (my $line=<>)) { chomp($line); next if ($line eq ""); # by default this will parse my quirky apache logs. uncomment the next line # to parse more traditional logs instead # my ($hostname, $ident, $remoteuser, $timestamp, $request, $status, $size, $referer, $useragent) = ($line =~ m/^(.*?) +(.*?) +(.*?)+(\[.*?\]) +\"(.*?)\" +(\d+) +([\d-]*) +\"(.*?)\" +\"(.*?)\"/); my ($hostname, $ident, $remoteuser, $timestamp, $request, $status, $size, $referer, $useragent, $xforward) = ($line =~ m/^(.*?) +(.*?) +(.*?)+(\[.*?\]) +\"(.*?)\" +(\d+) +([\d-]*) +\"(.*?)\" +\"(.*?)\" +\"(.*?)\"/); print "\r"; print "\e[",$attr{"bright"},";",$attr{"fg"},$color{"red"},"m"; print "$hostname"; print "\e[",$attr{"bright"},";",$attr{"fg"},$color{"green"},"m"; print " $timestamp"; print "\e[",$attr{"bright"},";",$attr{"fg"},$color{"cyan"},"m"; print " \"$request\""; print "\e[",$attr{"bright"},";",$attr{"fg"},$color{"white"},"m"; print " \"$referer\""; print "\e[",$attr{"reset"},";",$attr{"fg"},$color{"yellow"},"m"; print " \"$useragent\""; print "\e[",$attr{"bright"},";",$attr{"fg"},$color{"yellow"},"m"; print " \"$xforward\""; print "\e[",$attr{"reset"},"m"; print "\n"; print $mailmsg; } sub exit_handler { print "\e[",$attr{"reset"},"m"; print "\r\e[K"; exit(0); } sub mailcheck { my @stat = stat($ENV{"MAIL"}); if (($stat[8] < $stat[9]) && ($stat[7] > $mailfilesize)) { $mailmsg = "[new mail awaits]"; $mailfilesize = $stat[7]; print "\r\a",$mailmsg; } alarm $mailcheckinterval; }