#!/usr/bin/perl # History # 1.0.1 - 07/09/1997 Updated script to handle cookie log # created by Apache 1.2 as well as Apache 1.1 # # 1.0 - 07/10/1996 Initial version. 8-) # $version="1.0.1"; # 'ctime.pl' and 'localtime.pl' are standard parts # of the perl library. # In Other Words: Don't write me asking about them # - they should *already* be on your system. require 'ctime.pl'; require 'timelocal.pl'; # Strip '?' parameters (0=don't strip it, 1= do strip it) $STRIPTARGETPARMS=1; # Strip '#' anchors (0=don't strip it, 1= do strip it) $STRIPANCHORS=1; # Exclusion patterns (uncomment to use) # This excludes any URL matching this pattern # from the report. $EXCLUDEREFSTO='\.(gif|png|xbm|jpg)$'; # Inclusion patterns (uncomment to use) # This includes *ONLY* URLs matching the pattern # in the report. # $INCLUDEONLYREFSTO='(\.html?|/)$'; # Blank out any URL seqment matchin this pattern # (Good for compacting virtual URL spaces) $BLANKOUT='(index.html?|/part\d+|/\d+)$'; # default path to the cookie_log $CookieLog="/wwwlogs/netimages/cookie_log"; # Name of server $HTTPDSERVER='www.netimages.com'; # Log type (apache) $LogType="apache"; &Initialize; @ARGV = ("$CookieLog") if ($#ARGV == -1); foreach $LogFile (@ARGV) { if (! open(COOKIESLOG,$LogFile) ) { print STDERR "Can't open $LogFile. Skipped.\n $!"; next; } while($line=) { chop $line; # Count the number of hits before doing anything $rawhitscounter++; if ($LogType eq 'apache') { ($cookie,$method,$target,$protocal,$day,$month,$year,$hour,$minute,$second,$time,$zone) = $line =~ m#^(\S+)\s\"?(\S+)\s(\S*)\s(\S*)\"?\s\[(\d\d?)/(\w\w\w)/(\d\d\d\d):(\d\d?):(\d\d):(\d\d)\s+(\S+)\]#o; $day = "0$day" if (length($day) == 1); $hour = "0$hour" if (length($day) == 1); $datestamp = "${year}$NameToMonth{$month}${day}${hour}${minute}${second}"; } next if (! ($target)); $target=~ s/\%7[eE]/~/o; # Caniconalize %7E and %7e as ~ $target=~ s#//#/#go; # Remove any extra slashes $target=~ s#^ *$#/#o; # fix root ref if needed $target=~ s/#.+$//o; # combine #anchor refs with root doc $target=~ s/\?.*$//o if $STRIPTARGETPARMS; # strip '?' parameters $target=~ s/\#.*$//o if $STRIPANCHORS; # strip '#' anchors $target=~ s#${BLANKOUT}##o if $BLANKOUT; # Dump anything we have specifically excluded #if we are using an exclusion pattern. next if ($EXCLUDEREFSTO && ($target=~m#$EXCLUDEREFSTO#io)); # Dump anything not matching our inclusion patter # if we are using one. next if ($INCLUDEONLYREFSTO && (!( $target=~m#$INCLUDEONLYREFSTO#io))); # Count the total number of lines analyzed $refscounter++; # Count total number of hits on each target $TotalTargetCounter{$target}++; # Count total number of cookies $CookiesCounter++ if (! defined($TotalCookieCounter{$cookie})); # Count total number of hits by each cookie $TotalCookieCounter{$cookie}++; $EndDate = $datestamp if ($EndDate < $datestamp); $StartDate = $datestamp if ($StartDate > $datestamp); # Keep track of the first hit on this target if (defined $TargetFirstDate{$target}) { if ($TargetFirstDate{$target} > $datestamp) { $TargetFirstDate{$target}=$datestamp; } } else { $TargetFirstDate{$target}=$datestamp; } # Keep track of the last hit on this target if (defined $TargetLastDate{$target}) { if ($TargetLastDate{$target} < $datestamp) { $TargetLastDate{$target}=$datestamp; } } else { $TargetLastDate{$target}=$datestamp; } # Keep track of the first hit by this cookie if (defined $CookieFirstDate{$cookie}) { if ($CookieFirstDate{$cookie} < $datestamp) { $CookieFirstDate{$cookie}=$datestamp; } } else { $CookieFirstDate{$cookie}=$datestamp; } # Keep track of the last hit by this cookie if (defined $CookieLastDate{$cookie}) { if ($CookieLastDate{$cookie} > $datestamp) { $CookieLastDate{$cookie}=$datestamp; } } else { $CookieLastDate{$cookie}=$datestamp; } # Keep track of unique URLs visited for each cookie # and unique cookies for each URL visited $key = "$target $cookie"; if (! defined($TargetCookieTuple{$key})) { $TargetUniqueCookies{$target}++; $CookieUniqueTargets{$cookie}++; $TargetCookieTuple{$key}++; } } close(COOKIESLOG); } if ($OutputFile) { open (OUTPUTFILE,">$OutputFile") || die ("Could not open $OutputFile for writing.\n$!"); select(OUTPUTFILE); } if ($CookiesCounter == 0) { &NoCookies; exit; } $date=&ctime(time); $avhits=int(10 * $refscounter/$CookiesCounter) /10; $End=&ConvertDate($EndDate); $Start=&ConvertDate($StartDate); foreach $cookie (keys(%TotalCookieCounter)) { $TimeOnSite=&DiffDates($CookieFirstDate{$cookie},$CookieLastDate{$cookie}); $TotalTime+=$TimeOnSite; } $avtime=int($TotalTime/$CookiesCounter); print <<"EOF"; Cookie Statistics for ${HTTPDSERVER}

Cookie Statistics for ${HTTPDSERVER}

$rawhitscounter raw references examined;
$refscounter references used in final statistics generation.

Start Date of Report: $Start
End Date of Report: $End
Number of Cookies Assigned: $CookiesCounter
Average Number of Hits Per Assigned Cookie: $avhits
Average Time Spent on Site: $avtime seconds

Cookie breakdown by file accessed

Last Accessed                  Cookies  File
-------------------------------------------------------------------------------
EOF
foreach $target (sort keys(%TargetUniqueCookies)) {

	$date=&ConvertDate($TargetLastDate{$target});
	chop $date;
	$cookies=$TargetUniqueCookies{$target};
	$file=&htmlize($target);
	printf "%-25s %12s  %-s\n",$date,$cookies,$file;
}



print <<"EOF";
-------------------------------------------------------------------------------
CookieStats $version / snowhare\@netimages.com
EOF select(STDOUT); sub htmlize { local($line)=$_[0]; $line=~s/&/\&/go; $line=~s/>/\>/go; $line=~s/