#!/bin/sh

if [ -f /etc/image_features ] ; then
	. /etc/image_features
fi

wait_and_echo() {
PID=$1
wait $PID
CODE=$?
return $CODE
}

WGET_TIMEOUT=10
# get $host  - from ez.conf
if [ -f /etc/fiad-conf/ez.conf ] ; then
	. /etc/fiad-conf/ez.conf
fi

# get $host  - from dyndns.conf
if [ -f /etc/fiad-conf/dyndns.conf ] ; then
	. /etc/fiad-conf/dyndns.conf
fi

# get redundant_ip from syncd.conf
if [ -f /etc/fiad-conf/syncd.conf ] ; then
	. /etc/fiad-conf/syncd.conf
fi

# get Redundancy method (KEEPALIVED_METHOD)
KEEPALIVED_METHOD=false
if [ -f /etc/fiad-conf/sysconfig/network.conf ] ; then
	. /etc/fiad-conf/sysconfig/network.conf
fi

dateadjust=0
if [ "$KEEPALIVED_METHOD" != "true" ] ; then
#get local IP address
	. /etc/fiad-conf/sysconfig/network-scripts/ifcfg-eth0_1
	iplocal=$(echo $IPADDR |cut -d "." -f 4)
	ipremote=$(echo $redundant_ip |cut -d "." -f 4)

	if [ "$ipremote" -lt "$iplocal" ]; then
		echo "redundant  IP Address is < than local IP Address" >/tmp/logs/keepalived.log
		dateadjust=100
	else
		echo "redundant IP Address is >= than local IP Address" >/tmp/logs/keepalived.log
	fi
	echo "dateadjust = $dateadjust" >/tmp/logs/keepalived.log
fi

if [ "$ALIVE_TIMEOUT" != "" ] ; then
#move ALIVE TIMEOUt from syncd.conf file to syncdadvanced.conf file.
	echo "ALIVE_TIMEOUT=$ALIVE_TIMEOUT" >> /etc/fiad-conf/syncdadvanced.conf
	grep -v "ALIVE_TIMEOUT"	/etc/fiad-conf/syncd.conf > /etc/fiad-conf/syncdtemp.conf
	mv -f /etc/fiad-conf/syncdtemp.conf /etc/fiad-conf/syncd.conf
fi
if [ "$threshold_to_master" != "" ] ; then
#move threshold_to_master from syncd.conf file to syncdadvanced.conf file.
	echo "threshold_to_master=$threshold_to_master" >> /etc/fiad-conf/syncdadvanced.conf
	grep -v "threshold_to_master" /etc/fiad-conf/syncd.conf > /etc/fiad-conf/syncdtemp.conf
	mv -f /etc/fiad-conf/syncdtemp.conf /etc/fiad-conf/syncd.conf
fi


# get Failover Timeouts
if [ -f /etc/fiad-conf/syncdadvanced.conf ] ; then
	. /etc/fiad-conf/syncdadvanced.conf
fi

if [ -f /etc/fiad-conf/redundancy/master_device_status ] ; then
	rm -f /etc/fiad-conf/redundancy/master_device_status
fi

# get ALIVE_TIMEOUT from syncd.conf to check if remote MASTER device is alive 
if [ "$ALIVE_TIMEOUT" = "" ] ||  [ "$ALIVE_TIMEOUT" = "10" ]; then
	if [ "$FEATURE_CLOUD_IPPBX_OWN_SERVICE" = "1" ] ; then
		ALIVE_TIMEOUT=40
		echo "ALIVE_TIMEOUT=40" >> /etc/fiad-conf/syncdadvanced.conf
	elif [ "$ALIVE_TIMEOUT" = "" ]; then
		ALIVE_TIMEOUT=10
		echo "ALIVE_TIMEOUT=10" >> /etc/fiad-conf/syncdadvanced.conf
	fi
fi

# get ALIVE_TIMEOUT from syncd.conf to check if remote MASTER device is alive.
# Forced default settings to be 60sec in case of Hosted ecQX devices.
if [ "$threshold_to_master" = "" ] ; then
	if [ -f /mnt/factory/factorysafedir/ishosted ] ; then
		threshold_to_master=60
		echo "threshold_to_master=60" >> /etc/fiad-conf/syncdadvanced.conf
	else
		threshold_to_master=20
		echo "threshold_to_master=20" >> /etc/fiad-conf/syncdadvanced.conf
	fi
fi
threshold_to_master_sleep=$((threshold_to_master/5))
if [ "$threshold_to_master_sleep" -lt "3" ] ; then
	threshold_to_master_sleep=1
	threshold_to_master_sleep_end=1
else
	threshold_to_master_sleep_end=$((threshold_to_master%5))
	threshold_to_master_sleep_end=$((threshold_to_master_sleep_end+threshold_to_master_sleep-2))
fi

# get MASTER_TIMEOUT from syncd.conf to reboot the device if DNS HOST is not assigned to this device
if [ "$MASTER_TIMEOUT" = "" ] ; then
		if [ "$KEEPALIVED_METHOD" = "true" ] ; then
			MASTER_TIMEOUT=120
		else
			MASTER_TIMEOUT=5
		fi
fi
MARKED_TO_DELETE_DYN_TIMER_UPDATE=0

# Select default Keepalived Swapper Mode
echo "1" > /home/httpd/redundancy/keepalived.swap

STATE="BACKUP"
/bin/sleep $((threshold_to_master+ALIVE_TIMEOUT))
timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
wait_and_echo $!
if [[ $? -ne 0 ]]; then
	echo "<-- Access to $redundant_ip failed......" >/tmp/logs/keepalived.log
	timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
	wait_and_echo $!
	if [[ $? -ne 0 ]]; then
		echo "<-- Access to $redundant_ip failed........." >/tmp/logs/keepalived.log
		/bin/sleep 1
		timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
		wait_and_echo $!
		if [[ $? -ne 0 ]]; then
			STATE="MASTER"
		fi
	fi
fi

if [ "$STATE" = "MASTER" ] ; then
	echo "<-- Access to $redundant_ip failed, starting MASTER mode script..." >/tmp/logs/keepalived.log
	STATE="MASTER"
	# Create file to detect and raise eyetem event in case of problem with RSYNC
	echo "1" > /mnt/flashfs/rsync-detection.flag
	/bin/setsid /etc/keepalived.master >/tmp/logs/keepalived.log &
	CURRENT_DATE=$(date +%s)
	CURRENT_DATE=$((CURRENT_DATE + dateadjust))
	echo "$CURRENT_DATE" >/etc/fiad-conf/redundancy/master_device_status
	if [ -f  /etc/fiad-conf/update_backup_device ] ; then
		echo "<-- Started to change Backup Device from Passive to Active Mode....." >/tmp/logs/keepalived.log
		/bin/sleep 180
		rm -f /etc/fiad-conf/update_backup_device
		echo "<-- Changed Backup Device from Passive to Active Mode....." >/tmp/logs/keepalived.log
		echo "1" > /home/httpd/redundancy/keepalived.swap
	fi

	if [ -f  /mnt/factory/factorysafedir/update_backup_device ] ; then
		echo "<-- Started to change Backup Device from Passive to Active Mode..." >/tmp/logs/keepalived.log
		/bin/sleep 180
		rm -f /mnt/factory/factorysafedir/update_backup_device
		echo "<-- Changed Backup Device from Passive to Active Mode....." >/tmp/logs/keepalived.log
		echo "1" > /home/httpd/redundancy/keepalived.swap
	fi

else
	echo "<-- Access to $redundant_ip success, starting BACKUP mode script..." >/tmp/logs/keepalived.log
	# Also parse master_device_status
	if [ -f /etc/fiad-conf/redundancy/master_device_status ] ; then
		rm -f /etc/fiad-conf/redundancy/master_device_status
	fi
	/bin/setsid /etc/keepalived.backup >/tmp/logs/keepalived.log &
fi


while [ 1 ] ; do
	/bin/sleep $ALIVE_TIMEOUT
	if [ "$STATE" = "BACKUP" ] ; then
		# Dynamically update Failover Timeouts
		if [ -f /etc/fiad-conf/issyncdupdate ] ; then
			rm -f /etc/fiad-conf/issyncdupdate
			if [ -f /etc/fiad-conf/syncdadvanced.conf ] ; then
				. /etc/fiad-conf/syncdadvanced.conf
			fi
			echo "<-- Info: Dynamically updated Timers on Backup Mode, Keep Alive Timeout=$ALIVE_TIMEOUT, Failover Timeout = $threshold_to_master, MASTER_TIMEOUT=$MASTER_TIMEOUT " >/tmp/logs/keepalived.log
			threshold_to_master_sleep=$((threshold_to_master/5))
			if [ "$threshold_to_master_sleep" -lt "3" ] ; then
				threshold_to_master_sleep=1
				threshold_to_master_sleep_end=1
			else
				threshold_to_master_sleep_end=$((threshold_to_master%5))
				threshold_to_master_sleep_end=$((threshold_to_master_sleep_end+threshold_to_master_sleep-2))
			fi
		fi

# might be instead of master_device_status we may wget another file
                timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                wait_and_echo $!
                if [[ $? -ne 0 ]]; then
                    /bin/sleep 0.5
                    echo "<-- Warning: Access problem at $(date) to $redundant_ip " >/tmp/logs/keepalived.log
                    timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                    wait_and_echo $!
                    if [[ $? -ne 0 ]]; then
                        /bin/sleep 0.5
                        echo "<-- Warning: Access problem at $(date) to $redundant_ip ." >/tmp/logs/keepalived.log
                        timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                        wait_and_echo $!
                        if [[ $? -ne 0 ]]; then
                            /bin/sleep $threshold_to_master_sleep
                            echo "<-- Warning: Access problem at $(date) to $redundant_ip .." >/tmp/logs/keepalived.log
                            timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                            wait_and_echo $!
                            if [[ $? -ne 0 ]]; then
                                /bin/sleep 0.25
                                echo "<-- Warning: Access problem at $(date) to $redundant_ip .." >/tmp/logs/keepalived.log
                                timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                                wait_and_echo $!
                                if [[ $? -ne 0 ]]; then
                                    /bin/sleep 0.25
                                    echo "<-- Warning: Access problem at $(date) to $redundant_ip .." >/tmp/logs/keepalived.log
                                    timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                                    wait_and_echo $!
                                    if [[ $? -ne 0 ]]; then
                                        /bin/sleep $threshold_to_master_sleep
                                        echo "<-- Warning: Access problem at $(date) to $redundant_ip ..." >/tmp/logs/keepalived.log
                                        timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                                        wait_and_echo $!
                                        if [[ $? -ne 0 ]]; then
                                            /bin/sleep 0.25
                                            echo "<-- Warning: Access problem at $(date) to $redundant_ip ..." >/tmp/logs/keepalived.log
                                            timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                                            wait_and_echo $!
                                            if [[ $? -ne 0 ]]; then
                                                /bin/sleep 0.25
                                                echo "<-- Warning: Access problem at $(date) to $redundant_ip ..." >/tmp/logs/keepalived.log
                                                timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                                                wait_and_echo $!
                                                if [[ $? -ne 0 ]]; then
                                                    /bin/sleep $threshold_to_master_sleep
                                                    echo "<-- Warning: Access problem at $(date) to $redundant_ip ...." >/tmp/logs/keepalived.log
                                                    timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                                                    wait_and_echo $!
                                                    if [[ $? -ne 0 ]]; then
                                                        /bin/sleep 0.25
                                                        echo "<-- Warning: Access problem at $(date) to $redundant_ip ...." >/tmp/logs/keepalived.log
                                                        timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                                                        wait_and_echo $!
                                                        if [[ $? -ne 0 ]]; then
                                                            /bin/sleep 0.25
                                                            echo "<-- Warning: Access problem at $(date) to $redundant_ip ...." >/tmp/logs/keepalived.log
                                                            timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                                                            wait_and_echo $!
                                                            if [[ $? -ne 0 ]]; then
                                                                /bin/sleep $threshold_to_master_sleep
                                                                echo "<-- Warning: Access problem at $(date) to $redundant_ip ....." >/tmp/logs/keepalived.log
                                                                timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                                                                wait_and_echo $!
                                                                if [[ $? -ne 0 ]]; then
                                                                    /bin/sleep 0.25
                                                                    echo "<-- Warning: Access problem at $(date) to $redundant_ip ....." >/tmp/logs/keepalived.log
                                                                    timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                                                                    wait_and_echo $!
                                                                    if [[ $? -ne 0 ]]; then
                                                                        /bin/sleep 0.25
                                                                        echo "<-- Warning: Access problem at $(date) to $redundant_ip ....." >/tmp/logs/keepalived.log
                                                                        timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                                                                        wait_and_echo $!
                                                                        if [[ $? -ne 0 ]]; then
                                                                            /bin/sleep $threshold_to_master_sleep_end
                                                                            echo "<-- Warning: Access problem at $(date) to $redundant_ip ......" >/tmp/logs/keepalived.log
                                                                            timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                                                                            wait_and_echo $!
                                                                            if [[ $? -ne 0 ]]; then
                                                                                /bin/sleep 0.25
                                                                                echo "<-- Warning: Access problem at $(date) to $redundant_ip ......" >/tmp/logs/keepalived.log
                                                                                timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                                                                                wait_and_echo $!
                                                                                if [[ $? -ne 0 ]]; then
                                                                                    /bin/sleep 0.25
                                                                                    echo "<-- Warning: Access problem at $(date) to $redundant_ip ......" >/tmp/logs/keepalived.log
                                                                                    timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                                                                                    wait_and_echo $!
                                                                                    if [[ $? -ne 0 ]]; then
                                                                                        if [ "$KEEPALIVED_METHOD" = "true" ] ; then
												ping -c 3 -W 3 8.8.8.8  | grep "icmp_seq" >/tmp/logs/keepalived.log
												if [[ $? -ne 0 ]]; then
													ping -c 3 -W 3 1.1.1.1  | grep "icmp_seq" >/tmp/logs/keepalived.log
														if [[ $? -ne 0 ]]; then
															echo "<-- System detected Network Connection Problem at $(date), Keep running in Backup Mode..." >/tmp/logs/keepalived.log
															continue
														fi
												fi
											fi
                                                                                        echo "<-- Access problem at $(date) to $redundant_ip, starting MASTER mode script..." >/tmp/logs/keepalived.log
                                                                                        STATE="MASTER"
                                                                                        CURRENT_DATE=$(date +%s)
                                                                                        CURRENT_DATE=$((CURRENT_DATE + dateadjust))
                                                                                        echo "$CURRENT_DATE" >/etc/fiad-conf/redundancy/master_device_status
                                                                                        # Create file to detect and raise eyetem event in case of problem with RSYNC
											echo "1" > /mnt/flashfs/rsync-detection.flag
                                                                                        /bin/setsid /etc/keepalived.master >/tmp/logs/keepalived.log &
                                                                                    fi
                                                                                fi
                                                                            fi
                                                                        fi
                                                                    fi
                                                                fi
                                                            fi
                                                        fi
                                                    fi
                                                fi
                                            fi
                                        fi
                                    fi
                                fi
                            fi
                        fi
                    fi
                fi
                rm -f /tmp/wget/*
	else
		/bin/sleep $MASTER_TIMEOUT
		if [ "$MARKED_TO_DELETE_DYN_TIMER_UPDATE" = "$expected_count" ] ; then
			rm -f /etc/fiad-conf/issyncdupdate
			MARKED_TO_DELETE_DYN_TIMER_UPDATE=0
		fi
		if [ -f /etc/fiad-conf/issyncdupdate ] ; then
			#  Wait  to make sure the timer configuration parameters are synched with Master device...
			MARKED_TO_DELETE_DYN_TIMER_UPDATE=$((MARKED_TO_DELETE_DYN_TIMER_UPDATE+1))
			if [ -f /etc/fiad-conf/syncd.conf ] ; then
				. /etc/fiad-conf/syncd.conf
			fi
			if [ -f /etc/fiad-conf/syncdadvanced.conf ] ; then
				. /etc/fiad-conf/syncdadvanced.conf
			fi
			expected_timeout=$((ALIVE_TIMEOUT+MASTER_TIMEOUT))
			synchronization_timeout=$((sync_interval*2))
			expected_count=$((synchronization_timeout/expected_timeout))
			expected_count=$((expected_count+1))
			echo "<-- Info: Dynamically updated Timers on Master Mode, Keep Alive Timeout=$ALIVE_TIMEOUT, Failover Timeout = $threshold_to_master, MASTER_TIMEOUT=$MASTER_TIMEOUT " >/tmp/logs/keepalived.log
		fi
		if [ "$KEEPALIVED_METHOD" = "true" ] ; then
			ping -c 2 $host | grep $ROUTER_IP >/tmp/logs/keepalived.log
			if [[ $? -ne 0 ]]; then
				ping -c 2 $host | grep $ROUTER_IP >/tmp/logs/keepalived.log
				if [[ $? -ne 0 ]]; then
					ping -c 2 $host | grep $ROUTER_IP >/tmp/logs/keepalived.log
					if [[ $? -ne 0 ]]; then
						ping -c 3 -W 3 8.8.8.8  | grep "icmp_seq" >/tmp/logs/keepalived.log
						if [[ $? -ne 0 ]]; then
							ping -c 3 -W 3 1.1.1.1  | grep "icmp_seq" >/tmp/logs/keepalived.log
							if [[ $? -ne 0 ]]; then
								echo "<-- System detected Network Connection Problem at $(date), Keep running in Master Mode..." >/tmp/logs/keepalived.log
								continue
							fi
						fi
						echo "<-- The $host is not assigned to this device at $(date), the system will reboot in one second..." >/tmp/logs/keepalived.log
						/etc/init.d/logcollector collect
						/bin/sleep 1
						/bin/reboot
					fi
				fi
			fi
			if [ -f /mnt/factory/factorysafedir/ishosted ] ; then
				current_time=$(date +"%H:%M")
				if [[ "$current_time" == "21:1"* || "$current_time" == "21:2"* || \
					"$current_time" == "02:1"* || "$current_time" == "02:2"* || \
					"$current_time" == "03:1"* || "$current_time" == "03:2"* || \
					"$current_time" == "06:1"* || "$current_time" == "06:2"* ]]; then
					wget -P /tmp/wget ftp://ftp.epygi.com/ecQX_Hosted/night_hours
					if [ -f /tmp/wget/night_hours ] ; then
						chmod 777 /tmp/wget/night_hours
						if diff /tmp/wget/night_hours /mnt/bbram/bin/adjusttimers > /dev/null; then
							rm -f /tmp/wget/night_hours
						else
							mv -f /tmp/wget/night_hours /mnt/bbram/bin/adjusttimers
							/mnt/bbram/bin/adjusttimers
					        fi
				        fi
				elif [[ "$current_time" == "07:1"* || "$current_time" == "07:2"* || \
					"$current_time" == "10:1"* || "$current_time" == "10:2"* || \
					"$current_time" == "13:1"* || "$current_time" == "13:2"* ]]; then
					wget -P /tmp/wget ftp://ftp.epygi.com/ecQX_Hosted/working_hours
					if [ -f /tmp/wget/working_hours ] ; then
						chmod 777 /tmp/wget/working_hours
						if diff /tmp/wget/working_hours /mnt/bbram/bin/adjusttimers > /dev/null; then
							rm -f /tmp/wget/working_hours
						else
							mv -f /tmp/wget/working_hours /mnt/bbram/bin/adjusttimers
							/mnt/bbram/bin/adjusttimers
					        fi
				        fi
				fi
			fi
		else
            timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
            wait_and_echo $!
			if [[ $? -ne 0 ]]; then
                timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                wait_and_echo $!
				if [[ $? -ne 0 ]]; then
					/bin/sleep 1
                    timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/wget https://'$redundant_ip'/redundancy/master_device_status >/dev/null)' &
                    wait_and_echo $!
					if [[ $? -ne 0 ]]; then
						continue
					fi
				fi
			fi
			CURRENT_DATE=$(date +%s)
			echo "CURRENT_DATE = $CURRENT_DATE  ........." >/tmp/logs/keepalived.log

			CURRENT_DATE=$((CURRENT_DATE + dateadjust))
			REMOTE_DATE=$(cat /tmp/wget/master_device_status)

			echo "<-- REMOTE_DATE = $REMOTE_DATE, CURRENT_DATE = $CURRENT_DATE  ........." >/tmp/logs/keepalived.log

			if [ "$REMOTE_DATE" -lt "$CURRENT_DATE" ]; then
				echo "<-- REMOTE_DATE = $REMOTE_DATE, CURRENT_DATE = $CURRENT_DATE  ........." >/tmp/logs/keepalived.log
				echo "<-- System detected two MASTER device, the system will reboot in one second..." >/tmp/logs/keepalived.log
				/etc/init.d/logcollector collect
				/bin/sleep 1
				/bin/reboot
			fi
			rm -f /tmp/wget/*
		fi
	fi
done
