#!perl/bin/perl

# hitcnt.pl
#
# First version.  Goes through a single IIS log file, tallying
# the hits and IP addresses from which they came.
# Command-line version -- output to screen.

# Define path to a single log file.

    $LogDir = "c:/winnt/system32/logfiles/";
    $LogFile = "in970502.log";
    $LogPath = $LogDir.$LogFile;

# Attempt to open the log file; die if it doesn't happen.

    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.

    print "On 05/02/97:\n\n";

    for ($n = 0; $n < $HitCount; $n++)
        {
        print "$IPHits[$n] registered $NumHits[$n] hits\n";
        }

#                    End hitcnt.pl
