ITeye博客

Web服务自动监控shell

gaozhonghui · 2012-12-20 14:02 · Unix系列 · web服务 · 运维实践 · 互联网 · Markdown

我们在线上跑的服务,不知道为什么最近有几回运行的进程莫名其妙的就没有了,就特意写了这个监控脚本,让其自动扫描服务,把访问不正常的服务,自动启动或重启服务,并且导出当时的线程使用情况,方便定位问题。

步骤: 1.修改web服务名称和端口 monitorTcp.sh

2.修改扫描时间 monitorServer.sh

3.启动守候进程 /usr/bin/nohup /bin/sh /home/Gzh/shell/monitorServer.sh 2>&1 > /dev/null &

monitorServer.sh


#!/bin/sh
###########################################################
#desc:后台守候检查服务是否正常
#author:gaozhonghui
#mail:toptreegzh@163.com
#date:20121210
###########################################################

while true
do
   /bin/sh /home/Gzh/shell/monitorTcp.sh > /dev/null 2>&1
   sleep 10
done

monitorTcp.sh


#!/bin/sh
#####################################################
#desc:扫描后台服务器的应用服务器,若不能正常访问则重启
#author:gaozhonghui
#mail:toptreegzh@163.com
#date:20121127
#######################################################

year=`date -d "today" +"%Y"`
monthday=`date -d "today" +"%m"`
date=$(date -d "today" +"%Y%m%d")

#被监控服务器、端口列表
#str = web服务文件夹:端口号
server_all_list=(
	'www.test2.com:9090'
	'www.test.com:8090'
)

#应用服务器基路径
serverBasePath="/web/webserver/jboss/"
#日志路径
logBasePath="/web/webserver/logs/$year/$monthday/"

#获得监控服务PID
function_getPID(){
   local PID=`ps -ef|grep $1|grep java |awk '{print $2}'`
   echo $PID
}

#dump 线程详细信息方便定位问题
function_jstackinfo(){
 PID=`function_getPID $1`
 if [ x$pid != x"" ] ;then
    if [ ! -d ${logbasepath} ];then
       mkdir -p ${logBasePath}  	
    fi

    jstack -l ${PID} >> ${logBasePath}"jstack_$1_${date}.log"	
 fi
}

#关闭应用服务
function_shutdown(){
  local shutdownSh=${serverBasePath}$1"/bin/shutdown.sh"
  if [ -f $shutdownsh ];then
        local PID=`function_getPID $1`

        if [ x$pid != x"" ] ;then
                sh $shutdownSh > /dev/null 2>&1
                sleep 2
        fi

        local PID2=`function_getPID $1`

        if [ x$pid2 != x"" ];then
                kill -9 $PID2
        fi
  fi	
}

#启动应用服务
function_startup(){
 local startupSh=${serverBasePath}$1"/bin/startup.sh"
 if [ -f $shutdownsh ];then
	sh $startupSh > /dev/null 2>&1
 fi
}

#重启应用服务
function_restart(){
  function_shutdown $1
  sleep 5
  function_startup $1
}

#扫描监控服务
for server in ${server_all_list[@]}; do
   server_ip=127.0.0.1
   server_name=$( echo ${server} | awk -F ':' '{print $1}')
   server_port=$( echo ${server} | awk -F ':' '{print $2}')
   
   #status:    0,http down    1,http ok    2,http down but ping ok 
   if nc -vv -z -w 2 $server_ip $server_port > /dev/null 2>&1;then
       status=1
   else
       if nc -vv -z -w 3 $server_ip $server_port > /dev/null 2>&1;then
           status=1
       else
           if ping -c 1 $server_ip > /dev/null 2>&1;then
               status=2
           else
               status=0
           fi
       fi
   fi
	
   if [ x$status != x"1" ];then
	PID=`function_getPID  ${server_name}`
	
	if [ x$pid != x ];then
		function_jstackinfo  ${server_name}
		function_restart  ${server_name}
	else
		function_startup ${server_name}
	fi	
   fi
	
done

评论(6)

  1. #6 楼 · dplqg · 2012-12-24 09:58

    挺好的呀,顶楼主一个,谢谢分享 :D

  2. #5 楼 · daixianmeng · 2012-12-20 17:41

    回去研究一下,谢谢!!

  3. #4 楼 · gaozhonghui · 2012-12-20 16:55

    mrcai314 writes:

    不知道有没有效果

    我们生产环境跑有段时间了,表现良好。 您可以动手局域网测试。

  4. #3 楼 · gaozhonghui · 2012-12-20 16:53

    txf_7337 writes:

    注释都不写,踩一个

    有时间把注解补全。

  5. #2 楼 · mrcai314 · 2012-12-20 16:36

    不知道有没有效果

  6. #1 楼 · txf_7337 · 2012-12-20 14:41

    注释都不写,踩一个

评论功能维护中,当前仅展示历史评论。