#!/bin/bash

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


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
   # mysql group replication is OK, return http 200
    /bin/echo -e "HTTP/1.1 200 OK\r\n"
else
   # mysql group replication is down, return http 503
    /bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n"
fi
