How to trace message in postfix [POSTTRACE]

Step: 01
mkdir -p /var/adm/bin

Step: 02
vi /var/adm/bin/posttrace.sh

#!/bin/bash

export LC_ALL=C
while getopts ":f:t:l:" opt; do
case $opt in
f)
from="${OPTARG}"
;;
t)
to="${OPTARG}"
;;
l)
l="${OPTARG}"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument" >&2
exit 1
;;
esac
done
usage() {
cat << EOF
posttrace [-l </path/to/logfile>] [ -f <sender@domain.com> ] [ -t <recipient@domain.com> ]
EOF
}
if [ -z "${from}" ] && [ -z "${to}" ]
then
usage
exit 1
fi
configure() {
this_script=$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")
p=$(echo "${this_script}" | awk -F'.' '{print $1}')
d=/tmp
if [ -z "${l}" ]
then
l=/var/log/maillog
fi
blk=$(echo "scale=0;`stat --printf=\"%s\" ${l}`/`grep -c ^processor /proc/cpuinfo`/1024/1024*1+1"|bc -l)
}
spacer() {
echo ""
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo ""
}
highlight() {
grep --color -E "$1|$"
}
trim() {
awk -v len=`stty size </dev/tty | cut -d' ' -f2` '{ if (length($0) > len) print substr($0, 1, len-3) "..."; else print; }'
}
cleanup() {
/bin/rm -f /tmp/*\.chr 2>/dev/null
}
mailtrace() {
spacer
i=0
echo "${i}" > "${d}_${p}.counter"
if [ ! -z "${to}" ] && [ ! -z "${from}" ]
then
parallel --will-cite --gnu --pipe --block ${blk}M grep -i "from=\<.*${from}.*\>" < "${l}" | grep -Eo "([A-Z]|[0-9]){10}.*queue active" | awk -F':' '{print $1}' | while read a
do
parallel --will-cite --gnu --pipe --block ${blk}M grep "${a}" < "${l}" | grep -i -E -m1 "to=<${to}>" | grep -Eo "([A-Z]|[0-9]){10}" | xargs | grep . | while read a b
do
r=$(echo "`expr ${RANDOM}${RANDOM} % 10000000`+1"|bc -l)
parallel --will-cite --gnu --pipe --block ${blk}M grep -E "${a}\|${b}" < "${l}" > "${d}_${p}_${r}.txt"
if [ -f "${d}_${p}_${r}.txt" ]
then
cat "${d}_${p}_${r}.txt" | trim | highlight "${from}|${to}"
/bin/rm -f "${d}_${p}_${r}.txt"
spacer
(( i = `cat "${d}_${p}.counter"` + 1 ))
echo "${i}" > "${d}_${p}.counter"
fi
done
done
rejected=$(parallel --will-cite --gnu --pipe --block 2M grep -i "from=\<${from}\>" < "${l}" | grep -i "${to}" | grep -c reject)
echo "Emails from ${from} to ${to}: `cat ${d}_${p}.counter` accepted / ${rejected} rejected"
elif [ ! -z "${to}" ] && [ -z "${from}" ]
then
parallel --will-cite --gnu --pipe --block ${blk}M grep -i "to=\<.*${to}.*\>" < "${l}" | grep -Eo "([A-Z]|[0-9]){10}.*queued as ([A-Z]|[0-9]){10}" | \
awk '{print $1" "$NF}' | sed 's/://g' | while read a b
do
r=$(echo "`expr ${RANDOM}${RANDOM} % 10000000`+1"|bc -l)
parallel --will-cite --gnu --pipe --block ${blk}M grep -E "${a}\|${b}" < "${l}" > "${d}_${p}_${r}.txt"
if [ -f "${d}_${p}_${r}.txt" ]
then
cat "${d}_${p}_${r}.txt" | trim | highlight "${to}"
/bin/rm -f "${d}_${p}_${r}.txt"
spacer
(( i = `cat "${d}_${p}.counter"` + 1 ))
echo "${i}" > "${d}_${p}.counter"
fi
done
rejected=$(parallel --will-cite --gnu --pipe --block 2M grep -i "from=\<${from}\>" < "${l}" | grep -c reject)
echo "Emails to ${to}: `cat ${d}_${p}.counter` accepted / ${rejected} rejected"
elif [ -z "${to}" ] && [ ! -z "${from}" ]
then
parallel --will-cite --gnu --pipe --block ${blk}M grep -i "from=\<.*${from}.*\>" < "${l}" | grep -Eo "([A-Z]|[0-9]){10}.*queue active" | awk -F':' '{print $1}' | while read a
do
parallel --will-cite --gnu --pipe --block ${blk}M grep "${a}" < "${l}" | grep -i -E -m1 "to=<.*>" | grep -Eo "([A-Z]|[0-9]){10}" | xargs | grep . | while read a b
do
r=$(echo "`expr ${RANDOM}${RANDOM} % 10000000`+1"|bc -l)
parallel --will-cite --gnu --pipe --block ${blk}M grep -E "${a}\|${b}" < "${l}" > "${d}_${p}_${r}.txt"
if [ -f "${d}_${p}_${r}.txt" ]
then
cat "${d}_${p}_${r}.txt" | trim | highlight "${from}"
/bin/rm -f "${d}_${p}_${r}.txt"
spacer
(( i = `cat "${d}_${p}.counter"` + 1 ))
echo "${i}" > "${d}_${p}.counter"
fi
done
done
rejected=$(parallel --will-cite --gnu --pipe --block 2M grep -i "to=\<${to}\>" < "${l}" | grep -c reject)
echo "Emails from ${from}: `cat ${d}_${p}.counter` accepted / ${rejected} rejected"
fi
/bin/rm -f "${d}_${p}_${r}.counter"
}
# RUNTIME
configure
mailtrace
cleanup

Step: 03
chmod 755 /var/adm/bin/posttrace.sh

Step: 04
ln -s /var/adm/bin/posttrace.sh /usr/bin/posttrace

Step: 05
Execute the command
posttrace [-l </path/to/logfile>] [ -f <sender@domain.com> ] [ -t <recipient@domain.com> ]

i.e
posttrace -l /var/log/mail.log -f sender@domain.com
OR
posttrace -l /var/log/mail.log -t receiver@domain.com


Ref:

Comments

Popular posts from this blog

Cambium cnPilot E400/E410/E500 Configuration Tutorial

Disabling Zimbra's AntiSpam, Amavis and AntiVirus filtering

Error "Unable to retrive Zimbra GPG key for package validation"