#!/usr/bin/perl

# squid-bw.pl
#
# By Brian D. Davison
#
# Last Modified: 10 June 2001
#
# Goals:
#    Measure overall bytes retrieved, and average bandwidth used (B/s)
#    Measure byes sent per proxy, and ave. bandwidth used
#
# Reads squid log files
# Does not consider header bytes (important for 304, 404, etc.)

use Socket;

while (<STDIN>) {
    local($line) = $_;

#    scanf($line, "%f %d %s %s %d %s %s %s %s %s", $timestamp, $delay,
#	   $ip, $status, $size, $method, $url, $ig1, $ig2, $type);
    ($timestamp, $delay, $ip, $status, $size, $method, $url, $ig1, $ig2, $type) = split(' ', $line);

#    print "Got $url on $timestamp from $ip using $method for $size bytes.\n";

    $byteserved{$ip} += $size;
    if (!$starttime{$ip}) {
	$starttime = $timestamp - $delay;
    }
    $endtime{$ip} = $timestamp;

    $lines++;
}

print "\n$lines log entries analyzed.\n";
print "hostname (ip) MBytes retrieved, Bytes/sec (between first and last req)\n\n";

foreach $i (keys %byteserved)
{
    # need to use ip address
#    $iaddr = inet_aton($i); # or whatever address
#    $name  = gethostbyaddr($iaddr, AF_INET);

#    printf("%s (%s) %.3f %f\n", $name, $i, $byteserved{$i}/1024/1024, $byteserved{$i} / ($endtime{$i} - $starttime{$i}));
    printf("%s %.3f %f\n", $i, $byteserved{$i}/1024/1024, $byteserved{$i} / ($endtime{$i} - $starttime{$i}));
}



