周末在家闲的无聊,想到公司的备份服务器需要双机热备,原来我用得是heartbeat套件,感觉不错。不过感觉很复杂,通过udp694端口检测心跳来控制删除添加VIP,是不是心跳也可以用icmp包来检测呢?因为没学过shell,但懂得些C语言知识,找了一些控制选择语句(if-else-fi while do done),和C差别不大,那就写吧,经过半天的查资料与写程序虚拟机上调试(从零起步)。终于完成了:
主服务器:
#!/bin/bash
#Main server
BACKUP_HOST_IP=10.0.3.202 #备份服务器主机IP
MAINSERVER_PING_IP=10.0.3.204 #主服务器心跳检测IP
VIP_NETCARD_NAME=eth0
VIP=10.0.0.200
VIP_NETMASK=255.255.252.0
OK_PING_TIME=5
FAILD_PING_TIME=3
################
TAKEOVER=0 #是否IP转移过
OKNUMBER=1 #区分是否输出
STATE=1 #成功PING的状态
STATE1=1 #失败后PING的状态
################
ifconfig "$VIP_NETCARD_NAME:10" $VIP netmask $VIP_NETMASK up
service vsftpd start > /dev/null 2>&1
while :
do
ping -c 1 -I $MAINSERVER_PING_IP $BACKUP_HOST_IP > /dev/null 2>&1
STATE=$?
{
if [ $STATE -eq 0 ];then
{
if [ $TAKEOVER -eq 0 ] ; then
{
echo "$OKNUMBER Backup server is OK!"
OKNUMBER=$[-1*$OKNUMBER]
sleep $OK_PING_TIME
}
else [ $TAKEOVER -eq 1 ]
{
echo "The network recover start VIP...... "
ifconfig "$VIP_NETCARD_NAME:10" $VIP netmask $VIP_NETMASK up
service vsftpd start > /dev/null 2>&1
TAKEOVER=0
}
fi
}
else [ $STATE -ne 0 ]
{
echo "Number $? Backup server failed! "
sleep $FAILD_PING_TIME
{
for STATE in 1 2 3 4 5
do
ping -c 1 -I $MAINSERVER_PING_IP $MAIN_HOST_IP > /dev/null 2>&1
STATE1=$?
if [ $STATE1 -eq 0 ];then
{
break
}
else [ $STATE1 -ne 0 ]
{
echo "Number $STATE Backup server failed!"
if [ $STATE -eq 5 ];then
{
echo "The network breakdown give up VIP! ...... "
ifconfig "$VIP_NETCARD_NAME:10" $VIP netmask $VIP_NETMASK down
service vsftpd stop > /dev/null 2>&1
TAKEOVER=1
#IP takeover program!
}
fi
}
fi
done
}
}
fi
}
done
备份服务器:
#!/bin/bash
#Backup server
MAIN_HOST_IP=10.0.3.204 #主服务器主机IP
BACKUPSERVER_PING_RIP=10.0.3.202 #备份服务器服务器心跳检测IP
VIP_NETCARD_NAME=eth0
VIP=10.0.0.200
VIP_NETMASK=255.255.252.0
OK_PING_TIME=5
FAILD_PING_TIME=3
####################
OKNUMBER=1
TAKEOVER=0
STATE=1
STATE1=1
####################
ifconfig "$VIP_NETCARD_NAME:10" $VIP netmask $VIP_NETMASK down
service vsftpd stop > /dev/null 2>&1
while :
do
ping -c 1 -I "$BACKUPSERVER_PING_RIP" "$MAIN_HOST_IP" > /dev/null 2>&1
STATE=$?
{
if [ $STATE -eq 0 ];then
{
if [ $TAKEOVER -eq 0 ] ; then
{
echo "$OKNUMBER Main server is OK!"
OKNUMBER=$[ -1*$OKNUMBER ]
sleep $OK_PING_TIME
}
else [ $TAKEOVER -eq 1 ]
{
echo "The network recover release VIP "
ifconfig "$VIP_NETCARD_NAME:10" $VIP netmask $VIP_NETMASK down
service vsftpd stop > /dev/null 2>&1
TAKEOVER=0
}
fi
}
else [ $STATE -ne 0 ]
{
echo "Number $? Main server failed! "
sleep $FAILD_PING_TIME
{
for STATE in 1 2 3 4 5
do
ping -c 1 -I $BACKUPSERVER_PING_RIP $MAIN_HOST_IP > /dev/null 2>&1
STATE1=$?
if [ $STATE1 -eq 0 ];then
{
break
}
else [ $STATE1 -ne 0 ]
{
echo "Number $STATE Main server failed!"
if [ $STATE -eq 5 ];then
{
echo "The network breakdown start VIP! ...... "
ifconfig "$VIP_NETCARD_NAME:10" $VIP netmask $VIP_NETMASK up
service vsftpd start > /dev/null 2>&1
TAKEOVER=1
#IP takeover program!
}
fi
}
fi
done
}
}
fi
}
done
这两个程序分别运行在主服务器与备份服务器上,通过PING对方主机,如果连续5次每次3秒 ping不到说明网络或主机故障,主服务器放弃VIP停止服务,备份服务器开始VIP,并启动服务,功能很简单呵呵。
转自:
阅读(1458) | 评论(0) | 转发(0) |