#!perl/bin/perl

# hitcnt1.pl
#
# Second version.  Goes through the entire IIS log file directory.
# the hits and IP addresses from which they came.
# Command-line version -- output to screen.

# Define path to a log directory.

    $LogDir = "c:/winnt/system32/logfiles";

# Open the directory; die if it's not possible.

    opendir (LOGD, $LogDir) || die "Can't open $LogDir: $!\n";

# Get the list of log files into an array.

    @LogFiles = readdir (LOGD);

# Loop through the list, avoiding the . and .. entries.

    foreach $LogFile (@LogFiles)
        {
        if (($LogFile eq ".") || ($LogFile eq ".."))
            {
            next;
            }

    # Attempt to open the log file; die if it doesn't happen.

        $LogPath = $LogDir."/".$LogFile;
        open (LOG, $LogPath) || die "Can't open $LogPath: $!\n";

    # Loop through the log file a line at a time and extract
    # the entry information.

        $n = 0;                # Initialize a counter.

        while (<LOG>)
            {
            ($ClientIP, $Dummy, $Date, $Time, $SvcName,
                $SrvrName, $SrvrIP, $CPUTime, $BytesRecv,
                $BytesSent, $SvcStatus, $NTStatus, $Operation,
                $Target, $Dummy) = split (/,/);

        # Store the client IP address, increment counter.

            $IPArray[$n] = $ClientIP;
            $n++;
            }                    # end while (<LOG>)

        close (LOG);            # Close the log file.

    # Store the total hits, then initialize two arrays for the IPs
    # and the number of hits for each.

        $TotalHits = $n;
        @IPHits = ();
        @NumHits = ();
        $HitCount = 0;

    # Loop through @IPArray and sort out the IPs that match,
    # incrementing that IPs hit count for each match.

        for ($n = 0, $i = 0; $n < $TotalHits; $n++)
            {
            for ($p = 0; $p < $HitCount; $p++)
                {
                if ($IPArray[$n] eq $IPHits[$p])
                    {
                    $NumHits[$p]++;
                    last;                    # Same as break in C
                    }
                }

        # If $p == $HitCount, no matches were found.  This is a new
        # IP address, so add it to the list.

            if ($p == $HitCount)
            {
                $IPHits[$HitCount] = $IPArray[$n];
                $NumHits[$HitCount]++;
                $HitCount++;
            }
        }                    # end for ($n = 0...)

    # Print out the results after formatting date from file name.

        if ($LogFile =~ /(..)(..)(..)(..)/)
            {
            $year = $2;
            $month = $3;
            $day = $4;
            }

        print "On $month/$day/$year:\n\n";

        for ($n = 0; $n < $HitCount; $n++)
            {
            print "$IPHits[$n] registered $NumHits[$n] hits\n";
            }

        print "\n";        # Extra line.

    }                    # end foreach $LogFile...

# Close the directory.

    closedir (LOGDIR);

#                    End hitcnt1.pl
