Curl 도구 의 묘 용: 셸 에서 curl 로 업무 모니터링 과 고장 자동 처리
17941 단어 Linux/UNIX 운영 실전
#!/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