Last active
June 9, 2022 00:27
-
-
Save ZsBT/4d6f77ffa255e47e89bbf6cc2874e658 to your computer and use it in GitHub Desktop.
socket control script for HAproxy backends
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# control script for HAproxy backends. | |
# | |
# rare usage does not allow me to remember socket commands... | |
# @author ZsBT@GitHub | |
# | |
_socmd(){ echo "$*" | socat stdio $(egrep -o '[^ ]+\.sock' /etc/haproxy/haproxy.cfg); } | |
[ -z "$(which socat)" ] && { echo "error: socat not installed";exit 1; } | |
case $1 in | |
list) | |
opstates[0]=DOWN | |
opstates[1]=STARTING | |
opstates[2]=UP | |
opstates[3]=STOPPING | |
renum='^[0-9]+$' | |
admflag[2]="iMAINT" | |
admflag[4]="cMAINT" | |
admflag[8]="fDRAIN" | |
admflag[16]="iDRAIN" | |
admflag[32]="rMAINT" | |
admflag[64]="hMAINT" | |
_socmd "show servers state" | while read be_id be_name srv_id srv_name srv_addr srv_op_state srv_admin_state foo;do | |
if [[ "$srv_op_state" =~ $renum ]];then | |
opstate=${opstates[$srv_op_state]} | |
echo -n "$be_name/$srv_name : $opstate" | |
for mask in 2 4 8 16 32 64;do | |
[ $mask -eq $(($srv_admin_state & $mask)) ] && echo -n " ${admflag[$mask]}" | |
done | |
echo | |
fi | |
done | |
;; | |
maint|drain|ready) | |
_socmd "set server $2 state $1" | |
;; | |
*) | |
echo -e "\nUsage:" | |
echo -e "\t$(basename $0) list" | |
echo -e "\t$(basename $0) <maint|drain|ready> <backend/server>" | |
echo | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment