This bash script we used for our of our client.
to use simple create a file e.g failover.sh and paste the below mentioned script into it.
now configure a cronjob to run this script every after 5 minutes or whatever you like.
#!/bin/bash
HOST1="10.2.4.115"
HOST2="10.2.4.116"
COUNT=4
for myHost in $HOST1
do
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }' 2>/dev/null)
#count=$count"skj"
# echo $count" ccc"
if [ $count -gt 0 ]; then
# 100% failed
g_count=`grep -c $HOST1 /etc/hosts`
if [ $g_count -eq 0 ]; then
echo "change host1 ip"
`cat /etc/hosts | sed 's/10.2.4.116/10.2.4.115/' > /etc/hosts_tmp`
`cp /etc/hosts_tmp /etc/hosts --reply=yes`
fi
echo "Host : $myHost is UP"
else
g_count=`grep -c $HOST2 /etc/hosts`
echo $g_count
if [ $g_count -eq 0 ]; then
echo "change host2 ip"
`cat /etc/hosts | sed 's/10.2.4.115/10.2.4.116/' > /etc/hosts_tmp`
`cp /etc/hosts_tmp /etc/hosts --reply=yes`
fi
echo "Host : $myHost is down"
fi
done