#!/bin/bash
#环境变量
export  JAVA_HOME=/opt/apps/jdk1.6.0_41
export  PATH=$PATH:/opt/apps/jdk1.6.0_41/bin
export  CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/jre/lib/rt.jar
#需要监控的网页地址
URL="http://127.0.0.1:85"
#tomcat程序实际路径
TOMCATlujin="/opt/apps/apache-tomcat-6.0.37"
#tomcat进程ID号
TOMCATID=$(ps -ef  |grep $TOMCATlujin |grep -v "grep" |awk '{print $2}')
#访问成功次数,B初始化为0,经过5次访问,如果B=0,即一次也未成功访问,则报警或重启应用
B=0
for ((i=1;i<=5;i++));do
           http_server_status=$(curl --connect-timeout 20 --max-time 30 --head --silent $URL |grep '200' |awk '{print $2}')
           if ` http_server_status -eq 200 `;then
                ((B++))
           fi
done
echo "The number of successful $B,time:$(date +%Y-%m-%d\ %H:%M:%S)">>/opt/apps/tomcat.log
if [[ $B -eq 0 ]];then
       echo "app is cannot access,time:$(date +%Y-%m-%d\ %H:%M:%S) ">>/opt/apps/tomcat.log
       echo "app is cannot access ,time:$(date +%Y-%m-%d\ %H:%M:%S) ,please check(Please do not reply)" |mail -s "app service status" ***@qq.com
       kill -9 $TOMCATID
       cd $TOMCATlujin/bin
      /bin/bash startup.sh
fi