#!/usr/local/bin/perl #Name: stats: develop stats page from log files. $baseurl = "/home/grad23/doyle/.www/cgi-bin"; $| = 1; $logname= "$baseurl/logs/counter.log"; $totalfile= "$baseurl/logs/counter.txt"; $histdir = "$baseurl/../logs"; $spacing = 10; if (!(-e "$baseurl/countrys.txt")) { print "missing the countries file\n\n"; exit; } # get country codes for different hostnames open (CTRY, "$baseurl/countrys.txt")|| die "$0: can\'t open countrys.txt: $!\n"; while () { $line = $_; $line =~ /\A\s*(\w+)\s+(.*)/; push @ends, $1; push @ctrys, $2; } close (CTRY); #get total number of hits open (TOTAL, "$totalfile")|| die "$0: can\'t $totalfile: $!\n"; $total = ; close (TOTAL); &view_current if ($ARGV[0] eq 'view_current'); &roll_history if ($ARGV[0] eq 'roll_hist') ; &view_current; sub get_date { #generate date string mm/dd/yy ($mdy) @time = localtime(time); $mm = $time[4] + 1; $mm = "0$mm" if (length($mm) == 1); $dd = $time[3]; $dd = "0$dd" if (length($dd) == 1); $yy = $time[5]; $mdy = "$mm/$dd/$yy"; return $mdy; } sub get_month { #generate month mm-yy ($my) @time = localtime(time); $mm = $time[4] + 1; $mm = "0$mm" if (length($mm) == 1); $yy = $time[5]; $my = "$mm-$yy"; return $my; } sub gen_page { $today = &get_date; %servertypes=(); %doctypes=(); %referertypes=(); %browsertypes=(); %ostypes=(); %datetypes=(); %timetypes=(); $maxsize = 300; $maxdom =1; $maxdoc =1; $maxref =1; $maxtime=1; $maxdate=1; &bad_name unless (-e $logname); open(LOG_FILE,"$logname"); while(){ $line=$_; ( $tdate, $document, $referer, $hostname, $browser, $os, $user) = split(/\|/,$line); ($idate, $itime) = split(/\s+/,$tdate); #split browser and OS stuff, if needed. if ($os =~ /\A\s*\Z/) # if os type missing { $browser =~ /(\S+)\s+(\S+.*)/; $browser = $1; $os = $2; $os =~ s/\[.*\]\s+//; } else { $browser =~ s/\s+\S+.*//; } #get hostname if ($hostname =~ /([A-Za-z]+)$/) { #takes last word $type = $1; } else { #convert all numeric only addresses $type = "unknown (numeric)"; } if (defined $servertypes{$type}){ $servertypes{$type}++; if($servertypes{$type} > $maxdom){ $maxdom = $servertypes{$type}; } } else { $servertypes{$type} = 1; } if ($hostname =~/\.aol\.com/i) { if (defined $servertypes{"aol (America On-Line)"}) { $servertypes{"aol (America On-Line)"}++; } else { $servertypes{"aol (America On-Line)"} = 1; } } if ($hostname =~/\.umn\.edu/i) { if (defined $servertypes{"umn (U of MN)"}) { $servertypes{"umn (U of MN)"}++; } else { $servertypes{"umn (U of MN)"} = 1; } } #get document if (defined $doctypes{$document}){ #document $doctypes{$document}++; if($doctypes{$document} > $maxdoc){ $maxdoc = $doctypes{$document}; } } else { $doctypes{$document}=1; } #get referer if (($referer =~ /~doyle/) || ($referer =~ /\%7Edoyle/)) { $referer =~ s/\A.*?doyle//; } if ($referer =~ /\A\s*\Z/) { $referer = "unknown"; } if (defined $referertypes{$referer}){ #referer $referertypes{$referer}++; } else { $referertypes{$referer}=1; } #find browser $browser =~ s/(Mozilla\/\d+\.\d+).+/$1/; if ($browser =~ /\A\s*\Z/) { $browser = "unknown"; } if (defined $browsertypes{$browser}){ #browser $browsertypes{$browser}++; } else { $browsertypes{$browser}=1; } if ($os =~ /MSIE/) { $xtra= $os; $xtra =~ /(MSIE[\s\d\.]*)/; $xtra = $1; if (defined $browsertypes{$xtra}){ #browser $browsertypes{$xtra}++; } else { $browsertypes{$xtra}=1; } } $os =~ s/(.*?)via.*/$1/; $os =~ s/\A\((.*?)\)(.*)/$1$2/; $os =~ s/\;\s+[IU]\s*\Z//; $os =~ s/\;\s+[IU]\s*\;/;/; $os =~ s/X11\s*\;\s*//; $os =~ s/\A\s*//; $os =~ s/\s*\Z//; if (($os =~ /win98/i) || ($os =~ /windows\s98/i)) { $os = "Windows 98"; } if (($os =~ /win95/i) || ($os =~ /windows\s95/i)) { $os = "Windows 95"; } elsif (($os =~ /macintosh/i) || ($os =~ /macos/i) || ($os =~ /\sMac_/)){ $os = "Macintosh"; } elsif (($os =~ /windows\snt/i) || ($os =~ /winnt/i)) { $os = "Windows NT"; } elsif (($os =~ /windows\s*\;/i) || ($os =~ /windows\s3\./i)) { $os = "Windows 3.1"; } elsif (($os =~ /windows 16/i) || ($os =~ /win\s?16/i)) { $os = "Windows 16"; } elsif ($os =~ /webtv/i) { $os =~ s/([^\(]*).*/$1/; } elsif ($os =~ /linux/i) { $os = "Linux"; } elsif ($os =~ /sunos/i) { $os = "SunOS"; } elsif ($os =~ /HP-UX/) { $os = "HP-UX"; } elsif ($os =~ /IRIX/) { $os = "IRIX"; } elsif ($os =~ /OSF1/) { $os = "OSF1"; } elsif ($os =~ /AIX/) { $os = "AIX"; } elsif (($os =~ /\A\s*default\s*\Z/i) || ($os =~ /\A\s*undefined\s*\Z/i) || ($os =~ /\A\s*unknown\s*\Z/i) || ($os =~ /\A\s*\Z/i)) { $os = "Unknown"; } $os =~ s/\A\s*compatible;?\s*(.*)/$1/i; if (defined $ostypes{$os}){ #os $ostypes{$os}++; } else { $ostypes{$os}=1; } if (defined $datetypes{$idate}){ #datetypes $datetypes{$idate}++; if($datetypes{$idate}>$maxdate){ $maxdate = $datetypes{$idate}; } } else { $datetypes{$idate}=1; } ($ihh,$imm,$iss)= split (/:/,$itime); if (defined $timetypes{$ihh}){ #timetypes $timetypes{$ihh}++; if($timetypes{$ihh}>$maxtime){ $maxtime = $timetypes{$ihh}; } } else { $timetypes{$ihh} = 1; } $monthtotal++; } close(); print OUTPAGE "Monthly Access Stats\n"; print OUTPAGE "\n"; print OUTPAGE "This month, $monthtotal users have hit your webpage. Giving a total of $total.\n\n

"; # connect to history if ($show_hist == "yes") { print OUTPAGE "To see historical data, go here

"; } #----------------------------- SERVERTYPES ---------------------------- print OUTPAGE "\n"; print OUTPAGE "Domain Report:

\n\n"; $unitsize = $maxsize/$maxdom; $xsize = $maxdom/10; $xsize = int $xsize; if($xsize==0){ $xsize =1; } $xunitsizes = int $xsize*$unitsize+1; print OUTPAGE "Each "; print OUTPAGE "Represents $xsize users.

\n\n"; print OUTPAGE "\n"; print OUTPAGE "\n"; print OUTPAGE "
domain:#logins:
\n"; foreach $server (sort { $servertypes{$b} <=> $servertypes{$a}; } keys %servertypes) { for ($i=0; $i< $#ends; $i++) { if ($server =~ /\A$ends[$i]\Z/i) { $label = "\L$ends[$i]\E ($ctrys[$i])"; } } if ($label eq "") { print OUTPAGE "$server
\n"; } else { print OUTPAGE "$label
\n"; } $label = ""; } print OUTPAGE "
\n"; foreach $server (sort { $servertypes{$b} <=> $servertypes{$a}; } keys %servertypes) { $numb = int $servertypes{$server}*$unitsize+1; print OUTPAGE "$servertypes{$server}
\n"; } print OUTPAGE "
\n"; #----------------------------- DOCTYPES ---------------------------- print OUTPAGE "


"; print OUTPAGE "\n"; print OUTPAGE "Document Request Summary:

\n\n"; $unitsize = $maxsize/$maxdoc; $xsize = $maxdate/10; $xsize = int $xsize; if($xsize==0){ $xsize =1; } $xunitsizes = int $xsize*$unitsize+1; print OUTPAGE "Each "; print OUTPAGE "Represents $xsize users.

\n\n"; print OUTPAGE "\n"; print OUTPAGE "\n"; print OUTPAGE "
document:#hits:
\n"; foreach $doc (sort keys %doctypes){ print OUTPAGE "$doc
\n"; } print OUTPAGE "
\n"; foreach $doc (sort keys %doctypes){ $numb = int $doctypes{$doc}*$unitsize+1; print OUTPAGE "$doctypes{$doc}
\n"; } print OUTPAGE "
\n"; #----------------------------- REFTYPES ---------------------------- print OUTPAGE "


"; print OUTPAGE "\n"; print OUTPAGE "Referer Summary:

\n\n"; print OUTPAGE "\n"; print OUTPAGE "\n"; print OUTPAGE "
referer:#referred:
\n"; foreach $ref (sort keys %referertypes){ print OUTPAGE "$referertypes{$ref}
\n"; } print OUTPAGE "
\n"; foreach $ref (sort keys %referertypes){ # $numb = int $referertypes{$ref}*$unitsize; if (($ref =~ /\Ainternal\Z/) || ($ref =~ /\Aunknown\Z/)){ print OUTPAGE "$ref
\n"; } else { print OUTPAGE "$ref
\n"; } } print OUTPAGE "
\n"; #----------------------------- DATETYPES ---------------------------- print OUTPAGE "


"; print OUTPAGE "\n"; print OUTPAGE "Daily Access Summary:

\n\n"; $unitsize = $maxsize/$maxdate; $xsize = $maxdate/10; $xsize = int $xsize; if($xsize==0){ $xsize =1; } $xunitsizes = int $xsize*$unitsize+1; print OUTPAGE "Each "; print OUTPAGE "Represents $xsize users.

\n\n"; print OUTPAGE "\n"; print OUTPAGE "\n"; print OUTPAGE "
date:#logins:
\n"; foreach $datet (sort keys %datetypes){ $numb = int $datetypes{$datet}*$unitsize; print OUTPAGE "$datet
\n"; } print OUTPAGE "
\n"; foreach $datet (sort keys %datetypes){ $numb = int $datetypes{$datet}*$unitsize+1; print OUTPAGE "$datetypes{$datet}
\n"; } print OUTPAGE "
\n"; #----------------------------- HOURTYPES ---------------------------- print OUTPAGE "


"; print OUTPAGE "\n"; print OUTPAGE "Hourly Summary:

\n\n"; $unitsize = $maxsize/$maxtime; $xsize = $maxtime/10; $xsize = int $xsize; if($xsize==0){ $xsize =1; } $xunitsizes = int $xsize*$unitsize+1; print OUTPAGE "Each "; print OUTPAGE "Represents $xsize users.

\n\n"; print OUTPAGE "\n"; print OUTPAGE "\n"; print OUTPAGE "
hr:#logins:
\n"; foreach $hourt (sort keys %timetypes){ $numb = int $timetypes{$hourt}*$unitsize; print OUTPAGE "$hourt
\n"; } print OUTPAGE "
\n"; foreach $hourt (sort keys %timetypes){ $numb = int $timetypes{$hourt}*$unitsize+1; print OUTPAGE "$timetypes{$hourt}
\n"; } print OUTPAGE "
\n"; #----------------------------- BROSWERTYPES ---------------------------- print OUTPAGE "


"; print OUTPAGE "\n"; print OUTPAGE "Browser Version Report:

\n\n"; print OUTPAGE "\n"; print OUTPAGE "\n"; print OUTPAGE "
#browsers:browser version:
\n"; foreach $browsert (sort { $browsertypes{$b} <=> $browsertypes{$a} }keys %browsertypes){ print OUTPAGE "$browsertypes{$browsert}
\n"; } print OUTPAGE "
\n"; foreach $browsert (sort { $browsertypes{$b} <=> $browsertypes{$a} }keys %browsertypes){ print OUTPAGE "$browsert
\n"; } print OUTPAGE "
\n"; #----------------------------- OSTYPES ---------------------------- print OUTPAGE "


"; print OUTPAGE "\n"; print OUTPAGE "Operating System Report:

\n\n"; print OUTPAGE "\n"; print OUTPAGE "\n"; print OUTPAGE "
#os's:operating system:
\n"; foreach $osty (sort { $ostypes{$b} <=> $ostypes{$a} } keys %ostypes){ print OUTPAGE "$ostypes{$osty}
\n"; } print OUTPAGE "
\n"; foreach $osty (sort { $ostypes{$b} <=> $ostypes{$a} } keys %ostypes){ print OUTPAGE "$osty
\n"; } print OUTPAGE "
\n"; print OUTPAGE "\n"; } sub view_current { print "Content-type: text/html\n\n"; open OUTPAGE, ">&STDOUT"; $show_hist = "yes"; &gen_page; close OUTPAGE; } sub roll_history { $month = &get_month; #check existence of the directory and/or file $file = "$baseurl/logs/history/$month.html"; open OUTPAGE, ">$file" || die "$0: can\'t open $file: $!\n"; $show_hist = "no"; &gen_page; close OUTPAGE; chmod (0644, "$file"); truncate $logname,0; exit; } sub bad_name { print "Sorry, log $logname does not exist.\n"; exit; }