postfix/get_number_of_deferred_mailqueue.sh

156 lines
3.5 KiB
Bash
Executable File

#!/usr/bin/env bash
script_name="$(basename $(realpath $0))"
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/${script_name%%.*}.conf"
# ----------
# - Some variables
# ----------
host_name=`hostname -f`
from_address="postfix@$host_name"
content_type='Content-Type: text/plain;\n charset="utf-8"'
default_notification_addresses="argus@oopen.de"
postfix_queue_dir=/var/spool/postfix
declare -i count_warn
declare -i default_count_warn=80
# ----------
# Base Function(s)
# ----------
info (){
if $terminal ; then
echo ""
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
fi
}
warn (){
if $terminal ; then
echo ""
echo -e " [ \033[33m\033[1mWarn\033[m ] $*"
echo ""
fi
}
# ----------
# - Some checks ..
# ----------
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
# ----------
# Read Configurations from $conf_file
# ----------
# - Give your default values here
# -
if [[ -f "$conf_file" ]]; then
source "$conf_file"
else
warn "No configuration file '$conf_file' present.\n
Loading default values.."
fi
# - Commandline parameter overwrites those given at configuration file
# -
if [[ $1 =~ ^-?[0-9]+$ ]];then
count_warn=$1
fi
[[ -n "$notification_addresses" ]] || notification_addresses="$default_notification_addresses"
[[ -n "$count_warn" ]] || count_warn=$default_count_warn
## - 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
info "$count messages in postfix deferred queue"
if $terminal ; then
echo ""
echo -e " \033[1mRecipient domain and time (age in minutes)\033[m:"
echo "$(qshape deferred)"
echo ""
echo ""
echo -e " \033[1mSender domain and time (age in minutes)\033[m:"
echo "$(qshape -s deferred)"
echo ""
fi
exit 0