#!/bin/sh
#自動監控tomcat腳本而且執行重啓操做
#獲取tomcat_ID
TomcatID=`ps -ef|grep tomcat|grep -v "grep"|grep -v "monitor_tomcat"|awk '{print $2}'`
Tomcatbag=`ls /usr/local/apache-tomcat-7.0.42/webapps -t |head -1`
#tomcat啓動程序
StartTomcat=/home/deployer/scripts/monitor_restart.sh
TomcatCache=/usr/local/apache-tomcat-7.0.42/work/
# 定義要監控的頁面地址
#積分 WebUrl=http://127.0.0.1:8080/integral/health/check
WebUrl=http://127.0.0.1:8080/$Tomcatbag/healthCheck/verify
# 日誌輸出
GetPageInfo=/home/deployer/scripts/monitor_logs/error_$(date +'%F').log
TomcatMonitorLog=/home/deployer/scripts/monitor_logs/TomcatMonitor_$(date +'%F').log
Monitor()
{
echo "[info]開始監控tomcat...[$(date +'%F %H:%M:%S')]"
if [[ $TomcatID ]];then
echo "[info]當前tomcat進程ID爲:$TomcatID,檢測頁面..."
TomcatServiceCode=$(curl -s -o $GetPageInfo -m 10 --connect-timeout 30 $WebUrl -w %{http_code})
echo $TomcatServiceCode
if [ $TomcatServiceCode -eq 200 ];then
echo "[info]頁面返回碼爲$TomcatServiceCode,tomcat啓動成功,測試頁面正常......"
else
echo "[error]tomcat頁面出錯,請注意......狀態碼爲$TomcatServiceCode,錯誤日誌已輸出到$GetPageInfo"
echo "[error]頁面訪問出錯,開始重啓tomcat"
kill -9 $TomcatID
sleep 3
rm -rf $TomcatCache
$StartTomcat
fi
else
echo "[error]tomcat進程不存在!tomcat開始自動重啓..."
echo "[info]$StartTomcat,請稍候......"
rm -rf $TomcatCache
$StartTomcat
fi
echo "------------------------------"
}
Monitor>>$TomcatMonitorLog
cd /home/deployer/scripts/monitor_logs
find . -maxdepth 2 -mtime +5 -name "*.log" -exec rm -rf {} \;web