#!/bin/bash
# by liuxiangtao
#定义入参:服务器ip,工程代号
reqIP=$1
ProjCode=$2
#工程部署ip,同服务器同工程多IP
reqPort=""
reqPortOther=""
reqFile='test.jsp'
fSendAdress='test@qq.com'
tAccept='test@qq.com'
sSmtp='smtp.qq.com'
subject='应用监控报警通知'
xuSendaccount='test@qq.com'
xpPasswd='password'
mContent=''
#cron有自己独立的环境变量,如果不引入系统变量,会导致很多功能失效,也会有编码集问题,
#这是cron比较坑爹的地方,博主被这块给坑死了
source ~/.bash_profile
source /etc/profile
#端口工程映射关系
if [ $ProjCode == '001' ]; then
reqPort='8081'
urlPath='abc'
ProjName='def'
elif [ $ProjCode == '002' ]; then
reqPort='8082'
urlPath='hig'
ProjName='lmn'
fi
#日志文件及路径定义
logDir=/home/log/monitor/$ProjName-monitor.log
#入参校验
if [ ! $reqIP ]; then
echo "$(date):reqIP is null ===> " >> $logDir
exit 0
fi
if [ ! $ProjCode ]; then
echo "$(date):ProjCode is null ===> " >> $logDir
exit 0
fi
#特殊工程转换,经代通的工程名字与应用名字不一致,需要做转换处理jdt→wjjdt
if [ $urlPath == 'jdt' ]; then
urlPath="wj"$urlPath
fi
#目标url
reqUrl="http://"$reqIP":"$reqPort"/"$urlPath"/"$reqFile
dealShell=$ProjName".sh"
if [ $urlPath == 'email' ]; then
reqUrl2="http://"$reqIP":"$reqPortOther"/"$urlPath"/"$reqFile
dealShell=email-18080.sh
dealShell2=email-18081.sh
fi
echo -e "
$(date):============$reqIP:$reqPort $ProjName start ============ " >> $logDir
#请求目标url
justWeb=`curl -I -m 10 -o /dev/null -s -w %{http_code} $reqUrl `
echo "$(date):request url ===> " $reqUrl >> $logDir
echo "$(date):request code ===> " $justWeb >> $logDir
#若请求目标url返回异常状态码则重启服务
if [ ${justWeb}x != "200"x ] && [ ${justWeb}x != "302"x ]; then
echo "$(date):$reqIP:$reqPort be executed shell ===>" $dealShell >> $logDir
#服务重启脚本
$dealShell
#服务重启状态是否成功
if [ $? == 0 ]; then
echo "$(date):restart status ===> success" >> $logDir
mContent="老板,$reqIP:$reqPort 服务器上的$ProjName 挂了!已自动处理,可通过访问如下地址验证:$reqUrl "
sendemail -f $fSendAdress -t $tAccept -s $sSmtp -u $subject -o message-content-type=html -o
message-charset=utf8 -xu $xuSendaccount -xp $xpPasswd -m $mContent
else
echo "$(date):restart status ===> failed" >> $logDir
fi
fi
#---------------------------------------------------------------------------------
if [ $urlPath == 'email' ]; then
echo -e "
$(date):============$reqIP:$reqPortOther $ProjName start ============ " >> $logDir
#请求目标url
justWeb2=`curl -I -m 10 -o /dev/null -s -w %{http_code} $reqUrl2 `
echo "$(date):request url ===> " $reqUrl2 >> $logDir
echo "$(date):request code ===> " $justWeb2 >> $logDir
#若请求目标url返回异常状态码则重启服务
if [ ${justWeb2}x != "200"x ] ; then
echo "$(date):$reqIP:$reqPortOther be executed shell ===> " $dealShell2 >> $logDir
#服务重启脚本
$dealShell2
#服务重启状态是否成功
if [ $? == 0 ]; then
echo "$(date):restart status ===> success" >> $logDir
mContent="老板,$reqIP:$reqPortOther 服务器上的$ProjName 挂了!已自动处理,可通过访问如下地址验证:$reqUrl "
sendemail -f $fSendAdress -t $tAccept -s $sSmtp -u $subject -o message-content-type=html -o
message-charset=utf8 -xu $xuSendaccount -xp $xpPasswd -m $mContent
else
echo "$(date):restart status ===> failed" >> $logDir
fi
fi
fi
echo "$(date):============================================================" >> $logDir
©️公众号:思考者文刀
- 上一篇: sendEmail的安装
- 下一篇: 随地球去流浪


















评论