tomcat健康检查监控脚本

#!/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 {} \;

posted on 2017-05-15 15:15  franjia  阅读(585)  评论(0编辑  收藏  举报