Cover V11, I04

Article
Listing 1
Listing 2

apr2002.tar

Listing 1 Put this script in named's administrator's crontab to execute as often as you feel prudent

#!/bin/sh
#
# CONFIGURATION SECTION 
#    
# Set PRIMARYNS to the IP address of your primary nameserver
PRIMARYNS=192.168.60.25
#    
# Set KNOWNHOST to a host you *know* $PRIMARYNS can resolve 
KNOWNHOST=www.sun.com
#    
# If SYSLOG is set, write a message to the named file on failover
SYSLOG=/dev/log
#    
# Set NDC to wherever the "ndc" binary is on your system
NDC=/usr/sbin/ndc
#    
# Set NSLOOKUP to wherever your "nslookup" binary is on your system
NSLOOKUP=/usr/sbin/nslookup
#    
# Set NAMEDB to the directory where you keep all your BIND records 
NAMEDB=/var/named
#    
# Set PRIMARYDB to the filename containing your "normal" DNS records
PRIMARYDB=db.foo.com
#    
# Set BACKUPDB to the filename containing your "fallback" DNS records
BACKUPDB=bak.foo.com
#    
# END CONFIGURATION SECTION 
     
if [ ! -x $NDC ]; then 
   echo "File $NDC does not seem to exist or be executable. Please fix."
   exit 1  
fi   

if [ ! -x $NAMEDB ]; then 
   echo "Unable to access directory $NAMEDB. Please fix."
   exit 1  
fi   

# Wow, this is really hacky. nslookup doesn't always set a return code -
# anyone know a better way?

$RC=`$NSLOOKUP $KNOWNHOST $PRIMARYNS | grep "Name:" | wc -l`
if [ $RC -eq 1 ]; then
   exit 0 # Everything's cool
fi

if [ $RC -eq 0 ]; then
   echo "Nameserver $PRIMARYNS not responding correctly. Failing over."

   cp $NAMEDB/$PRIMARYDB /tmp/$PRIMARYDB
   cp $NAMEDB/$BACKUPDB $NAMEDB/$PRIMARYDB
   cp /tmp/$PRIMARYDB $NAMEDB/$BACKUPDB

   echo "Restarting BIND..."
   $NDC restart

   if [ ! -z $SYSLOG ]; then
      $DATE = `date`
      echo "$DATE : Primary nameserver unreachable. Failing over." \
         > $SYSLOG
   fi
fi