ephemeric has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

To be honest, I'm lost, I only know the basics.
I would like to show the total bytes transferred per user in the Squid access log.
I have managed to cobble the below to show my total usage.
Do I use a hash and assign an array of byte values to each key/value pair of each user in the hash?
I've looked at lists, linked lists, hashes of hashes, arrays of arrays etc. but it's overwhelming.
Basically, parse the log, take the user and store the value, next user in the log, store the value, match same user, take the bytes field and correlate to the user. Parse the whole log, add all byte values per user to total up and print.
#!/usr/bin/perl use strict; use warnings; my $bytes; open(FILE, "squid.log") or die $!; while(<FILE>) { next unless m/\d+\s\d+\s\d+.\d+.\d+.\d+\s\w+\/\d+\s(\d+)\s\w+\s(.*? +)\s(?!-)\w+\s\w+\/(?!172.\d+.\d+.\d+)/; #1257981327.964 212098 172.16.32.142 TCP_MISS/200 3037 CONNECT mail +.google.com:443 rgabriel DIRECT/172.16.32.14 - $bytes += $1; } close(FILE); print "$bytes\n";

Replies are listed 'Best First'.
Re: Squid Usage per User
by Anonymous Monk on Nov 28, 2009 at 15:48 UTC
Re: Squid Usage per User
by Anonymous Monk on Nov 28, 2009 at 17:00 UTC
    #!/usr/bin/perl -- use strict; use warnings; use Regexp::Log::Common; my $rlog = Regexp::Log::Common->new(); $rlog->capture(qw' remotehost bytes '); my $rlogre = $rlog->regexp; my %remotes; { use autodie qw' open close '; open my($file), '<', "squid.log"; while(<$file>){ if(/$rlogre/){ $remotes{$1} += $2; } } close $file; } for(sort keys %remotes){ printf "%10d %s\n", $remotes{$_}, $_; }