On 21 Jul 2004, [EMAIL PROTECTED] wrote:

> I need make a report with 
> 
>     "DATE"-"FROM"-"TO"-"SIZE"
> 
> of all mail sent to @xyz.com
> 
> I use multilog to log stmp and qmail.
> I need a script ( sed / awk / grep / c / ... etc ) or software.

I wrote something I find useful.  It will work with TAI64 filtered or
with the raw data.  It only analyzes qmail-smtpd output.

Ted

#!/usr/bin/perl -w

# by Ted Zlatanov <[EMAIL PROTECTED]>
# GPL license

use strict;
use Data::Dumper;

my %messages;

while (<>)
{
 if (m/(.*?) qmail-smtpd (\d+): (.*)/)
 {
  my $id = $2;
  my $line = $3;
  $messages{$id}->{date} = $1;

  if ($line =~ m/mail from: (\S+)/)
  {
   $messages{$id}->{from} = $1;
  }
  elsif ($line =~ m/rcpt to: (\S+)/)
  {
   push @{$messages{$id}->{to}}, $1;
  }
  elsif ($line =~ m/size (\d+) bytes/)
  {
   $messages{$id}->{size} = $1;
  }
 }
}

printf "ID         date               from\n";

foreach my $id (sort { $a <=> $b } keys %messages)
{
 next unless exists $messages{$id}->{from};
 next unless exists $messages{$id}->{to};
 next unless exists $messages{$id}->{date};
 next unless exists $messages{$id}->{size};

 printf "%-10d %s %10s %30s\n\t%s\n",
  $id,
   $messages{$id}->{date},
  human($messages{$id}->{size}),
  $messages{$id}->{from},
   join ("\n\t", map { "to: $_" } @{$messages{$id}->{to}})
}

# get a human-readable size
sub human
{
 my $i = shift @_;
 my @sizes = qw/k m g/;
 my $size = '';

 do
 {
  $i /= 1024;
  $size = shift @sizes;
 }
 while ($i > 1024 && @sizes);

 return sprintf '%.2f%s', $i, $size;
}

Reply via email to