#!/bin/sh
#Write by LJ @ 2007/05/15 DaLian
########################################################
#This script can monitor qmail's log and queue,move mail
#from qmail's queue and recover them.
########################################################
PATH=/var/qmail/bin:/bin:/usr/bin:/usr/local/bin:/usr/local/sbin
export PATH
QMAILDUID=`id -u qmaild`;
NOFILESGID=`id -g qmaild`;
svclist="qmail-send qmail-smtpd";
path=/usr/local/src
Home_Dir="/var/qmail/filter";
mkdir -p $Home_Dir;
mkdir -p $Home_Dir/backup;
echo "Warning email" > "$Home_Dir/Warning.txt";
MailTo=`cat $Home_Dir/mail_list`;
KeyWords=`cat $Home_Dir/keywords`;
if [ -n "`cat "$Home_Dir/check_time"`" ]
then
last_check_time=`cat $Home_Dir/check_time`;
else
touch $Home_Dir/check_time;
last_check_time=`tail -n 1 /var/log/qmail/current | tai64nlocal | awk -F"." '{print $1}'`;
fi
Qmail_start()
{
echo "Starting qmail now...";
for svc in $svclist
do
if svok /service/$svc;
then
svc -u /service/$svc;
else
echo $svc service not running;
fi
done
if [ -d /var/lock/subsys ]
then
touch /var/lock/subsys/qmail;
fi
}
Qmail_stop()
{
echo "Stoping qmail now...";
for svc in $svclist
do
echo " $svc"
svc -d /service/$svc
done
if [ -f /var/lock/subsys/qmail ]
then
rm /var/lock/subsys/qmail
fi
}
Qmail_restart()
{
echo "Restarting qmail now...";
for svc in $svclist ; do
if [ "$svc" != "qmail-send" ]
then
echo "* Stopping $svc.";
svc -d /service/$svc
fi
done
echo "* Sending qmail-send SIGTERM and restarting."
svc -t /service/qmail-send
for svc in $svclist ; do
if [ "$svc" != "qmail-send" ]
then
echo "* Restarting $svc."
svc -u /service/$svc
fi
done
}
Qmail_stat()
{
for svc in $svclist
do
svstat /service/$svc
svstat /service/$svc/log
done
/var/qmail/bin/qmail-qstat;
}
Move_queue()
{
Date_Time=`date +%s`;
find /var/qmail/queue/bounce/ /var/qmail/queue/info/ /var/qmail/queue/intd/ /var/qmail/queue/mess/ -type f -newer "$Home_Dir/check_time" ! -newer "/var/log/qmail/current" > $Home_Dir/remove_time/remove_file.$Date_Time;
Qmail_stop;
for email_file in `cat $Home_Dir/remove_time/remove_file.$Date_Time`
do
mess_path=`echo "$email_file" | awk -F"/" '{print $1"/"$2"/"$3"/"$4"/"$5"/"$6}' | sed '/bounce/d' | sed '/info/d' | sed '/intd/d'`;
mess_file=`echo "$email_file" | sed '/bounce/d' | sed '/info/d' | sed '/intd/d' | awk -F"/" '{print $7}'`;
info_path=`echo "$email_file" | awk -F"/" '{print $1"/"$2"/"$3"/"$4"/"$5"/"$6}' | sed '/bounce/d' | sed '/mess/d' | sed '/intd/d'`;
info_file=`echo "$email_file" | sed '/bounce/d' | sed '/mess/d' | sed '/intd/d' | awk -F"/" '{print $7}'`;
bounce_path=`echo "$email_file" | grep "bounce" | awk -F"[0-9]" '{print $1}' | tail -n 1`;
bounce_file=`echo "$email_file" | grep "bounce" | awk -F"[0-9]" '{print $2}' | tail -n 1`;
intd_path=`echo "$email_file" | grep "intd" | awk -F"[0-9]" '{print $1}' | tail -n 1`;
intd_file=`echo "$email_file" | grep "intd" | awk -F"[0-9]" '{print $2}' | tail -n 1`;
mkdir -p $Home_Dir/backup/$Date_Time;
echo $mess_path;
if [ -n "$mess_path" ]
then
echo $mess_path;
if [ -n "$mess_file" ]
then
echo $mess_file;
mess_info="$mess_path/$mess_file";
else
mess_info="$mess_path/$info_file";
fi
else
mess_path=`echo $info_path | sed 's/info/mess/'`;
if [ -n "$mess_file" ]
then
echo $mess_file;
mess_info="$mess_path/$mess_file";
else
mess_info="$mess_path/$info_file";
fi
fi
if [ -n $info_path ]
then
echo $info_path;
if [ -n $info_file ]
then
echo $info_file;
info_info="$info_path/$info_file";
else
info_info="$info_path/$mess_file";
fi
else
info_path=`echo $mess_path | sed 's/mess/info/'`
if [ -n $info_file ]
then
info_info="$info_path/$info_file";
else
info_info="$info_path/$mess_file";
fi
fi
if [ -n $bounce_path ]
then
echo $bounce_path;
if [ -n $bounce_file ]
then
echo $bounce_file;
bounce_info="$bounce_path/$bounce_file";
fi
fi
if [ -n $intd_path ]
then
echo $intd_path;
if [ -n $intd_file ]
then
echo $intd_file;
intd_info="$intd_path/$intd_file";
fi
fi
if [ -f "$mess_info" ]
then
for mailaddress in `cat /var/qmail/filter/mail_list`
do
echo $mailaddress;
for keywords in `cat /var/qmail/filter/keywords`
do
echo $keywords;
echo $mess_info;
Result1=`cat $mess_info | grep "$keywords"`;
Result2=`cat $mess_info | grep "$mailaddress"`;
if [ -n "$Result1" ] && [ -n "Result2" ]
then
mkdir -p $Home_Dir/backup/$Date_Time/$mess_path;
mkdir -p $Home_Dir/backup/$Date_Time/$info_path;
mkdir -p $Home_Dir/backup/$Date_Time/$bounce;
mkdir -p $Home_Dir/backup/$Date_Time/$intd;
if [ -f "$mess_info" ]
then
mv $mess_info $Home_Dir/backup/$Date_Time/$mess_path > /dev/null 2>&1;
fi
if [ -f "$info_info" ]
then
mv $info_info $Home_Dir/backup/$Date_Time/$info_path > /dev/null 2>&1;
fi
if [ -f "$bounce_info" ]
then
mv $bounce_info $Home_Dir/backup/$Date_Time/$bounce > /dev/null 2>&1;
fi
if [ -f "$intd_info" ]
then
mv $intd_info $Home_Dir/backup/$Date_Time/$bounce > /dev/null 2>&1;
fi
fi
done
done
fi
done
Qmail_start;
echo "Queue has been Move to $Home_Dir/backup/";
}
Recover_queue()
{
mkdir -p $Home_Dir/recover;
Recover_path="$Home_Dir/backup"
last_backup=`ls -l $Recover_path | sort |sed '/total/d' | tail -n 1 | awk '{print $9}'`;
echo $last_backup;
Now_time=`date +%s`;
find $Recover_path/$last_backup -name "[0-9]*" -type f > $Home_Dir/recover/$Now_time.tmp;
local=`find $Recover_path/$last_backup -name "[0-9]*" -type f`;
for cause in `cat $Home_Dir/recover/$Now_time.tmp`
do
daa=`echo $cause | awk -F"var" '{print $3}'`;
echo $daa;
daa="/var$daa";
printf "%s%s%s\n" cp" " $cause" " $daa >> $Home_Dir/recover/recover.$Now_time;
done
cd $Home_Dir/recover/
chmod 755 $Home_Dir/recover/recover.$Now_time;
Qmail_stop;
sh $Home_Dir/recover/recover.$Now_time;
Qmail_start;
echo "Queue has been recovery.";
}
Qmail_log()
{
ipadd="10.15.2.131"
Check_time=`date +%F" "%T`;
hostname=`hostname`;
max_num="30";
echo "
IPADD :$ipadd
TIME :$Check_time
HOST :$hostname
STAT :Warning
INFO :JHFSYS/LiuJun" > $Home_Dir/Warning.txt;
echo "" >> $Home_Dir/Warning.txt;
for email in `cat "$Home_Dir/mail_list" `
do
echo $email;
email_num=`cat /var/log/qmail/current | tai64nlocal | awk '/'"$last_check_time"'/,/$0/' | grep $email | wc -l`;
queue_num=`/var/qmail/bin/qmail-qread | grep $email | wc -l`;
email_num=`expr $email_num + $queue_num`;
mkdir -p $Home_Dir/num/$email;
Check_time2=`date +%F`;
echo "$email_num" >> $Home_Dir/num/$email/$Check_time2;
email_num=0;
for num in `cat $Home_Dir/num/$email/$Check_time2`
do
email_num=`expr $num + $email_num`;
done
if [ $email_num -gt $max_num ]
then
echo "EmaiAdd :$email" >> $Home_Dir/Warning.txt;
echo "Number :$email_num" >> $Home_Dir/Warning.txt;
echo "SHELL :$path/Mqmail.sh" >> $Home_Dir/Warning.txt;
echo "" >> $Home_Dir/Warning.txt;
echo "=============================================" >> $Home_Dir/Warning.txt;
echo "Action:" >> $Home_Dir/Warning.txt;
echo "1:Login to $ipadd." >> $Home_Dir/Warning.txt;
echo "2:Mqmail.sh remove/clear." >> $Home_Dir/Warning.txt;
echo "" >> $Home_Dir/Warning.txt;
echo "Note:" >> $Home_Dir/Warning.txt;
echo "remove:email can recover." >> $Home_Dir/Warning.txt;
echo "clear:delete mail in queue." >> $Home_Dir/Warning.txt;
echo "=============================================" >> $Home_Dir/Warning.txt;
echo "" >> $Home_Dir/Warning.txt;
mail -s "$email_num is more than $max_num on $ipadd." liujun@srv.bestwiz.cn < $Home_Dir/Warning.txt;
echo > $Home_Dir/num/$email/$Check_time2;
fi
done
}
Qmail_clear()
{
echo "Are you sure remove:(y/n)";
read yn;
if [ $yn = "y" ]
then
Qmail_stop;
find /var/qmail/queue/bounce/ /var/qmail/queue/info/ /var/qmail/queue/intd/ /var/qmail/queue/mess/ -type f | xargs rm -rf;
Qmail_start;
else
echo "No action.";
fi
}
case "$1" in
monitor)
Qmail_log;
;;
remove)
Move_queue;
;;
recover)
Recover_queue;
;;
clear)
Qmail_clear;
#Qmail_remove > /dev/null 2>&1;
;;
stop)
Qmail_stop;
;;
start)
Qmail_start;
;;
stat)
Qmail_stat;
;;
restart)
Qmail_restart;
;;
*)
echo
echo "Usage: Mqmail.sh [ monitor | remove | recover | clear | stop | start | stat | restart ]";
;;
esac
last_check_time=`tail -n 1 /var/log/qmail/current | tai64nlocal | awk -F"." '{print $1}'`;
echo "$last_check_time" > $Home_Dir/check_time;
exit 0;