Skip to content

Instantly share code, notes, and snippets.

@oogali
Created January 13, 2011 23:32
Show Gist options
  • Save oogali/778843 to your computer and use it in GitHub Desktop.
Save oogali/778843 to your computer and use it in GitHub Desktop.
Spin BIRD up or down depending on the state of DNS
#!/bin/sh
## should we anycast?
## - WHAT? a brief script that will spin bird up or down, based on
## results from a dns test.
##
## - WHY? because if our dns server falls down, we don't want a dns
## blackhole for our querying users
##
## omachonu ogali // oogali@gmail.com // @oogali
## define our constants
## - RR is the resource record we wish to lookup
## - NAMESERVER is the server we want to test
## - BIRDCTL is the path to the bird initscript
## - DIG_ARGS are optional arguments to pass to dig
RR=localhost
NAMESERVER=127.0.0.1
BIRDCTL=/etc/init.d/bird
DIG_ARGS="+short"
## make sure we have pgrep and dig installed
PGREP=`which pgrep 2>/dev/null`
if [ -z "${PGREP}" ]; then
echo "$0: could not find pgrep!"
exit 1
fi
DIG=`which dig 2>/dev/null`
if [ -z "${DIG}" ]; then
echo "$0: could not find dig!"
exit 1
fi
## get pid of bird instance that's running
bird_pid=`${PGREP} bird`
## do a dns lookup for our defined resource record
${DIG} ${DIG_ARGS} ${RR} @${NAMESERVER} >/dev/null 2>&1
if [ $? -eq 0 ]; then
## if dns lookup succeeds, but bird isn't running, start it.
if [ -z "${bird_pid}" ]; then
${BIRDCTL} start
fi
else
## if dns lookup fails, but bird IS running, stop it.
if [ ! -z "${bird_pid}" ]; then
${BIRDCTL} stop
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment