#!/usr/local/bin/perl require 'cgi-lib.pl'; $DEBUG=1; # Parse standard input if (&ReadParse) { print &PrintHeader; } else{ print "No string inputted\n"; exit; } ####Build log file names and concatenate # Grab query string and parse log file $SDATE=$in{'sdate'}; $EDATE=$in{'edate'}; ($SMTH,$SDAY,$SYR)=split("/",$SDATE); ($EMTH,$EDAY,$EYR)=split("/",$EDATE); # Make sure start date occurs before end date $SNUM=$SYR.$SMTH.$SDAY; $ENUM=$EYR.$EMTH.$EDAY; if ($SNUM > $ENUM) { print "Start date must occer before end date\n"; exit; } # Remove leading 0. $SMTH =~ s/^0//; $EMTH =~ s/^0//; $SDAY =~ s/^0//; $EDAY =~ s/^0//; #Remove any previous log files left undeleted. `rm /usr/tmp/accesslogfile*`; $LOGDIR="/usr/local/www/httpd/log/"; $EXT=getppid; $ACCESS_LOG="/usr/tmp/accesslogfile".$EXT; #Initialize date from which to start processing $PMTH=$SMTH; $PDAY=$SDAY; $PYR=$SYR; #Process across months while ($PMTH<=$EMTH) { #Loop for all log files within same month as month being processed. while ($PDAY<=@mth_lday[$PMTH-1]) { if ($PMTH==$EMTH && $PDAY>$EDAY) { last; } (($PDAY > 9) && ($FPDAY=$PDAY)) || ($FPDAY="0".$PDAY); # Get month name as literal $LMTH=@mths[$PMTH-1]; $F_NAME="$LOGDIR"."httpd-log.".$LMTH.$FPDAY.$PYR; $dt="$LMTH"." "."$PDAY"."$PYR"; #Concatenate file to access log if (-e $F_NAME) { `cat $F_NAME >> $ACCESS_LOG`; } else { #print "File $F_NAME does not exist\n"; } $PDAY++; #print "$F_NAME \n" if $DEBUG==1; } #Increment month $PMTH++; #Reset present day $PDAY=1; } $RHOST=$ENV{'REMOTE_HOST'}; $TEXT=$in{'qstring'}; if ($TEXT) { open (LOG,"grep $TEXT ${ACCESS_LOG}|"); } else { print "No document found for this topic\n"; exit; } #@LINE=(); #Discard remote host server entries @LINE=grep(!/$RHOST/,); close(LOG); $BEGIN=$LINE[0]; $BEGIN=(split(' ',$BEGIN))[3]; $BMONTH=(split('/',$BEGIN))[1]; $MONTH=$BMONTH; $BDATE=(split('/',$BEGIN))[0]; $BDATE=substr($BDATE,1); foreach $k (0..$#LINE) { @INFO=split(' ',@LINE[$k]); $TOTAL++; ($site,$time)=split('\[',@LINE[$k]); $site=(split(' ',$site))[0]; $hour=(split(':',$time))[1]; $hour =~ s/0(\d)/$1/g; $date=(split(':',$time))[0]; $date=~s/\[//g; ($DAYNO,$MONTH,$YEAR)=split("/",$date); $DAYNO=~ s/^0//; foreach $i (0..$#mths) { if ($MONTH eq @mths[$i]) { $MTH=$i+1; last; } } if ($TMP{$date}) { $day=$TMP{$date}; } else { # Get day of week, given a date using a version of zeller's congruence $day = &weekday($YEAR,$MTH,$DAYNO); $TMP{$date}=$day; } $day=~s/Sun/Sunday/; $day=~s/Mon/Monday/; $day=~s/Tue/Tuesday/; $day=~s/Wed/Wednesday/;$day=~s/Thu/Thursday/; $day=~s/Fri/Friday/; $day=~s/Sat/Saturday/; $DAY{$day}++; $url=$INFO[6]; $FROM{$site}++; $TIME{$hour}++; $URL{$url}++; } $border="BORDER=10"; # Print out Report # ---------------- print "\n"; print <Search Results

Search Results


The following is the result of doing a search for $TEXT in the access_log for the World Wide Web server on this machine. The first date in the log is $BMONTH ${BDATE}.


TOTAL ACCESSES: $TOTAL
EOF $HIGHEST=0; foreach $key (keys %TIME) {if ($TIME{$key} > $HIGHEST){$HIGHEST=$TIME{$key};}} $VALUE=int($HIGHEST/15); if ($VALUE == 0) { $VALUE=1; } foreach $key (keys %TIME) { $barsize{$key} = int(($TIME{$key} * 250) / $HIGHEST); } print "

Accesses by hour of the day

\n"; print "
\n"; foreach $key (0..23) { ($barsize{$key} < 2) && ($barsize{$key}=2); print < $TIME{$key}
EOF } print "
\n\n"; foreach $key (0..23) { print "\n"; } print "\n
$key
\n


\n"; #Figure accesses by day #---------------------- $HIGHEST=0; undef %barsize; foreach $key (keys %DAY) { if ($DAY{$key} > $HIGHEST) {$HIGHEST=$DAY{$key}; } } foreach $key (keys %DAY) { $barsize{$key} = int(($DAY{$key} * 400) / $HIGHEST); } print "

Accesses by day of the week

\n"; print "\n"; foreach $key (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday) { print< EOF } print "
$key: $DAY{$key}
\n"; print "


\n"; print "


\n"; sub by_value { $FROM{$b} <=> $FROM{$a}; } sub by__value { $URL{$b} <=> $URL{$a}; } print "

Pages Accessed

\n"; foreach $key (sort by__value keys %URL) { print "$URL{$key}: $key
\n"; } print "
\n"; print "

Requests From

\n"; foreach $key (sort by_value keys %FROM) { print " $FROM{$key}: $key
\n"; } print "\n"; #remove log file `rm ${ACCESS_LOG}`; print <

This output was generated by a script originally written by Matt Kruse and rewritten by Olufemi Anthony.
femibyte\@ritz.mordor.com



EOF