Skip to content

Instantly share code, notes, and snippets.

@SeonghoonKim
Last active March 8, 2022 08:12
  • Star 22 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save SeonghoonKim/5385982 to your computer and use it in GitHub Desktop.
Apache HTTPD balancer-manager control script
#! /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
server="localhost"
port="80"
manager="balancer-manager"
while getopts "s:p:m:" opt; do
case "$opt" in
s)
server=$OPTARG
;;
p)
port=$OPTARG
;;
m)
manager=$OPTARG
;;
esac
done
shift $(($OPTIND - 1))
action=$1
list_balancers() {
$CURL -s "http://${server}:${port}/${manager}" | grep "balancer://" | sed "s/.*balancer:\/\/\(.*\)<\/a>.*/\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 "http://${server}:${port}/${manager}" | 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 "http://${server}:${port}/${manager}" | grep nonce | grep "${balancer}" | sed "s/.*nonce=\(.*\)['\"].*/\1/" | tail -n 1`
if [ -z "$nonce" ]; then
echo "balancer_name ($balancer) not found"
exit 1
fi
echo "Enabling $2 of $1..."
# Apache 2.2.x
#$CURL -s -o /dev/null -XPOST "http://${server}:${port}/${manager}?" -d b="${balancer}" -d w="${worker}" -d nonce="${nonce}" -d dw=Enable
$CURL -s -o /dev/null -XPOST "http://${server}:${port}/${manager}?" -d b="${balancer}" -d w="${worker}" -d nonce="${nonce}" -d w_status_D=0
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
echo "Disabling $2 of $1..."
nonce=`$CURL -s "http://${server}:${port}/${manager}" | grep nonce | grep "${balancer}" | sed "s/.*nonce=\(.*\)['\"].*/\1/" | tail -n 1`
if [ -z "$nonce" ]; then
echo "balancer_name ($balancer) not found"
exit 1
fi
# Apache 2.2.x
#$CURL -s -o /dev/null -XPOST "http://${server}:${port}/${manager}?" -d b="${balancer}" -d w="${worker}" -d nonce="${nonce}" -d dw=Disable
$CURL -s -o /dev/null -XPOST "http://${server}:${port}/${manager}?" -d b="${balancer}" -d w="${worker}" -d nonce="${nonce}" -d w_status_D=1
sleep 2
status
}
status() {
$CURL -s "http://${server}:${port}/${manager}" | 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 " -s server"
echo " -p port"
echo " -m balancer-manager-context-path"
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 $?
@rainoko
Copy link

rainoko commented May 26, 2015


---
 apache-balancer.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/apache-balancer.sh b/apache-balancer.sh
index eb6d199..c195161 100644
--- a/apache-balancer.sh
+++ b/apache-balancer.sh
@@ -42,7 +42,7 @@ list_workers() {
     echo "  balancer_name :    balancer name"
     exit 1
   fi
-  $CURL -s "http://${server}:${port}/${manager}" | grep "/balancer-manager?b=${balancer}&w" | sed "s/.*href='\(.[^']*\).*/\1/" | sed "s/.*w=\(.*\)&.*/\1/"
+  $CURL -s "http://${server}:${port}/${manager}" | grep "/${manager}?b=${balancer}&w" | sed "s/.*href='\(.[^']*\).*/\1/" | sed "s/.*w=\(.*\)&.*/\1/"
 }

 enable() {
-- 
2.1.4

@Leobq
Copy link

Leobq commented Jul 25, 2019

curl-7.19.7-37.el6_5.3.x86_64; httpd-2.2.15-69.el6.x86_64

disable and enable script functions, for us, required a different url as it was not a POST but a GET method and, w_status_D was changed in status_D:

disable: $CURL -s -o /dev/null -G "http://${server}:${port}/${manager}?b=${balancer}" -d w="${worker}" -d nonce="${nonce}" -d status_D=1

PS1: enable: "0" instead of "1".
PS2: -X GET is the default
PS3: it hasn't been necessary to specify the whole parameters string used in the form (ls, lf, rr, etc...) to disable or enable the workers.

@andrekloster
Copy link

andrekloster commented Aug 30, 2019

curl version 7.64.0-4; apache2 version 2.4.38-3

POST requests still work for me.
BUT: Since the last apache update you have to specify a referer inside your request! Otherwise it may be that you can't disable the loadbalance member.

add this part: -H "Referer: ${proto}://${vhost}:${port}/${manager}?"

Example (Disable Member):
$CURL -s -o /dev/null -XPOST -H "Referer: ${proto}://${vhost}:${port}/${manager}?" "${proto}://${vhost}:${port}/${manager}?" -d b="${balancer}" -d w="${worker}" -d nonce="${nonce}" -d w_status_D=1

@rdumi7ru
Copy link

Hi,
I also use balancer-manager but after upgrading to httpd2.4.41 the curl is not working anymore, nothing changes on balancer page.
Anyone had this issue?

@unc1
Copy link

unc1 commented Aug 4, 2021

Hi,
I also use balancer-manager but after upgrading to httpd2.4.41 the curl is not working anymore, nothing changes on balancer page.
Anyone had this issue?

Hi,
I am having this issue. I had a working bash script that use to be able to manipulate the balance manager members. But, some upgrade seems to have broken this functionality.

@abenoit-reeliant
Copy link

Hello.
Same issue here, but it works when adding the referer header :
$CURL -s -o /dev/null -XPOST "http://${server}:${port}/${manager}?" -H "Referer: http://${server}:${port}/${manager}?b=${balancer}&w=${worker}&nonce=${nonce}" -d b="${balancer}" -d w="${worker}" -d nonce="${nonce}" -d w_status_D=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment