Skip to content

Instantly share code, notes, and snippets.

@dotmanila
Last active December 16, 2023 06:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dotmanila/1a78ef67da86473c70c7c55d3f6fda89 to your computer and use it in GitHub Desktop.
Save dotmanila/1a78ef67da86473c70c7c55d3f6fda89 to your computer and use it in GitHub Desktop.
Orchestrator STONITH via ProxySQL
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
export ORCHESTRATOR_API="$1"
OC=orchestrator-client
PSQL='mysql --defaults-file=/etc/proxysql/user.cnf -P6032 -h127.1'
VER=$(date +%s)
$OC -c raft-leader > /dev/null 2>&1
HASQUORUM=$?
if [ "x${HASQUORUM}" != "x0" ]; then
echo "Orchestrator quorum is lost!"
sessions=$($PSQL -BNe "select SessionID from stats_mysql_processlist" | xargs)
echo "Terminating existing connections $sessions"
if [ ! -z "$sessions" -a "$sessions" != " " ]; then
for session in $sessions; do
echo "KILL CONNECTION $session"
$PSQL -e "KILL CONNECTION $session";
done
fi
echo "Setting ALL to OFFLINE_SOFT"
$PSQL -BNe "UPDATE mysql_servers SET status = 'OFFLINE_SOFT', comment = 'proxysql-oc-helper-${VER}'"
$PSQL -BNe "LOAD MYSQL SERVERS TO RUNTIME; SAVE MYSQL SERVERS TO DISK;"
exit 0
fi
for h in $($OC -c downtimed|cut -d':' -f1); do
echo "Setting $h to OFFLINE_SOFT"
$PSQL -BNe "UPDATE mysql_servers SET status = 'OFFLINE_SOFT', comment = 'proxysql-oc-helper-${VER}' WHERE hostname = '$h'"
_status=$($PSQL -BNe "SELECT DISTINCT status FROM runtime_mysql_servers WHERE hostname = '$h'")
if [ "x${_status}" != 'xOFFLINE_SOFT' -a "x${_status}" != 'x' ]; then
$PSQL -BNe "LOAD MYSQL SERVERS TO RUNTIME; SAVE MYSQL SERVERS TO DISK;"
fi
done
_sql="SELECT hostname FROM mysql_servers WHERE status = 'OFFLINE_SOFT' AND comment LIKE 'proxysql-oc-helper-%' AND comment <> 'proxysql-oc-helper-${VER}'"
for h in $($PSQL -BNe "${_sql}"); do
echo "Setting $h back to ONLINE"
$PSQL -BNe "UPDATE mysql_servers SET status = 'ONLINE' WHERE hostname = '$h'"
$PSQL -BNe "LOAD MYSQL SERVERS TO RUNTIME; SAVE MYSQL SERVERS TO DISK;"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment