#!/bin/bash
####################################################################
# description：用于安装keepalive软件，实现centos6.4双机热备
# author：王志刚 18633809213
# date:2019年10月23日
####################################################################
CONFIG_PATH=/etc/keepalived/keepalived.conf
#参数配置
hostType=$1			#主机配置 1:MASTER  2:BACKUP
vip=$2				#虚拟ip 需要带子网掩码 ex:192.168.3.12/24
synPath=$3			#同步文件夹路径
netDevice=$4		#内网网卡名称 

#必要软件检查和安装
echo "必要软件检查和安装......"
if [ -z rpm -qa |grep net-snmp ];then
	echo "安装net-snmp....."
	yum install -y net-snmp
elif [ -z rpm -qa |grep keeplived ]
	echo "安装keeplived....."
	yum install -y keeplived
else
	echo "已安装必要软件"
fi

#修改keepalive配置文件
[ $hostType==1 ] && hostType=MASTER || hostType=BACKUP

cat <<EOF > ${CONFIG_PATH}
! Configuration File for keepalived
global_defs {
   router_id ${hostType}
}
vrrp_instance VI_1 {
    state ${hostType}
    interface ${netDevice}
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        ${vip}
    }
}
EOF


#重启keepalive服务器，注册开机启动
service keeplived restart
chkconfig keeplived on
