| 1 |
7 |
dpavlin |
#!/usr/bin/perl -w |
| 2 |
|
|
|
| 3 |
|
|
# 2002-06-11 Dobrica Pavlinusic <dpavlin@rot13.org> |
| 4 |
|
|
# |
| 5 |
|
|
# Unroll sendmail and/or postfix log on message-by-message basis |
| 6 |
|
|
# |
| 7 |
|
|
# Usage: |
| 8 |
|
|
# $ cat */mail.log | ./flow_mail.pl | vi -R - |
| 9 |
|
|
# |
| 10 |
|
|
|
| 11 |
|
|
$|=1; # flush stdout |
| 12 |
|
|
|
| 13 |
|
|
my $debug = 0; |
| 14 |
|
|
|
| 15 |
|
|
my $nr=0; |
| 16 |
|
|
|
| 17 |
|
|
my %id2nr_hash; |
| 18 |
|
|
my %nr2id_hash; |
| 19 |
|
|
|
| 20 |
|
|
my %nr_line; |
| 21 |
|
|
my %id_line; |
| 22 |
|
|
|
| 23 |
|
|
my @t = ('','',0); # placeholder for time stamp |
| 24 |
|
|
|
| 25 |
|
|
print STDERR "reading log from stdin...\n 0---------12---------24"; |
| 26 |
|
|
|
| 27 |
|
|
sub fix_id { |
| 28 |
|
|
if (my $id=$_[0]) { |
| 29 |
|
|
# $id =~ tr/a-zA-Z0-9/_/cs; |
| 30 |
|
|
$id =~ s/:$//; |
| 31 |
|
|
return $id; |
| 32 |
|
|
} |
| 33 |
|
|
} |
| 34 |
|
|
|
| 35 |
|
|
sub id2nr { |
| 36 |
|
|
# "clever" way to find message number -- try current one, |
| 37 |
|
|
# if that fails use first one... |
| 38 |
|
|
my $id = $_[0]; |
| 39 |
|
|
if (grep(/$nr/,@{$id2nr_hash{$id}})) { |
| 40 |
|
|
return $nr; |
| 41 |
|
|
} else { |
| 42 |
|
|
return $id2nr_hash{$id}[0]; |
| 43 |
|
|
} |
| 44 |
|
|
} |
| 45 |
|
|
|
| 46 |
|
|
sub push_id2nr { |
| 47 |
|
|
my ($id,$nr) = @_; |
| 48 |
|
|
if (! grep(/$nr/,@{$id2nr_hash{$id}})) { |
| 49 |
|
|
push @{$id2nr_hash{$id}}, $nr; |
| 50 |
|
|
} |
| 51 |
|
|
} |
| 52 |
|
|
|
| 53 |
|
|
sub push_nr2id { |
| 54 |
|
|
my ($nr,$id) = @_; |
| 55 |
|
|
if (! grep(/\Q$id\E/,@{$nr2id_hash{$nr}})) { |
| 56 |
|
|
push @{$nr2id_hash{$nr}}, $id; |
| 57 |
|
|
} |
| 58 |
|
|
} |
| 59 |
|
|
|
| 60 |
|
|
|
| 61 |
|
|
while(<>) { |
| 62 |
|
|
chomp; |
| 63 |
|
|
chomp; |
| 64 |
|
|
my $line = $_; |
| 65 |
|
|
my @arr=split(/\s+/,$line); |
| 66 |
|
|
if ($arr[5] && $arr[5] =~ m/^[a-zA-Z0-9]*:$/) { |
| 67 |
|
|
my $id = fix_id($arr[5]); |
| 68 |
|
|
print STDERR "id: $id\n" if ($debug); |
| 69 |
|
|
if (! defined id2nr($id)) { |
| 70 |
|
|
$nr++; |
| 71 |
|
|
print STDERR "new: $nr -- $id\n" if ($debug); |
| 72 |
|
|
push_id2nr($id,$nr); |
| 73 |
|
|
push_nr2id($nr,$id); |
| 74 |
|
|
push @{$nr_line{$nr}}, $line; |
| 75 |
|
|
push @{$id_line{$id}}, $line; |
| 76 |
|
|
} else { |
| 77 |
|
|
my $c_nr=id2nr($id); |
| 78 |
|
|
print STDERR "line: $c_nr -- $id\n" if ($debug); |
| 79 |
|
|
if ($arr[9] && $arr[12] && $arr[9] eq "status=sent") { |
| 80 |
|
|
# postfix follow |
| 81 |
|
|
my $f_id=fix_id($arr[12]); |
| 82 |
|
|
push_id2nr($f_id,$c_nr); |
| 83 |
|
|
push_nr2id($c_nr,$f_id); |
| 84 |
|
|
print STDERR "follow postfix: $c_nr -> $f_id -- ",join("|",@arr),"\n" if ($debug); |
| 85 |
|
|
} elsif ($arr[15] && $arr[15] eq "stat=Sent") { |
| 86 |
|
|
# sendmail follow |
| 87 |
|
|
if (my $f_id=fix_id($arr[19])) { |
| 88 |
|
|
push_id2nr($f_id,$c_nr); |
| 89 |
|
|
push_nr2id($c_nr,$f_id); |
| 90 |
|
|
print STDERR "follow sendmail: $c_nr -> $f_id -- ",join("|",@arr),"\n" if ($debug); |
| 91 |
|
|
} |
| 92 |
|
|
} |
| 93 |
|
|
push @{$nr_line{$c_nr}}, $line; |
| 94 |
|
|
push @{$id_line{$id}}, $line; |
| 95 |
|
|
} |
| 96 |
|
|
} else { |
| 97 |
|
|
# this is a cludge. If you don't have much other |
| 98 |
|
|
# log entries excpt mailers your progress indicator won't |
| 99 |
|
|
# be updated often enough. Since I do, I save a lot |
| 100 |
|
|
# of computing this way :-) |
| 101 |
|
|
|
| 102 |
|
|
my $h = $arr[2] || ''; $h =~ s/:.*//g; |
| 103 |
|
|
|
| 104 |
|
|
if ($t[0] ne $arr[0] || $t[1] != $arr[1]) { |
| 105 |
|
|
printf STDERR "\n%3s %2d ",$arr[0],$arr[1]; |
| 106 |
|
|
@t = ($arr[0], $arr[1], 0); |
| 107 |
|
|
} |
| 108 |
|
|
if ($h != $t[2]) { |
| 109 |
|
|
print STDERR "." x ($h - $t[2]); |
| 110 |
|
|
$t[2] = $h; |
| 111 |
|
|
} |
| 112 |
|
|
} |
| 113 |
|
|
} |
| 114 |
|
|
|
| 115 |
|
|
print STDERR "\nprocessed $nr messages\n"; |
| 116 |
|
|
|
| 117 |
|
|
print STDERR "saving...\n0","-"x21,"50%","-"x21,"100%\n."; |
| 118 |
|
|
|
| 119 |
|
|
my $last_pcnt=0; |
| 120 |
|
|
|
| 121 |
|
|
for ($i = 1; $i < $nr; $i++) { |
| 122 |
|
|
print "-" x 60,"\n"; |
| 123 |
|
|
if ($nr2id_hash{$i}) { |
| 124 |
|
|
print "nr: $i id: ",join(", ",@{$nr2id_hash{$i}}),"\n"; |
| 125 |
|
|
# foreach (@{$nr_line{$i}}) { |
| 126 |
|
|
# print "$_\n"; |
| 127 |
|
|
# } |
| 128 |
|
|
foreach $id (@{$nr2id_hash{$i}}) { |
| 129 |
|
|
foreach (@{$id_line{$id}}) { |
| 130 |
|
|
print "$_\n"; |
| 131 |
|
|
} |
| 132 |
|
|
} |
| 133 |
|
|
} |
| 134 |
|
|
my $pcnt = int($i*50/$nr); |
| 135 |
|
|
if ($pcnt > $last_pcnt) { |
| 136 |
|
|
print STDERR "."; |
| 137 |
|
|
$last_pcnt = $pcnt; |
| 138 |
|
|
} |
| 139 |
|
|
} |
| 140 |
|
|
|
| 141 |
|
|
print STDERR "\nover...\n"; |
| 142 |
|
|
|