2011/03/25

Scripting the mod_proxy_balancer Balancer Manager from Bash

Here's a little script I wrote for https://www.digitalpigeon.com that allows me to interact with the Balancer Manager via a Bash script. It basically allows you to enable/disable a worker from the shell.

e.g. ./balancer.sh enable http://web-01:8080

#!/bin/bash
set -e

print_usage() 
{
 echo "Usage: $0 enable|disable http://worker-url"
}

WORKER_URL=$2
ACTION=$1

if [ "${WORKER_URL}" == "" ]; then
 print_usage
 exit 1
fi
if [ "${ACTION}" == "" ]; then
 print_usage
 exit 1
fi

BALANCER_MANAGER_URL=http://load-balancer-host/balancer-manager
BALANCER_NAME=load-balancer
BALANCER_NONSE=`curl -s ${BALANCER_MANAGER_URL}  | sed -n "/href=/s/.*href=\([^>]*\).*/\1/p" | tail -1 | sed -n "s/.*nonce=\(.*\)\"/\1/p"`

if [ "${BALANCER_NONSE}" == "" ]; then
 echo "Could not extract nonce from ${BALANCER_MANAGER_URL}"
        exit 1
fi

curl -s -o /dev/null "${BALANCER_MANAGER_URL}?b=${BALANCER_NAME}&w=${WORKER_URL}&dw=${ACTION}&nonce=${BALANCER_NONSE}"