Nathan Pralle - www.nathanpralle.com
Kickass Phone Rates from 3U
PLACES TO GO:  
Back to Software Print Version
WATCH WEBLOG

Scripts.com Freshmeat The Perl Archive HotScripts

#!/usr/bin/perl -w
use strict;
use Socket;

###############################################################
# WATCH WEBLOG - Apache log watching utility
# Copyleft 2005 - Nathan E. Pralle
#
# DESCRIPTION:  This utility will allow you to watch, interactively,
# an Apache access_log for a website.  It shows you the domain accessing,
# how many times they have done so, and what page or resource they are
# requesting.  This utility accepts input from standard in (STDIN) so
# it is best coupled with 'tail'
#
# It employs a DNS cache to enable faster lookups as well.
#
# SYNTAX:  tail -f location/to/access_log | watch_weblog.pl
#
# REQUIRED: Perl Sockets (default install)
#
# CONTACT:  Questions?  Comments?
#           http://www.nathanpralle.com/contact.html
#################################################################


my $line;
my $domainstring;
my @temparray;
my %iparray;
my $counter=0;
my $hitcounter=0;
my %domainlookups;
print "Watch Weblog\n";
print "Copyleft (C)2005 Nathan E. Pralle\n";
print "\n";
while(){
	chomp;
	$line=$_;
	$counter++;
	@temparray=split(/ /,$line);
	my $lookup=$domainlookups{$temparray[0]};
	my $host;
	if(!defined $lookup){
		my $iaddr=inet_aton("$temparray[0]");
		$host=gethostbyaddr($iaddr,AF_INET);
	}
	else{
		$hitcounter++;
		$host=$lookup;
	}
	if($host){
		$domainlookups{$temparray[0]}=$host;
		if($host=~/[0-9]$/){
			$domainstring=$host;
		}
		else{
			my @domainarray=split(/\./,$host);
			my $limit=scalar @domainarray;
			$limit--;
			if(length($domainarray[$limit])<3){
				$domainstring=$domainarray[$limit - 2].'.'.$domainarray[$limit - 1].'.'.$domainarray[$limit];
			}	
			else{
				$domainstring=$domainarray[$limit - 1].'.'.$domainarray[$limit];
			}
		}
	}
	else{
		$domainstring=$temparray[0];
		$domainlookups{$temparray[0]}=$temparray[0];
	}
	my $num=$iparray{$domainstring};
	if($num){
		$iparray{$domainstring}=$num+1;
	}
	else{
		$iparray{$domainstring}=1;
	}
	print pad_blank_front($domainstring,25)."  ".pad($iparray{$domainstring},4)."    $temparray[6]\n";
}
exit;


sub pad{
        my $thing=shift;
        my $len=shift;
        while(length($thing)<$len){
                $thing="0".$thing;
        }
        return $thing;
}

sub pad_blank_front{
        my $thing=shift;
        my $len=shift;
        while(length($thing)<$len){
                $thing=" ".$thing;
        }
        return $thing;
}



This site and all content (C)2002-2008 Nathan E. Pralle (www.nathanpralle.com).