Comment in en, and some code cleanup in util_parse_mail_in

This commit is contained in:
Daniel Berteaud 2016-04-19 15:21:04 +02:00
parent a6074b8ca2
commit 3514ca3349
1 changed files with 19 additions and 16 deletions

View File

@ -18,10 +18,13 @@
# along with Foobar; if not, write to the Free Software # along with Foobar; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# This script parse qpsmtpd logs (which must be sent to STDIN), and count the number
# of emails rejected by each plugins, and those accepted. You need to logterse plugin enabled
my $what = $ARGV[0] || ''; my $what = $ARGV[0] || '';
# On initialise nos compteurs a 0 # This is the list of plugins we can get stats for
# you can set the regex used to identify a line in the logs
my %denied = ( my %denied = (
dnsbl => qr{dnsbl\s+90}, dnsbl => qr{dnsbl\s+90},
rhsbl => qr{rhsbl\s+90}, rhsbl => qr{rhsbl\s+90},
@ -47,14 +50,15 @@ foreach (keys %denied, @others){
while (<STDIN>) { while (<STDIN>) {
my $line = $_; my $line = $_;
# on limites aux lignes concernant logterse # We only want logterse lines like
# @400000004994ad092afa867c 18386 logging::logterse plugin: etc... # @400000004994ad092afa867c 18386 logging::logterse plugin:
# selon la version de qpsmtpd, les lignes logterse peuvent varier # The format can slightly change depending on qpsmtpd version
next unless $line =~ m/^\@[0-9a-f]{24} \d+( \((queue|deny)\))? logging::logterse/; next unless $line =~ m/^\@[0-9a-f]{24} \d+( \((queue|deny)\))? logging::logterse/;
# D'abord on traite tout ceux qui contiennent 'msg denied before queued' # Lets count all the message which have been denied 'msg denied before queued'
if ($line =~ m/msg denied before queued/){ if ($line =~ m/msg denied before queued/){
$cnt{total_denied}++; $cnt{total_denied}++;
# Now try to find the plugin responsible for the deny
foreach (keys %denied){ foreach (keys %denied){
if ($line =~ m/$denied{$_}/){ if ($line =~ m/$denied{$_}/){
$cnt{$_}++; $cnt{$_}++;
@ -63,26 +67,27 @@ while (<STDIN>) {
next; next;
} }
# Les messages refuses par spamassassin # Rejected by spamassassin because spam score is too high
elsif ($line =~ m/spam score exceeded threshold/){ elsif ($line =~ m/spam score exceeded threshold/){
$cnt{spam_denied}++; $cnt{spam_denied}++;
next; next;
} }
# Spam mis en queue # Tagged as spam, but kept accepted
elsif ($line =~ m/queued.*Yes/){ elsif ($line =~ m/queued\s+<.*>\s+Yes,\s+(score|hits)=/){
$cnt{spam_queued}++; $cnt{spam_queued}++;
next; next;
} }
# Enfin, les bon mails # Queued, not tagged as spam, those are the clean emails
elsif ($line =~ m/queued.*No/){ elsif ($line =~ m/queued\s+<.*>\s+No,\s+(score|hits)=/){
$cnt{queued}++; $cnt{queued}++;
next; next;
} }
} }
# Caclul des totaux: # Now lets count other_denied, which is total_denied minus
# all the known plugins denied
$cnt{other_denied} = $cnt{total_denied}; $cnt{other_denied} = $cnt{total_denied};
foreach (keys %denied){ foreach (keys %denied){
$cnt{total} = $cnt{total} + $cnt{$_}; $cnt{total} = $cnt{total} + $cnt{$_};
@ -92,21 +97,19 @@ foreach (@others){
$cnt{total} = $cnt{total} + $cnt{$_} if ($_ !~ /total/); $cnt{total} = $cnt{total} + $cnt{$_} if ($_ !~ /total/);
} }
# Si l'argument est "print" on affiche toutes les stats # The print argument prints all on stdout
if ($what eq "print"){ if ($what eq "print"){
foreach (keys %denied,@others){ foreach (keys %denied,@others){
print "$_: $cnt{$_}\n"; print "$_: $cnt{$_}\n";
} }
} }
# Si l'argument correspond a un compteur definit # If the arg is a known plugin, prints only its value
# On affiche uniquemment cette valeur
elsif (defined $cnt{$what}){ elsif (defined $cnt{$what}){
print "$cnt{$what}\n"; print "$cnt{$what}\n";
} }
# Sinon, on quitte avec une erreur # Else, print an error
else{ else{
print "supported items are: "; print "supported items are: ";
foreach (keys %denied, @others){ foreach (keys %denied, @others){