#!/bin/bash
#队列目录
QUEUE_DIR_NAES="/incoming/
/active/
/bounce/
/defer/
/deferred/
/corrupt/
/hold/
/trace/"
ADMIN="it@zze.com
op@zzme.com"
#最大警戒值
MAXNUM=800
#日志
LOG="/var/log/monitor_queue.log"
#主机IP
LOCAL_IP=$(ifconfig |grep "inet addr"| head -n 1 | cut -f 2 -d ":"|cut -f 1 -d " ")
echo_red()
{
echo -e "\033[1;31;40m$*\033[0m"
}
echo_yellow()
{
echo -e "\033[1;33;40m$*\033[0m"
}
send_mail()
{
subject="Mail Queue monitor Warning"
content="[$(date +"%Y-%m-%d %H:%M:%S")] Warning: $LOCAL_IP, The message queue number has more than the warning value quantity, ($1) ===> ($2)."
mailto=$ADMIN
for mail_account in "$ADMIN"
do
echo "$content" | mail -s "$subject" $mail_account
done
}
echo "############### start mon queue [$(date +"%Y-%m-%d %H:%M:%S")] ############### " >> $LOG
while :
do
for dir_name in $QUEUE_DIR_NAES
do
COUNT=$(find /var/spool/postfix/ -type f | grep "$dir_name" | wc -l)
if [ $COUNT -gt $MAXNUM ]
then
export LANG="zh_CN"
dir2queue=${dir_name##/}
queue_name=${dir2queue%%/}
echo_yellow "[$(date +"%Y-%m-%d %H:%M:%S")] Queue number has exceeded the set warning value:">> $LOG
echo_red "$queue_name queue num $COUNT. " >> $LOG
echo "" >> $LOG
send_mail $queue_name $COUNT
fi
done
sleep 60
echo "############### poll mon queue [$(date +"%Y-%m-%d %H:%M:%S")] ############### " >> $LOG
done