#!/usr/bin/perl # NOTE: This was one of my first scripts. There are some ugly # things in here. Please forgive me :) # # ACCESS.CGI # by Matt Kruse 6/11/95 # # PURPOSE # This script makes it easy for anyone who has a WWW page on your site to # check out their access statistics by simply loading up a WWW page and # putting in their username or other string to search for. # example useage: http://cs.sau.edu/util/access.html # # LEGAL # This script is free and can be used or modified however you want (although # I would appreciate it if you would keep my name and e-mail address on the # output page). # # USAGE # This script works for the access_log of ncsa httpd 1.4. I'm not sure how # other servers differ, so I can't say how much modifcation might be needed. # All that is required for this script to work is a FORM that has an input # named TEXT, which is the string to search for. #timelocal is a standard perl library require "timelocal.pl"; # cgi-lib is a set of routines for CGI use. Do a search to find it if you # don't already have it. require "cgi-lib.pl"; $ACCESS_LOG="/usr/local/etc/httpd/logs/access_log"; &ReadParse(*input); print &PrintHeader; $TEXT=$input{'TEXT'}; $TEXT=~ s/\W//g; #make this safer %MONTHS=('Jan',0,'Feb',1,'Mar',2,'Apr',3,'May',4,'Jun',5, 'Jul',6,'Aug',7,'Sep',8,'Oct',9,'Nov',10,'Dec',11); %DAYS=(0,'Sun',1,'Mon',2,'Tue',3,'Wed',4,'Thu',5,'Fri',6,'Sat'); sub DAY { local($date,$month,$year)=@_; $year =~ s/19(..)/$1/; $time=&timelocal(0,0,0,$date,$MONTHS{$month},$year); return $DAYS{(localtime($time))[6]}; } open (LOG,"$ACCESS_LOG"); while () { next unless /$TEXT/; $TOTAL++; @INFO=split; $site=$INFO[0]; $time=$INFO[3]; ($date,$month,$year,$hour)= ($time=~ m|(..)/(...)/(....):(..)|); $hour =~ s/0(\d)/$1/g; $day=&DAY($date,$month,$year); $DAY{$day}++; $url=$INFO[6]; $FROM{$site}++; $TIME{$hour}++; $URL{$url}++; } close(LOG); # Print out Report # ---------------- 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.


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; } print "

Accesses by hour of the day

\n"; print "Each # = $VALUE accesses

\n

\n"; for ($i=15;$i>0;$i--) { for ($j=0;$j<24;$j++) { if (int($TIME{$j}/$VALUE) >= $i) {print " # ";} else { print " "; } } print "\n"; } print "--------------------------------------------------------------------------\n"; print " 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23\n\n"; print "\n"; print "
\n"; #Figure accesses by day #---------------------- $HIGHEST=0; foreach $key (keys %DAY) { if ($DAY{$key} > $HIGHEST) {$HIGHEST=$DAY{$key}; } } $VALUE=int($HIGHEST/30); if ($VALUE == 0) { $VALUE=1; } print "

Accesses by day of the week

\n"; print "* = $VALUE logins

\n"; print "

\n"; print " Sunday: "; print '*' x int($DAY{'Sun'}/$VALUE); print "($DAY{'Sun'})\n"; print " Monday: "; print '*' x int($DAY{'Mon'}/$VALUE); print "($DAY{'Mon'})\n"; print " Tuesday: "; print '*' x int($DAY{'Tue'}/$VALUE); print "($DAY{'Tue'})\n"; print " Wednesday: "; print '*' x int($DAY{'Wed'}/$VALUE); print "($DAY{'Wed'})\n"; print " Thursday: "; print '*' x int($DAY{'Thu'}/$VALUE); print "($DAY{'Thu'})\n"; print " Friday: "; print '*' x int($DAY{'Fri'}/$VALUE); print "($DAY{'Fri'})\n"; print " Saturday: "; print '*' x int($DAY{'Sat'}/$VALUE); print "($DAY{'Sat'})\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 <
This script written by Matt Kruse (mkruse@saunix.sau.edu). E-mail me for more information, or with any comments.


EOF