#!/usr/local/bin/perl # ============================================================================ # # statparse # statparse is a little program that parses stat's output # ============================================================================ # # ============================================================================ # # Configuration # ============================================================================ # # statFile is the server path of the statistics file you set stat.pl to record to my $statFile = "stat.txt"; # ============================================================================ # # Program - Don't mess! # ============================================================================ # use strict; use CGI; my $page = new CGI; my (@entries, @entryDayOrder, @entryMonthOrder, @entryYearOrder); my (%statHash, %uniqueIPHash); my ($currentMday, $currentMonth, $currentYear) = &getDate(); print $page->header; print qq~ statparse
statparse - $currentMday $currentMonth $currentYear

~; # Read the stats file open(STATFILE, $statFile); my @stats = ; close STATFILE; # Step through the input file for (my $i=0; $i <= $#stats; $i+=5) { my $timestamp = $stats[$i]; my $host = $stats[$i+1]; my $host_ip = $stats[$i+2]; my $user_agent = $stats[$i+3]; # Build an entry string my $entry = $timestamp.$host.$host_ip.$user_agent."\n"; # Tidy things up chomp($timestamp, $host_ip); # Get current date my ($entryTime, $day, $monthday, $month, $year) = &parseDate($timestamp); # Add this entry to the big list push(@entries, $entry); # Update statistic hashes $statHash{"$year"}++; $statHash{"$month $year"}++; $statHash{"$monthday $month $year"}++; # Add the entry to the daily string $statHash{"entry $monthday $month $year"} .= $entry if ($currentMonth eq $month && $currentYear eq $year); # Add the entries to the respective day, month, year lists so we can pull statistics out of the hash in the correct order. # No duplicates push (@entryDayOrder, "$monthday $month $year") if ($entryDayOrder[$#entryDayOrder] ne "$monthday $month $year"); push (@entryMonthOrder, "$month $year") if ($entryMonthOrder[$#entryMonthOrder] ne "$month $year"); push (@entryYearOrder, "$year") if ($entryYearOrder[$#entryYearOrder] ne "$year"); # Check for unique IP - if unique, put a value in the slot $uniqueIPHash{$host_ip} = 1; } print "Stat File Totals

"; print ""; # Print totals print '"; # Print year total list print '"; # Print month total list print '"; # Print daily total list print '"; print "
Totals

'; print "Total Visitors: $#entries
"; print "Total Unique IPs: ".scalar(keys %uniqueIPHash)."
"; print "
Yearly Totals

'; print "$_: $statHash{$_}
" foreach (reverse @entryYearOrder); print "
Monthly Totals

'; print "$_: $statHash{$_}
" foreach (reverse @entryMonthOrder); print "
Daily Totals

'; foreach (reverse @entryDayOrder) { print "$_: $statHash{$_}
" if (/$currentMonth $currentYear/); } print "


"; # Print the entry list for this month print "Daily Entries (Current Month)

"; foreach (reverse @entryDayOrder) { my $entry = $statHash{"entry $_"}; $entry =~ s/\n/
\n/g; print "
  • $_
      $entry
    " if (/$currentMonth $currentYear/); } sub parseDate { my ($entryTime, $day, $date, $year) = split /, /, shift; my ($month, $monthday) = split / /, $date; return ($entryTime, $day, $monthday, $month, $year); } sub getDate { # Arrays for the days & months my @shortdays = ("Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"); my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); # Snag the current time my ($sec,$min,$hr,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); # Fix the current time $year = $year + 1900; return ($mday, $months[$mon], $year); }