Last active
August 29, 2015 14:15
-
-
Save jigante/ebba09f1a7a2a0c773dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
## O original esta neste Link | |
## www.arschkrebs.de/postfix/scripts/mailqfmt.pl | |
## http://www.arschkrebs.de/postfix/scripts/ | |
# Postfix mailq file reformatter, (C) 2003 by Matthias Andree | |
# This file is licensed to you under the conditions of the | |
# GNU General Public License v2. | |
## Install perl-HTTP-Date, perl-JSON | |
use strict; | |
use warnings; | |
use Getopt::Long; | |
use HTTP::Date; | |
use JSON; | |
### LOOP DEFAULT | |
my $POSTQUEUE = "postqueue"; | |
my $POSTSUPER = "postsuper"; | |
my ($rec, $rsn, $total, $key); | |
my %hash_mail = (); | |
my %hash_to = (); | |
my @queue = `$POSTQUEUE -p`; | |
foreach my $queue(@queue) { | |
chomp $queue; | |
if ($queue =~ /^Mail queue is empty/) { print; next; } | |
if ($queue =~ /^-/) { next; } # skip header | |
if ($queue =~ /^\s+.*\@(.*)/ ) { $hash_to{$1}++;} | |
if ($queue =~ /^([0-9A-F]+)\s*([ !*])\s+(\d+)\s+(\S+\s+\S+\s+\d+\s+\d+:\d+:\d+)\s+(.+)$/) | |
{ | |
my ($qid, $qfl, $siz, $dat, $from) = ($1, $2, $3, $4, $5); | |
$rec="$qid from=$from"; | |
if ($from =~ /^(\w|\-|\_|\.)+\@((\w|\-|\_)+\.)+[a-zA-Z]{2,}$/){$hash_mail{$from}++;} | |
next; | |
} | |
} | |
### Variaveis Ambientes | |
my $DELETE = 0; | |
my $EMAIL = 0; | |
my $GETJSON = 0; | |
(&ListQueue()) if (!$ARGV[0]); | |
GetOptions ('json' => \$GETJSON, 'del' => \$DELETE, 'email=s' => \$EMAIL ) or ( &help() ); | |
sub ListQueue(){ | |
print "LISTA FILA TO DOMINIO\n"; | |
print "--------------------------------------------\n"; | |
foreach $key (reverse sort { $hash_to{$a} <=> $hash_to{$b}} keys %hash_to) { $total += $hash_to{$key}; print "$hash_to{$key} - $key \n"; } | |
print "\n$total -> TOTAL NA FILA TO\n"; | |
print "\nLISTA FILA FROM\n"; | |
print "--------------------------------------------\n"; | |
$total = 0; | |
foreach $key (reverse sort { $hash_mail{$a} <=> $hash_mail{$b}} keys %hash_mail) { | |
$total += $hash_mail{$key}; | |
print "$hash_mail{$key} - $key \n"; | |
} | |
print "\n$total -> TOTAL NA FILA FROM\n"; | |
} | |
if ($GETJSON) { &ListQueueJSON(); } | |
sub ListQueueJSON(){ | |
my (@from, @domain, @geral, $json); | |
$total = 0; | |
foreach $key (reverse sort { $hash_to{$a} <=> $hash_to{$b}} keys %hash_to) { | |
$total += $hash_to{$key}; | |
push @domain, {'domain' => $key, 'total' => $hash_to{$key}}; | |
} | |
push @domain, {'total_domain' => $total}; | |
$total = 0; | |
foreach $key (reverse sort { $hash_mail{$a} <=> $hash_mail{$b}} keys %hash_mail) { | |
$total += $hash_mail{$key}; | |
push @from, {'email' => $key, 'total' => $hash_mail{$key}}; | |
} | |
push @from, {'total_email' => $total}; | |
push @geral, {'domain' =>\@domain, 'email' => \@from}; | |
$json = encode_json \@geral; | |
print $json; | |
} | |
if (($DELETE) && ($EMAIL)){ &DeleteQueue($EMAIL); } | |
sub DeleteMailQueue($){ | |
my ($delEmail) = @_; | |
$delEmail =~ s/\@/\\@/g; | |
my ($date,$time,$error,$queueid,$size,$totalsize,$totalrequests); | |
my $delta = 0; | |
my $x = 0; | |
my $sender = ""; | |
my $recipient = ""; | |
my %Q; | |
foreach my $queue(@queue) { | |
chomp $queue; | |
if ($queue =~ /^Mail queue is empty/) { print "Nada na fila\n"; exit ; } | |
if ($queue =~ /^([0-9A-F]*)[ \*\!]*(\d+) *([a-zA-Z]{3} [a-zA-Z]{3} [ 0-9]{2} \d{2}:\d{2}:\d{2}) +([^ ]+)/ ) { | |
$queueid = $1; $size = $2; $date = $3; $sender = $4; | |
$date = str2time($date); $time = time; $delta = $time - $date; $error = ""; $recipient = ""; | |
} elsif ($queue =~ /^ *\((.+)\)/ ) { $error = $1; } elsif ($queue =~ /^ *(.+.+)/ ) { $recipient = $1; | |
} elsif ($queue =~ /^-- (\d+) Kbytes in (\d+) Requests./ ) { $totalsize = $1; $totalrequests = $2; } elsif ($queue =~ /^-Que.*/ ) { | |
# do nothing | |
} elsif (($delta > 1) and ("$recipient" ne "")) { | |
# print $queueid . " " . $delta . " " . $sender . " " . $recipient . "\n"; | |
if ( ($sender =~ m/$delEmail/) or ($recipient =~ m/$delEmail/) ){ $Q{$queueid} = 1; $x++; } | |
$delta = 0; $sender = ""; $recipient = ""; | |
} elsif (($delta > 0) and ("$recipient" ne "")) { $delta = 0; $sender = ""; $recipient = ""; } | |
} | |
if ( $x eq 0 ) { print "nothing to do\n" ; exit 0; } ; | |
open(POSTSUPER, "|$POSTSUPER -d -") || die "postsuper error \n"; | |
foreach (keys %Q) { print POSTSUPER "$_\n"; } | |
close(POSTSUPER); | |
## Flush na Fila | |
`$POSTQUEUE -f`; | |
} | |
sub help { | |
print "Para Lista a FILA em JSON \n"; | |
print "Usage $0 --json \n\n"; | |
print "Para Deletar email na FILA\n"; | |
print "Usage $0 --del --email=email\@domain.com \n\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment