postfix/get_number_of_deferred_mailqueue.sh

104 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env bash
declare -i count_warn
declare -i count_warn_default=100
notification_addresses="ckubu-adm@oopen.de"
host_name=`hostname -f`
from_address="postfix@$host_name"
content_type='Content-Type: text/plain;\n charset="utf-8"'
postfix_queue_dir=/var/spool/postfix
if [[ $1 =~ ^-?[0-9]+$ ]];then
count_warn=$1
else
count_warn=$count_warn_default
fi
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
## - Get number of "deferred" mails in postfix queue
## -
## - Notice: Don't use mailq or postqueue command to examin
## - the number of mails. Both will open every mail in
## - the queue and this may take a (very) long time if
## - many mails are there (and it will cost much resources).
## -
## - I used a little perl script but its also posible to get
## - the numbers of mail with:
## - find ${postfix_queue_dir}/deferred -type f | wc -l
## -
declare -i count=$(
perl -e '
use strict;
use warnings;
use Symbol;
sub count {
my ($dir) = @_;
my $dh = gensym();
my $c = 0;
opendir($dh, $dir) or die "$0: opendir: $dir: $!\n";
while (my $f = readdir($dh)) {
if ($f =~ m{^[A-F0-9]{5,}$}) {
++$c;
} elsif ($f =~ m{^[A-F0-9]$}) {
$c += count("$dir/$f");
}
}
closedir($dh) or die "closedir: $dir: $!\n";
return $c;
}
my $qdir = shift(@ARGV) or die "Usage: $0 queue-directory\n";
chdir($qdir) or die "$0: chdir: $qdir: $!\n";
printf count("deferred");
' $postfix_queue_dir
)
if [[ $count -gt $count_warn ]]; then
subject="[ WARN ] postfix deferred queue exceeds $count_warn Messages"
msg="\n*${host_name}* - `date +\"%d.%m.%Y %H:%M h\"`\n\n[ Warning ]: $count messages in postfix deferred queue\n"
msg+="\n\nRecipient domain and time (age in minutes):\n`qshape deferred`\n"
msg+="\n\nSender domain and time (age in minutes):\n`qshape -s deferred`\n"
msg+="\n\n\n** Output from script \"`basename $0`\" - Don't reply to this e-mail **"
for email_to in $notification_addresses ; do
echo -e "To:${email_to}\n${content_type}\nSubject:$subject\n\n${msg}" | \
/usr/sbin/sendmail -F $from_address $email_to
done
fi
if $terminal ; then
echo ""
#echo "*${host_name}* - $(date +"%d.%m.%Y %H:%M h")"
echo ""
echo "[ Info ]: $count messages in postfix deferred queue"
echo ""
echo ""
echo "Recipient domain and time (age in minutes):"
echo "$(qshape deferred)"
echo ""
echo ""
echo "Sender domain and time (age in minutes):"
echo "$(qshape -s deferred)"
echo ""
fi
exit 0