Skip to content

Instantly share code, notes, and snippets.

@oleksandriegorov
Forked from thomasdarimont/lb-manager-client.sh
Last active April 24, 2020 20:45
Show Gist options
  • Save oleksandriegorov/bacf9829b9a9352c06914e46fbe26dfc to your computer and use it in GitHub Desktop.
Save oleksandriegorov/bacf9829b9a9352c06914e46fbe26dfc to your computer and use it in GitHub Desktop.
Shell script to configure load- balancing with mod-proxy-balancer
#! /bin/sh
# Set up a default search path
PATH="/usr/bin:/bin"
CURL=`which curl`
if [ -z "$CURL" ]; then
echo "curl not found"
exit 1
fi
target="http://127.0.0.1/balancer-manager"
while getopts "t:" opt; do
case "$opt" in
t)
target=$OPTARG
;;
esac
done
shift $(($OPTIND - 1))
action=$1
list_balancers() {
$CURL -s "${target}" | grep "balancer://" | sed "s/.*balancer:\/\/\(.*\)<\/h3>.*/\1/"
}
list_workers() {
balancer=$1
if [ -z "$balancer" ]; then
echo "Usage: $0 [-s host] [-p port] [-m balancer-manager] list-workers balancer_name"
echo " balancer_name : balancer name"
exit 1
fi
$CURL -s "${target}" | grep "/balancer-manager?b=${balancer}&w" | sed "s/.*href='\(.[^']*\).*/\1/" | sed "s/.*w=\(.*\)&.*/\1/"
}
enable() {
balancer=$1
worker=$2
if [ -z "$balancer" ] || [ -z "$worker" ]; then
echo "Usage: $0 [-s host] [-p port] [-m balancer-manager] enable balancer_name worker_route"
echo " balancer_name : balancer/cluster name"
echo " worker_route : worker route e.g.) ajp://192.1.2.3:8009"
exit 1
fi
nonce=`$CURL -s "${target}" | grep nonce | grep "b=${balancer}" | sed "s/.*nonce=\(.*\)['\"].*/\1/" | tail -n 1`
if [ -z "$nonce" ]; then
echo "balancer_name ($balancer) not found"
exit 1
fi
call_balancer "enable"
sleep 2
status
}
disable() {
balancer=$1
worker=$2
if [ -z "$balancer" ] || [ -z "$worker" ]; then
echo "Usage: $0 [-s host] [-p port] [-m balancer-manager] disable balancer_name worker_route"
echo " balancer_name : balancer/cluster name"
echo " worker_route : worker route e.g.) ajp://192.1.2.3:8009"
exit 1
fi
call_balancer "disable"
sleep 2
status
}
call_balancer() {
action=$1
nonce=`$CURL -s "${target}" | grep nonce | grep "b=${balancer}" | sed "s/.*nonce=\(.*\)['\"].*/\1/" | tail -n 1`
if [ -z "$nonce" ]; then
echo "balancer_name ($balancer) not found"
exit 1
fi
if [ "$action" = "enable" ]
then
w_status_D=0
elif [ "$action" = "disable" ]
then
w_status_D=1
fi
echo "$action $2 of $1..."
HTTPD=$(which httpd)
if [ "${HTTPD}" = "" ]
then
HTTPD=/usr/sbin/httpd
fi
apache_release=$($HTTPD -v | grep -Po '(?<=Apache/).*(?=\s)')
if [ "$apache_release" = "" ]
then
apache_release="2.4.X"
fi
major_release=$(echo $apache_release | cut -d'.' -f1)
minor_release=$(echo $apache_release | cut -d'.' -f2)
if [ $major_release -eq 2 -a $minor_release -le 2 ]
then
# Apache 2.2.x
$CURL -s -o /dev/null "${target}?b=${balancer}&w=${worker}&nonce=${nonce}&dw=Enable&lf=1&ls=0&wr=&rr="
elif [ $major_release -eq 2 -a $minor_release -gt 2 ]
# newer Apache
then
$CURL -s -o /dev/null -XPOST "${target}?" -d b="${balancer}" -d w="${worker}" -d nonce="${nonce}" -d w_status_D=${w_status_D}
else
echo "Apache version : $apache_release"
fi
}
status() {
$CURL -s "${target}" | grep "href" | sed "s/<[^>]*>/ /g"
}
case "$1" in
list-balancer)
list_balancers "${@:2}"
;;
list-worker)
list_workers "${@:2}"
;;
enable)
enable "${@:2}"
;;
disable)
disable "${@:2}"
;;
status)
status "${@:2}"
;;
*)
echo "Usage: $0 {list-balancer|list-worker|enable|disable|status}"
echo ""
echo "Options: "
echo " -t target (e.g. https://app.acme.com/balancer-manager)"
echo ""
echo "Commands: "
echo " list-balancer"
echo " list-worker balancer-name"
echo " enable balancer_name worker_route"
echo " disable balancer_name worker_route"
exit 1
esac
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment