#!/usr/bin/perl -w
#
# tpop3dtraffic:
# A trivial script to extract traffic statistics from log files written
# to by tpop3d.
#
# Usage: tpop3dtraffic [NUMBER] < /var/log/whatever
#
# Prints the top NUMBER users, or 10 if NUMBER is not specified.
#
# Copyright (c) 2002 Chris Lightfoot.
# Email: chris@ex-parrot.com; WWW: http://www.ex-parrot.com/~chris/
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

my $rcsid = '$Id$';

$num = $ARGV[0];
$num ||= 10;

%sess = ( );
%recv = ( );
%send = ( );

while (<STDIN>) {
    if (m#tpop3d.+client \[.+\]([^(]+).+ (\d+)/(\d+) bytes read/written#) {
        ++$sess{$1};
        $recv{$1} += $2;
        $send{$1} += $3;
    }
}

my @top = sort { $send{$b} <=> $send{$a} } keys %send;
print <<EOF;
User                                             Times  Received  Sent
------------------------------------------------ ------ --------- ---------
EOF
foreach (@top[0..$num - 1]) {
    printf("%48s (% 4d) % 9d % 9d\n", $_, $sess{$_}, $recv{$_}, $send{$_});
}
