#!/bin/bash
#Author: Liang WeiCheng
ip_array=($(grep "server 10.112.84" /etc/nginx/nginx.conf |awk '{print $2}'))
while true
do
##对当前负载均衡的所有节点挨个检查
for((i=0;i<${#ip_array[*]};i++))
do
egrep "^#.*${ip_array[$i]}.*" /etc/nginx/nginx.conf &>/dev/null
[ $? -eq 0 ] && continue
status=`curl -s -w "%{http_code}" -o /dev/null ${ip_array[$i]}`
if [ ${status} -ne 200 ]
then
sed -i "s/server ${ip_array[$i]}/#server ${ip_array[$i]}/g" /etc/nginx/nginx.conf
/usr/sbin/nginx -s reload
fi
done
sleep 5
for((i=0;i<${#ip_array[*]};i++))
do
a=`curl -s -w "%{http_code}" -o /dev/null ${ip_array[$i]}`
if [ ${a} -eq 200 ];then
egrep "^#.*${ip_array[$i]}.*" /etc/nginx/nginx.conf &>/dev/nullif [ $? -eq 0 ];then
sed -i -r "s/#(.*${ip_array[$i]}.*)/\1/g" /etc/nginx/nginx.conf
/usr/sbin/nginx -s reload
fi
fi
done
done