Skip to content

Instantly share code, notes, and snippets.

@cobusc
Created March 3, 2015 04:58
Show Gist options
  • Save cobusc/6a88d0050b098b799f29 to your computer and use it in GitHub Desktop.
Save cobusc/6a88d0050b098b799f29 to your computer and use it in GitHub Desktop.
Helper script to rotate varnish backends
#!/bin/bash
# This script is can be used to enable/disable varnish backends associated
# with a specified server. The state of the varnish backends are displayed
# after the changes have been applied.
#
# At the time of writing the varnish backend names are constructed as a
# concatenation of the servername (with hyphens replaced by underscores) and
# the port on which it listens.
#
# The specified server name is transformed by replacing hyphens with underscores
# after which a simple prefix match is performed to find match backend names.
#
# This implies that one can also pass a specific varnish backend name or
# arbitrary prefix instead of a server name.
#
# Example output of "varnishadm backend.list"
#
# Backend name Refs Admin Probe
# server018000(10.10.10.14,,8000) 1 probe Healthy 5/5
# server018001(10.10.10.14,,8001) 1 probe Healthy 5/5
# server018002(10.10.10.14,,8002) 1 probe Healthy 5/5
# server018003(10.10.10.14,,8003) 1 probe Healthy 5/5
# server018004(10.10.10.14,,8004) 1 probe Healthy 5/5
# server018005(10.10.10.14,,8005) 1 probe Healthy 5/5
# server018006(10.10.10.14,,8006) 1 probe Healthy 5/5
# server018007(10.10.10.14,,8007) 1 probe Healthy 5/5
# server018008(10.10.10.14,,8008) 1 probe Healthy 5/5
# server018009(10.10.10.14,,8009) 1 probe Healthy 5/5
#
BACKEND_SERVER=${1:?"Specify the backend server name or prefix"}
BACKEND_ACTION=${2:?"Specify the action: enable or disable"}
BACKEND_PREFIX=${BACKEND_SERVER/-/_} # Replace hyphens with underscores
VARNISH_ADMIN="varnishadm"
case ${BACKEND_ACTION} in
"enable" ) OP="auto";;
"disable" ) OP="sick";;
*) printf "Unexpected action '${BACKEND_ACTION}'. Use 'enable' or 'disable'.\n"
exit 1;;
esac
printf "Prefixes to %s: '%s'\n" ${BACKEND_ACTION} ${BACKEND_PREFIX}
${VARNISH_ADMIN} backend.list | grep "^${BACKEND_PREFIX}" | cut -d '(' -f 1 |
while read BACKEND_NAME; do
printf "%s %s..." ${BACKEND_ACTION} ${BACKEND_NAME}
${VARNISH_ADMIN} backend.set_health ${BACKEND_NAME} ${OP}
done
printf "*** CURRENT VARNISH BACKENDS ***\n"
${VARNISH_ADMIN} backend.list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment