#!/bin/bash

set -u

. `dirname $0`/functions

THIS_SCRIPT_NAME="`basename $0`"

write_switch_log "Start check."

for i in $PEER_HOST_ADDRESS_LIST; do
    result=`ssh_execute_cmd "$PEER_HOST_USER_NAME@$i" 'echo ok'`
    if [ $? -ne 0 -a "$result" = "ok" ]; then
        write_switch_log "Detected that the peer host($i) is unreachable."
    fi
done

SLAVE_STATUS=`local_db_execute_query "SHOW SLAVE STATUS\G" false`
if [ $? -ne 0 ]; then
    write_switch_log "Execution of the SQL statement SHOW SLAVE STATUS failed."
    exit 1
fi

if [ -z "$SLAVE_STATUS" ]; then
    exit 0
fi

replication_have_error () {
    local sql_running="$1"
    local io_running="$2"
    local result=
    local ip_address="`get_available_ip_address`"
    if [ -n "$ip_address" ]; then
        result=`peer_db_execute_query "$ip_address" "select 'ok';" true`
        if [ $? -ne 0 ]; then
            write_switch_log "Execution of the SQL statement select failed on \
remote($ip_address), error($result)."
            return 1
        fi

        if [ "$sql_running" != "Yes" ] || [ "$io_running" != "Yes" -a "$io_running" != "Connecting" ]; then
            return 0
        fi

    else
        write_switch_log "there is no IP address in ($PEER_HOST_ADDRESS_LIST) that can be successfully connected."
    fi

    return 1
}

SLAVE_SQL_RUNNING=$(echo "$SLAVE_STATUS" | grep "Slave_SQL_Running:" | awk -F': ' '{print $2}')
SLAVE_IO_RUNNING=$(echo "$SLAVE_STATUS" | grep "Slave_IO_Running:" | awk -F': ' '{print $2}')
if [ "$SLAVE_SQL_RUNNING" != "Yes" ] || [ "$SLAVE_IO_RUNNING" != "Yes" ]; then
    write_switch_log "Replication is not running normally, Slave_SQL_Running($SLAVE_SQL_RUNNING), Slave_IO_Running($SLAVE_IO_RUNNING)."

    replication_have_error "$SLAVE_SQL_RUNNING" "$SLAVE_IO_RUNNING"
    if [ $? -eq 0 ]; then
        write_switch_log "An error has been detected in the synchronous \
replication, and the keepalived service will be stopped soon."
        stop_keepalived_service
    fi

    LAST_ERROR=$(echo "$SLAVE_STATUS" | grep "Last_Error:")
    LAST_IO_ERROR=$(echo "$SLAVE_STATUS" | grep "Last_IO_Error:")
    LAST_ERROR_VALUE=$(echo "$LAST_ERROR" | awk -F': ' '{print $2}')
    LAST_IO_ERROR_VALUE=$(echo "$LAST_IO_ERROR" | awk -F': ' '{print $2}')
    if [ -n "$LAST_ERROR_VALUE" ]; then
        write_switch_log "Replication error detected: $LAST_ERROR"
    fi

    if [ -n "$LAST_IO_ERROR_VALUE" ]; then
        write_switch_log "Replication error detected: $LAST_IO_ERROR_VALUE"
    fi
fi

SLAVE_STATUS=`local_db_execute_query "SHOW STATUS LIKE 'Rpl_semi_sync_slave_status';" true`
if [ $? -ne 0 ]; then
    write_switch_log "Execution of the SQL statement SHOW STATUS LIKE 'Rpl_semi_sync_slave_status' failed."
    exit 1
fi

SEMI_STATUS=$(echo "$SLAVE_STATUS" | grep "Rpl_semi_sync_slave_status" | awk '{print $2}')
if [ "$SEMI_STATUS" = "OFF" ]; then
    write_switch_log "Attention! The database service semi-synchronous replication has been downgraded to asynchronous replication."
elif [ "$SEMI_STATUS" = "ON" ]; then
    write_switch_log "OK! database service semi-synchronous replication is still running normally."
else
    write_switch_log "Unknown status($SLAVE_STATUS), cmd(SHOW STATUS LIKE 'Rpl_semi_sync_slave_status')"
fi
