1 #!/bin/bash 2 # 3 VIP=192.168.43.200 4 CPORT=80 5 FAIL_BACK=127.0.0.1 6 RS=("192.168.43.202" "192.168.43.203") 7 declare -a RSSTATUS 8 RW=("2" "1") 9 RPORT=80 10 TYPE=g 11 CHKLOOP=3 12 LOG=/var/log/ipvsmonitor.log 13 14 addrs(){ 15 ipvsadm -a -t $VIP:$CPORT -r $1:$RPORT -$TYPE -w $2 16 [ $? -eq 0 ] && return 0 || return 1 17 } 18 delrs(){ 19 ipvsadm -d -t $VIP:$CPORT -r $1:$RPORT 20 [ $? -eq 0 ] && return 0 || return 1 21 } 22 checkrs(){ 23 local I=1 24 while [ $I -le $CHKLOOP ]; do 25 if curl --connect-timeout 1 http://$1 &> /dev/null; then 26 return 0 27 fi 28 let I++ 29 done 30 return 1 31 } 32 initstatus(){ 33 local I 34 local COUNT=0; 35 for I in ${RS[*]}; do 36 if checkrs $I; then 37 RSSTATUS[$COUNT]=1 38 else 39 RSSTATUS[$COUNT]=0 40 fi 41 let COUNT++ 42 done 43 } 44 initstatus 45 while :; do 46 let COUNT=0 47 for I in ${RS[*]}; do 48 if checkrs $I; then 49 if [ ${RSSTATUS[$COUNT]} -eq 0 ]; then 50 addrs $I ${RW[$COUNT]} 51 [ $? -eq 0 ] && RSSTATUS[$COUNT]=1 && echo " $I is back." >> $LOG 52 fi 53 else 54 if [ ${RSSTATUS[$COUNT]} -eq 1 ]; then 55 delrs $I 56 [ $? -eq 0 ] && RSSTATUS[$COUNT]=0 && echo " $I is gone." >> $LOG 57 fi 58 fi 59 let COUNT++ 60 done 61 sleep 5 62 done
转载于:https://www.cnblogs.com/Matchman/p/9112464.html