HAPROXY_CONFIG='./haproxy.cfg'
MYSQL_SERVER=./opt/mysql_server
mysql0="192.168.251.176:33361"
mysql1="192.168.251.154:33362"
mysql2="192.168.251.152:33363"
MYSQL_MEM=($mysql0 $mysql1 $mysql2)
MD5_FILE=./opt/haproxy_config.md5
PID_FILE=/run/haproxy.pid
exec_sql="select * from performance_schema.replication_group_members;"
MYSQL_REPL_USER="repl"
MYSQL_REPL_PASSWORD="Xjb0^DDy"

mysqla="mysql-mgra-0.mysql-mgra.edoc2.svc.cluster.local"
mysqlb="mysql-mgrb-0.mysql-mgrb.edoc2.svc.cluster.local"
mysqlc="mysql-mgrc-0.mysql-mgrc.edoc2.svc.cluster.local"
subStr="#"

check_status() {
    info=`cat ${HAPROXY_CONFIG} | grep "${HOSTNAME}"`
    if [[ ${info} == *${subStr}* ]];then
        str_status="t"
        echo "包含#"
    else
        str_status="f"
        echo "不包含#"
    fi
    rest_info=`cat ${HAPROXY_CONFIG} | grep "${HOSTNAME}" | awk -F " " '{print$1" "$2}'`
    echo "${rest_info}"
}

change_conf() {
    if [ "${mysql_status1}" == "ONLINE" ] && [ "${mysql_status2}" == "PRIMARY" ];then
        if [ $str_status == "t" ];then
            sed -i "s/${rest_info}/${new_conf}/g" "${HAPROXY_CONFIG}"
        elif [ $str_status == "f" ];then
            echo "nothing to do"
        fi
    else
        if [ $str_status == "t" ];then
            echo "nothing to do"
        elif [ $str_status == "f" ];then
            sed -i "s/${rest_info}/#${new_conf}/g" "${HAPROXY_CONFIG}"
        fi
    fi
}

while true
do
    for HOSTNAME in ${MYSQL_MEM[@]};do
        echo "========================================================================="
        echo "$HOSTNAME"
        echo ${HOSTNAME%:*}
        echo ${HOSTNAME#*:}
        if [ "${HOSTNAME}" == "192.168.251.176:33361" ];then
            echo "111111111111"
            echo "${mysqla}"
            mysql_status1=`mysql -u$MYSQL_REPL_USER -p$MYSQL_REPL_PASSWORD -h${HOSTNAME%:*} -P${HOSTNAME#*:} -e "${exec_sql}" | grep "${mysqla}" | awk -F " " '{print $5}'`
            mysql_status2=`mysql -u$MYSQL_REPL_USER -p$MYSQL_REPL_PASSWORD -h${HOSTNAME%:*} -P${HOSTNAME#*:} -e "${exec_sql}" | grep "${mysqla}" | awk -F " " '{print $6}'`
            check_status
            new_conf="server mysqlserver0"
            change_conf 
        elif [ "${HOSTNAME}" == "192.168.251.154:33362" ];then
            echo "222222222222"
            echo "${mysqlb}"
            mysql_status1=`mysql -u$MYSQL_REPL_USER -p$MYSQL_REPL_PASSWORD -h${HOSTNAME%:*} -P${HOSTNAME#*:} -e "${exec_sql}" | grep "${mysqlb}" | awk -F " " '{print $5}'`
            mysql_status2=`mysql -u$MYSQL_REPL_USER -p$MYSQL_REPL_PASSWORD -h${HOSTNAME%:*} -P${HOSTNAME#*:} -e "${exec_sql}" | grep "${mysqlb}" | awk -F " " '{print $6}'`
            check_status
            new_conf="server mysqlserver1"
            change_conf
        elif [ "${HOSTNAME}" == "192.168.251.152:33363" ];then
            echo "333333333333"
            echo "${mysqlc}"
            mysql_status1=`mysql -u$MYSQL_REPL_USER -p$MYSQL_REPL_PASSWORD -h${HOSTNAME%:*} -P${HOSTNAME#*:} -e "${exec_sql}" | grep "${mysqlc}" | awk -F " " '{print $5}'`
            mysql_status2=`mysql -u$MYSQL_REPL_USER -p$MYSQL_REPL_PASSWORD -h${HOSTNAME%:*} -P${HOSTNAME#*:} -e "${exec_sql}" | grep "${mysqlc}" | awk -F " " '{print $6}'`
            check_status
            new_conf="server mysqlserver2"
            change_conf
        fi
        echo ${mysql_status1}
        echo ${mysql_status2}
        echo "========================================================================="
    done
    if [ ! -f ${PID_FILE} ];then
        echo "start haproxy"
        /usr/bin/md5sum ${HAPROXY_CONFIG}|cut -f1 -d ' ' > ${MD5_FILE}
        # #haproxy -W -db -f ${HAPROXY_CONFIG} -p ${PID_FILE} &
    else
        if [ "$(/usr/bin/md5sum ${HAPROXY_CONFIG}|cut -f1 -d ' ')" != "$(cat ${MD5_FILE})" ];then
            /usr/bin/md5sum ${HAPROXY_CONFIG}|cut -f1 -d ' ' > ${MD5_FILE}
            echo "reload haproxy"
            #haproxy -W -db -f ${HAPROXY_CONFIG} -st $(cat ${PID_FILE}) -p ${PID_FILE} &
        fi
    fi
    sleep 5
done
