#!/bin/bash

mysqlcon=( mysql -hlocalhost -u${MYSQL_REPL_USER} -p${MYSQL_REPL_PASSWORD} )

if [ ! -f /var/lib/mysql/last_time ];then
    echo "0" > /var/lib/mysql/last_time
fi

while true;do
    if [ "$(${mysqlcon[@]} -e 'SELECT * FROM performance_schema.replication_group_members;' 2> /dev/null |grep $(hostname) | awk '{print $5$6}')" = "ONLINEPRIMARY" ] && [ $(${mysqlcon[@]} -e "show variables like 'read_only'\G" 2> /dev/null|grep Value|awk '{print $2}') = "OFF" ];then
        echo "$(date +%s%3N)" > /var/lib/mysql/last_time 
#    else
#        echo 0 > /var/lib/mysql/last_time
    fi
    sleep 1
done



