Curl 도구 의 묘 용: 셸 에서 curl 로 업무 모니터링 과 고장 자동 처리

JVM 메모리 가 넘 치 거나 서비스 프로 세 스 가 경직 되 어 접근 할 수 없 는 경우 가 종종 있 습 니 다. 이 럴 때 는 서비스 프로 세 스 를 다시 시작 해 야 합 니 다.zabbix 에서 웹 에 대한 모니터링 이 가능 하고 원 격 명령 을 실행 할 수 있 지만 안전 을 고려 하여 zabbix 의 에이전트 에서 원 격 명령 을 실행 할 수 있 는 권한 을 열지 않 습 니 다.이 컴퓨터 에 curl 도 구 를 설치 하여 웹 에 대한 감 시 를 실현 할 수 있 습 니 다. 스 크 립 트 를 보조 하면 고장 과 복구 경고 전송 과 프로 세 스 를 다시 시작 할 수 있 습 니 다.
#!/usr/bin/bash
##############################################
#NAME: monweb.sh 
#DESC:mon the web status,if wrong restart container.
#Note: Curl must be installed,curl         。
#History:
#   v1.0 2016-1-20 thinker yang
##############################################
#    ,    ,          
DEBUG=2
#
#define config
#
##local conf##
#             profile  ,                 
. /.profile
#      
TIME=`date`
#        curl
URL=wwww.baidu.com/123.html
#       
AlertHead=ERROR-TOMCAT
##server conf##
##       ##
#            
STARTSERVERCMD=/startapp.sh
#              
STOPSERVERCMD=/stopserver.sh
#      grep       pid,              ,             
PSKEY1=java
PSKEY2=java
PSKEY3=java
#sleep  / :                     
WAITRESTARTTIME=10
#
#sleep  / :        , curl    web         
WAITCHECKHEALTHTIME=60
##java core file conf##
#       java dump      
MEMOUTFILEPATH=/tmp
# grep                    (      )
MEMOUTFILEKEY1=heapdump
MEMOUTFILEKEY2=phd
# (Y) (N)            dump core  ,     
RMJAVACOREFILEFLAG=Y
##log conf##
#                
LOGDIR=/tmp/monlog
LOG=$LOGDIR/monweb.sh.log
##alert sms config##

#########################
#
#function:sendsms,                   ,     echosm "    ",      。
#        ,     echo $1    
#para:@message
#
#########################
echosm()
{
    echo $1
}
#########################
#
#function:getHttpStatus
#
#########################
getHttpStatus()
{
    HTTPCODE=`curl -o /dev/null -s -w %{http_code} $URL`
    return $HTTPCODE
}
#########################
#
#function:getHttpStatus
#return: container's process number.
#
#########################
getServerProcess()
{
    test $DEBUG -ge 1 && echo "DEBUG1> IN function getServerProcess echo PSKEY1: $PSKEY1"
    test $DEBUG -ge 1 && echo "DEBUG1> IN function getServerProcess echo PSKEY2: $PSKEY2"
    test $DEBUG -ge 1 && echo "DEBUG1> IN function getServerProcess echo PSKEY3:$PSKEY3"
    serverProcessNum=`ps -ef | grep -v grep | grep $PSKEY1 | grep $PSKEY2 | grep $PSKEY3 | wc -l`
    return $serverProcessNum
    test $DEBUG -ge 1 && echo "DEBUG1> function getServerProcess return serverProcessNum $serverProcessNum"
}
#########################
#
#function:killServer,Frist use normal cmd stop server,if fail then kill.
#
#########################
killServer()
{
    test $DEBUG -ge 1 && echo "DEBUG1> Func killServer Begin!" 
    $STOPSERVERCMD
    sleep $WAITRESTARTTIME
    getServerProcess
    #If staill has apache process,then killed
    if [ $serverProcessNum -gt 0 ];then
        ps -ef | grep -v grep | grep $PSKEY1 | grep $PSKEY2 | grep $PSKEY3 | awk '{print $2}'|xargs kill -9
    fi
}
#########################
#
#function:startServer,normal start container server.
#
#########################
startServer()
{
    $STARTSERVERCMD
}
#########################
#
#function:restartServer
#
#########################
restartServer()
{
    killServer
    sleep $WAITRESTARTTIME
    startServer
    echo "$AlertHead has been restarted! at $TIME on `hostname`" | tee -a  $LOG
    echosm "$AlertHead has been restarted! at $TIME on `hostname`" | tee -a  $LOG
}
#########################
#
#function:getOutMemFileNum,when Java memory out ,it will be dump some core files.
#return: dump core files number
#
#########################
getOutMemFileNum()
{
    if [ ! -d $MEMOUTFILEPATH ];then
        echo "$MEMOUTFILEPATH not exist,please check again!"
    else
        if [ ${RMJAVACOREFILEFLAG} = "Y" ];then
            heapdumpNum=`ls $MEMOUTFILEPATH | grep $MEMOUTFILEKEY1 | grep $MEMOUTFILEKEY2 | wc -l`
            return $heapdumpNum
        else
            heapdumpNum=0
            return $heapdumpNum
        fi
    fi
}
#########################
#
#function:delMemOutFile,Those Java memory dump core files very too larger,should be delete.
#
#########################
delMemOutFile()
{
    cd $MEMOUTFILEPATH
    ls $MEMOUTFILEPATH | grep $MEMOUTFILEKEY1 | grep $MEMOUTFILEKEY2 | xargs /usr/bin/rm -f
}
#########################
#
#function:main
#
#########################
main()
{
    #test ! -d $LOGDIR && mkdir -p $LOGDIR
    if [ ! -d $LOGDIR ];then
        mkdir -p $LOGDIR
    fi
    getOutMemFileNum
    getHttpStatus
    if [ "$HTTPCODE"x != "200"x ] || [ $heapdumpNum -ge 1 ];then
            echo "$AlertHead httpcode is $HTTPCODE ! at $TIME on `hostname` " | tee -a  $LOG
            #If exist apache process but httpcode is not 200 it will be restart,if there is no apache process then no work on it.
            getServerProcess
            test $DEBUG -ge 1 && echo "Frist run getServerProcess in func main return serverProcessNum: $serverProcessNum "
            if [ $serverProcessNum -ge 1 ];then
                echosm "$AlertHead httpcode is $HTTPCODE and will be restarted ! at $TIME on `hostname` " | tee -a  $LOG
            fi
            test $DEBUG -ge 2 && test $serverProcessNum -ge 1 && echo "$AlertHead httpcode is $HTTPCODE and will be restarted ! at $TIME on `hostname` " | tee -a  $LOG
            test $DEBUG -ge 1 && echo "Second run getServerProcess in func main return serverProcessNum: ${serverProcessNum} ,before restart!"
            getServerProcess
            if [ $serverProcessNum -ge 1 ];then 
                restartServer
            fi
            test $DEBUG -ge 2 && echo "DEBUG2> After restartServer the serverProcessNum : ${serverProcessNum} "
            sleep $WAITCHECKHEALTHTIME
            getHttpStatus
            echo "$AlertHead status is $HTTPCODE, at $TIME on `hostname` ." | tee -a  $LOG
            if [ "$HTTPCODE" = "200" ];then
                echosm "$AlertHead has OK now!at $TIME on `hostname` " | tee -a  $LOG
            else
                echosm "$AlertHead restart failed or after restart the web staill wrong,please check !at $TIME on `hostname` " | tee -a  $LOG
            fi
    else
            echo "$AlertHead status is $HTTPCODE, at $TIME on `hostname` ." | tee -a $LOG
    fi
    if [ ${RMJAVACOREFILEFLAG} = "Y" ];then
        delMemOutFile
        if [ $? -eq 0 ];then
            echo "JAVA Core Files in $MEMOUTFILEPATH has been rm, at $TIME ON `hostname` . " | tee -a  $LOG
        fi
    else
        echo "JAVA Core Files in $MEMOUTFILEPATH staill exist,Please check you free space on filesystem at $TIME ON `hostname` . " | tee -a  $LOG
    fi
}
#
#MAIN EXE
#
main

좋은 웹페이지 즐겨찾기