layout: post
title: keepalived—nginxcheck
date: 2018-03-13
tags: [“Cluster services”,”软件服务”]
function init(){
LOCKFILE=/tmp/nginxCheck.lock
[ -f $LOCKFILE ] && echo -e "\033[31m nginxCheck is running will exit \033[0m"&&exit '' touch $LOCKFILE
/sbin/ifconfig bond0 &>/dev/null
BOND0=$?
/sbin/ifconfig em1 &>/dev/null
ETH0=$?
if [ $BOND0 -eq 0 ];then
ETH=bond0;
elif [ $ETH0 -eq 0 ];then
ETH=em1
else
ETH=em2;
fi
USER=`whoami`
IP=$(/sbin/ifconfig $ETH ' grep -i "inet addr" ' awk '{print $2}' ' sed 's/addr://')
VIP=$(grep label /etc/keepalived/keepalived.conf ' tr '/' ' ' ' awk '{print $1}')
NGPID=$(ps -ef ' grep "nginx: master" ' grep -v grep ' awk '{print $2}')
if [[ ! -n "$NGPID" ]];then
echo -e "\033[31m nginx master not running \033[0m\n"
if [ $USER == root ];then
echo -e "\033[31m user is root try start nginx \033[0m\n"
killall nginx
sleep 3
/app/tengine-2.2.1/sbin/nginx
START_STATUS=$?
if [ $START_STATUS == 0 ];then
echo -e "\033[32m nginx is restart sucess \033[0m\n"
echo "nginx restart `date`" >> /tmp/nginxcheck.log
else
echo -e "\033[31m nginx is restart fail please chechk \033[0m \n"
fi
else
echo -e "\033[31m user is not root \033[0m\n"
fi
else
echo -e "\033[32m nginx master is running \033[0m \n"
fi
N=1
M=$(grep "listen" /app/tengine-2.2.1/conf/nginx.conf ' grep -v ^# ' tr ';' ' ' ' awk '{print $2}' ' wc -l)
}
checkVipPort(){
echo -e "\033[33m start check VIP status \033[0m \n"
ifconfig'grep $VIP >> /dev/null
VIP_STATUS=$?
if [ $VIP_STATUS == 0 ];then
echo -e "\033[32m $VIP in localhost \033[0m \n"
IS_IN=1
else
echo -e "\033[31m $VIP not in localhost \033[0m \n"
IS_IN=0
fi
echo -e "\033[33m check VIP status end \033[0m \n"
echo -e "\033[33m start check port connect status \033[0m \n"
if [ $IS_IN == 0 ];then
VIP=$IP
fi
for i in $(grep "listen" /app/tengine-2.2.1/conf/nginx.conf ' grep -v ^# ' tr ';' ' ' ' awk '{print $2}')
do
PORT=$i
curl -i -m 1 "http://$VIP:$PORT/lvs.jsp" &>/dev/null
GLOBAL_STAT=$?
if [[ $GLOBAL_STAT -eq 0 ]];then
echo -e "\033[32m $VIP:$PORT is running \033[0m\n"
elif [[ $N -lt $M ]];then
echo -e "\033[31m $VIP:$PORT is stoped \033[0m\n"
let N++
continue
else
if [ $USER == root ];then
echo -e "\033[31m user is root try start nginx \033[0m\n"
killall nginx
sleep 3
/etc/init.d/keepalived restart
/app/tengine-2.2.1/sbin/nginx
START_STATUS=$?
if [ $START_STATUS == 0 ];then
echo -e "\033[32m nginx is restart sucess \033[0m\n"
echo "nginx restart `date`" >> /tmp/nginxcheck.log
else
echo -e "\033[31m nginx is restart fail please chechk \033[0m \n"
fi
else
echo -e "\033[31m user is not root \033[0m\n"
fi
fi
#del lock file
rm -f $LOCKFILE
done
echo -e "\033[33m check port connect status end \033[0m \n"
}
init
checkVipPort
文档更新时间: 2018-12-20 11:19 作者:张尚