#!/bin/bash
##1.当tomcat没在运行时执行重启并前三次重启告警
##2.三次告警后会继续执行重启tomcat,但不在告警
##3.每两个小时候后会重复 1 2 动作
export.utf8
IP=`/sbin/ip add|grep "inet" |sed -n '3p'|awk -F "/" '{print $1}'|awk '{print $2}'`
Host=`hostnam`
Tag="tomcat is down"
Starting="starting tomcat ..."
Starting_pid="tomcat start success...PID is"
Start_failed="tomcat start failed..."
echo `date +%s -d +2hour` >/tmp/unix_date
Tomcat_Path=/usr/local/tomcat-yitihua/bin
pid=`ps aux |grep $Tomcat_Path|grep -v grep |awk '{print $2}'`
id=`ps aux |grep $Tomcat_Path|grep -v grep|wc -l`
Id_file=/tmp/Id_wc.log
LogFile=/var/log/yitihua.log
TmpLog=/tmp/yitihua.log
MailTo=xxx@qq.com
function Mail(){
echo "------------ `date +'%Y-%m-%d %H:%M:%S'` start ------------" >> $LogFile
echo "$IP $Host $Tag" >> $LogFile
echo $Starting >> $LogFile
./startup.sh
sleep 5
pid=`ps aux |grep $Tomcat_Path|grep -v grep |awk '{print $2}'`
id=`ps aux |grep $Tomcat_Path|grep -v grep|wc -l`
echo $pid >> $Id_file
Id_wc=`cat $Id_file |wc -l`
if [ $id -ne 0 ];then
echo "$Starting_pid $pid" >> $LogFile
echo "============ `date +'%Y-%m-%d %H:%M:%S'` end ==============" >> $LogFile
sed ':a;N;$!ba;s/.*\n\(.*start -.*\)=/\1/' $LogFile > $TmpLog
cat $TmpLog | mail -s "$IP $Host $Tag 第$Id_wc告警" $MailTo
echo -e "\n" >> $LogFile
else
echo "$Start_failed" >> $LogFile
echo "============ `date +'%Y-%m-%d %H:%M:%S'` end ==============" >> $LogFile
sed ':a;N;$!ba;s/.*\n\(.*start -.*\)=/\1/' $LogFile > $TmpLog
cat $TmpLog | mail -s "$IP $Host $Tag 第$Id_wc告警" $MailTo
echo -e "\n" >> $LogFile
fi
}
function No_mail(){
echo "------------ `date +'%Y-%m-%d %H:%M:%S'` start ------------" >> $LogFile
echo "$IP $Host $Tag" >> $LogFile
echo "starting tomcat ..." >> $LogFile
./startup.sh
sleep 5
pid=`ps aux |grep $Tomcat_Path|grep -v grep |awk '{print $2}'`
id=`ps aux |grep $Tomcat_Path|grep -v grep|wc -l`
echo $pid >> $Id_file
if [ $id -ne 0 ];then
echo "$Starting_pid $pid" >> $LogFile
echo "============ `date +'%Y-%m-%d %H:%M:%S'` end ==============" >> $LogFile
echo -e "\n" >> $LogFile
else
echo "$Start_failed" >> $LogFile
echo "============ `date +'%Y-%m-%d %H:%M:%S'` end ==============" >> $LogFile
echo -e "\n" >> $LogFile
fi
}
main(){
while true;
do
sleep 1
cd $Tomcat_Path
id=`ps aux |grep $Tomcat_Path|grep -v grep|wc -l`
Id_wc=`cat $Id_file |wc -l`
if [ $id -eq 0 ];then
if [ $Id_wc -lt 3 ];then
elif [ $Id_wc -ge 3 ] ;then
DATE1=`cat /tmp/unix_date`
DATE2=`date +%s`
if [ $DATE2 -lt $DATE1 ];then
No_mail
elif [ $DATE2 -ge $DATE1 ] ;then
>$Id_file
echo `date +%s -d +2hour`>/tmp/unix_date
fi
fi
fi
done
}
main
转载于:https://blog.51cto.com/increase/2087158