|
| ||||||||
|
巻き戻し中。
|
|
2020-07-16(木) 改良 [長年日記]
_ なんちゃって
fakeなvrrpdは、マスターの応答をタイムアウト1秒のpingで見てるので、 高負荷などで応答が滞った時にパタパタ(flap)しないようにちょっと手を入れた。#!/bin/sh # This is "Fake VRRP daemon" # Ver 1.03 # By H.Uekusa 20200716 # Put "fakevrrpd" into /usr/local/etc/rc.d # such as below # and do # "service fakevrrpd enable" # "service fakevrrpd enabled" # "service fakevrrpd start" #---------------------from here----------------------- # #!/bin/sh # # -*- coding: utf-8; mode: shell-script; -*- # # insert `fakevrrpd_enable="YES"' in /etc/rc.conf # # Use with fvrrpd # # By H.Uekusa 20200630 # # # # PROVIDE: fakevrrpd # # PROG="/usr/local/etc/fvrrpd" # # . /etc/rc.subr # # name=fakevrrpd # rcvar=fakevrrpd_enable # procname=/bin/sh # pidfile=/var/run/fvrrpd.pid # # start_cmd="${name}_start" # stop_cmd="${name}_stop" # # load_rc_config $name # : ${fakevrrpd_enable:="NO"} # # fakevrrpd_start () { # echo "Starting fakevrrpd" # $PROG -d # } # # fakevrrpd_stop () { # echo "Stopping fakevrrpd" # $PROG -k # } # # run_rc_command "$1" #-----------------------to here----------------------- # ############### configuration valiables ################ CAT="/bin/cat" GREP="/usr/bin/grep" IFCONFIG="/sbin/ifconfig" KILLA="/usr/bin/killall" LOGGER="/usr/bin/logger" PING="/sbin/ping" NC="/usr/bin/nc -z -w 1 " RESTART_NAMED="/usr/sbin/service named restart" # VIP="192.168.10.100" MASK="netmask 255.255.255.0" MASTER="192.168.10.10" #Actual master IP address IF="em0" #Physical interface name in ifconfig TIMEOUT="3" # Timeout for ping (Seconds) PORT="80" #Monitor port number for back up the master WAIT_LIMIT="60" #Limit for waiting the service port (Seconds) DOUBLE_CHECK="YES" #Double check for Master down (YES/NO) CHECK_LOOPNUM="4" #Number of loop for double check CHECK_INTERVAL="2" #Sleep time for double check (Seconds) # PIDFILE="/var/run/fvrrpd.pid" #It must be same configuration with fakevrrpd in rc.d. LOGNAME="fake_vrrpd" #Name on the messages ######################################################## ################### main procedure ##################### main () { STAT_PREV="0" PID=`echo $$` echo $PID > $PIDFILE # if [ "$1" == "verbose" ];then LOG="echo" elif [ "$1" == "daemon" ];then LOG=`echo $LOGGER -is -t $LOGNAME` else exit 3 fi # $LOG "Fake vrrpd PID is $PID" # while [ 1 ]; do #Infinite loop $PING -c1 -t$TIMEOUT $MASTER >/dev/null 2>&1 STAT_NOW=`echo $?` if [ $STAT_NOW != $STAT_PREV ];then #Found change if [ $STAT_NOW != 0 ];then #Change is master down if [ "$DOUBLE_CHECK" == "YES" ];then # Do double check LOOP_COUNT="0" while [ $LOOP_COUNT -lt $CHECK_LOOPNUM ];do $PING -c1 -t$TIMEOUT $MASTER >/dev/null 2>&1 STAT_DOUBLE=`echo $?` LOOP_COUNT=`expr $LOOP_COUNT + 1` sleep $CHECK_INTERVAL done else STAT_DOUBLE=1 fi if [ $STAT_DOUBLE != 0 ];then # Double check is fail $LOG "Master $MASTER down detected! I will become the Master" $IFCONFIG $IF |$GREP $VIP >/dev/null 2>&1 MY_VIP_ENABLED=`echo $?` if [ $MY_VIP_ENABLED != 0 ];then #My VIP is not set $PING -c1 -t$TIMEOUT $VIP >/dev/null 2>&1 STAT_VIP=`echo $?` if [ $STAT_VIP != 0 ];then #Another VIP is not found $LOG "I become the Master!" $IFCONFIG $IF alias $VIP $MASK $LOG "Restarting named" $RESTART_NAMED else $LOG "The Master seems down, but there is another Master ??" $LOG "To avoid conflict, do nothing." fi else $LOG "I am already the Master? Do nothing." fi else $LOG "Master $MASTER flapped, But baked up now." fi else #Change is master back up $LOG "Master $MASTER interface back up detected! Waiting for enabling service at $PORT." NC_STAT="1" STAT_COUNT="0" while [ $NC_STAT != 0 ];do $NC $MASTER $PORT NC_STAT=`echo $?` echo "Status $NC_STAT" echo "Count $STAT_COUNT" if [ $STAT_COUNT -gt $WAIT_LIMIT ];then $LOG "Waiting for enabling service at $PORT timed out $WAIT_LIMIT sec, aborting." $IFCONFIG $IF |$GREP $VIP >/dev/null 2>&1 MY_VIP_ENABLE=`echo $?` if [ $MY_VIP_ENABLE = 0 ];then #My VIP was set $LOG "Removing alias $VIP for master" $IFCONFIG $IF -alias $VIP else $LOG "VIP $VIP is already removed or not set, Nothing to do." fi $LOG "Restarting named" $RESTART_NAMED rm -f $PIDFILE exit 5 fi STAT_COUNT=`expr $STAT_COUNT + 1` sleep 1 done $LOG "Master $MASTER service back up detected! I will become the standby" $IFCONFIG $IF |$GREP $VIP >/dev/null 2>&1 MY_VIP_ENABLE=`echo $?` if [ $MY_VIP_ENABLE = 0 ];then #My VIP was set $LOG "I become the standby! Removing alias $VIP" $IFCONFIG $IF -alias $VIP $LOG "Restarting named" $RESTART_NAMED else $LOG "VIP $VIP is already removed or not set, Nothing to do." fi fi fi echo "IF Status $STAT_PREV->$STAT_NOW" STAT_PREV=`echo $STAT_NOW` sleep 1 done rm -f $PIDFILE exit 4 } ######################################################## ############## check arguments and execute ############# if [ "$1" == "-d" ];then #Run as daemon LOG=`echo $LOGGER -is -t $LOGNAME` $LOG "Fake vrrpd is starting as daemon" if [ -f "$PIDFILE" ];then #Exist PID file? $LOG "There is PID file at $PIDFILE,I'm exiting" $LOG "If you would like to start forcibly, Use -k option first" exit 2 fi $0 -vd >/dev/null 2>&1 & elif [ "$1" == "-h" ];then #Display help echo "Usage" echo " -d : Run as daemon" echo " -h : Display help" echo " -v : Run on foreground" echo " -k : Kill daemon" elif [ "$1" == "-k" ];then #Killing existing process LOG=`echo $LOGGER -is -t $LOGNAME` if [ -f "$PIDFILE" ];then #Exist PID file? PIDKILL=`$CAT $PIDFILE` $LOG "Killing fake vrrpd $PIDKILL" kill -9 $PIDKILL rm -f $PIDFILE $LOG "removing PID file" else $LOG "PID file not found, killing with killall" $KILLA $0 fi $IFCONFIG $IF |$GREP $VIP >/dev/null 2>&1 MY_VIP_ENABLE=`echo $?` if [ $MY_VIP_ENABLE = 0 ];then #My VIP was set $LOG "Removing alias $VIP for master" $IFCONFIG $IF -alias $VIP else $LOG "VIP $VIP is already removed or not set, Nothing to do." fi $LOG "Restarting named" $RESTART_NAMED exit 1 elif [ "$1" == "-v" ];then #Run on foreground LOG="echo" if [ -f "$PIDFILE" ];then #Exist PID file? $LOG "There is PID file at $PIDFILE,I'm exiting" $LOG "If you would like to start forcibly, Use -k option first" exit 2 fi main verbose elif [ "$1" == "-vd" ];then #Run as daemon main daemon else echo "Usage" echo " -d : Run as daemon" echo " -h : Display help" echo " -v : Run on foreground" echo " -k : Kill daemon" fi exit 0 ########################################################
Tweets by RC31E | |||||||||
| |||||||||
| |||||||||
|