#!/bin/sh
# an external DNS check for BIND nameservers that attempts to return
# fewer false positives than the internal version.  it will query a list
# of specified domains and only return a red status if none of the
# domains successfully resolve.  it also returns dig's output for a
# historical record of what went wrong.
#
# 06/30/2003 - Original concept.
#                          Mike Hoskins <mike at adept dot org>

# 09/10/2003 -  Changed
#               Now this script grep for a keyword in the bb-hosts
#               file and run this check for each server.
#                          Bruno Willener <willy@djus.ch>
#                          Martin Zollet  <tinel@zollet.net>

# scripts in BBHOME/ext will only be ran if they are listed in
# BBHOME/etc/bb-bbexttab for security.

# Functions
## SUBS

# pass in domain to lookup ($1 == DOMAIN)
dns_lookup() {
        if [ "$DEBUG" ]
        then
                # debug
                echo "<<< DNS_LOOKUP() >>>"
                echo "$DIG $1"
        else
                DIGOUT=`$DIG @$SERVER $1`
                DIGSTATUS=`echo "$DIGOUT"|grep "status:"|$TAIL -1|$CUT -d, -f2|$CUT -d: -f2|$TR -d " "`
        fi
}

# pass in color to report to BBDISP ($1 == COLOR)
send_status() {
        # fun with sed, usually set by BB
        #MACHINE=`hostname|sed s/\\\./,/g`
        if [ "$DEBUG" ]
        then
                # debug
                echo "<<< SEND_STATUS() >>>"
                echo "$BB $BBDISP \"status $HOST.$TEST $1 `date` - $STATUS $LINE\""
        else
                $BB $BBDISP "status $HOST.$TEST $1 `date` - $STATUS"
        fi
                # if the status is green, then we don't test the other
                # domains
                STATUS_FLAG=0
}

# grep for the keyword in bb-hosts
$GREP dns $BBHOME/etc/bb-hosts | grep -v "^#" |
while read line
do

        # status flag for testing the domains
        # if the status of the first domain = green
        # then this script will not test the other
        # domains
        STATUS_FLAG=1

        set $line                       # GET ALL THE LINE ARGS
        HOST=`echo $2 | $SED "s/\./,/g"`


        ## SET THESE...

        # if DEBUG is defined, the script will echo vs. execing any
        # commands - useful for debugging.
        #DEBUG="YES"
        # path to tr - this needs to be set, BB as/of 1.9 doesn't include
        # this in bbdef and friends.  you could move this to bbsys.local.
        TR="/usr/bin/tr"
        # path to wc - we use this one instead of the one in bbsys.sh
        # because we don't want the -l argument (BB default)
        WC="/usr/bin/wc"
        # path to cut
        CUT="/bin/cut"
        # DNS server to query - machines can have many interfaces with
        # a nameserver listening on any or all of them...  set this to
        # the hostname that resolves to the address/interface associated
        # with the nameserver process you want to test.
        SERVER="$2"
        # Query LIST - a whitespace delimited list of domains to query.  if
        # we get a successful query, return green to BBDISPLAY.  if we get a
        # failure, don't turn red unless each listed query fails. (try to avoid
        # false positives and/or problems with one site/domain.)
        QLIST="test.com foo.org"
        # Query STATUS - DNS query status that indicates "green" to BB.  defined
        # here because you could set this to NXDOMAIN to test "domains that
        # should not resolve."
        QSTATUS="NOERROR"
        # you can change FAILCOLOR to "yellow" if you do not want the test
        # to result in pages
        OKCOLOR="green"
        FAILCOLOR="red"

        # useful for finding problems
        BBPROG="bb-dns.sh"; export BBPROG
        # this is the test name the script will use when reporting to the
        # BBDISPLAY.  by default, we use the same name as the internal
        # test - you don't have to of course.
        TEST="dns"

        # BBHOME should be set in the environment
        if [ ! "$BBHOME" ]
        then
                echo "BBHOME is not set... exiting."
                exit 1
        fi
        # include standard definitions if needed
        if [ ! "$BBTMP" ]
        then
                . $BBHOME/etc/bbdef.sh
        fi

        ## MAIN

        # Query COUNT - number of domains to query (we don't turn red unless
        # they all fail.)
        QCOUNT=`echo "$QLIST"|$WC|$AWK '{print $2;}'`
        if [ "$QCOUNT" ]
        then
                for DOMAIN in $QLIST
                do
                        # if the flag = 0 then 1 test was green
                        if [ $STATUS_FLAG = 1 ]
                        then

                                # do the lookup
                                dns_lookup $DOMAIN
                                # always green when DEBUG is defined
                                if [ "$DEBUG" -o "$DIGSTATUS" = "$QSTATUS" ]
                                then
                                        # no error, return OKCOLOR and exit
                                        STATUS="DNS Server OK, checked Domain $DOMAIN"
                                        LINE="$DIGOUT"
                                        send_status $OKCOLOR
                                else
                                        # if this is the last domain and we
                                        # haven't succeeded, turn FAILCOLOR
                                        if [ "$QCOUNT" -eq 1 ]
                                        then
                                                STATUS="DNS Server not OK, checked domains $QLIST"
                                                LINE="$DIGOUT"
                                                send_status $FAILCOLOR
                                        fi
                                        QCOUNT=`$EXPR $QCOUNT - 1`
                                fi
                        fi
                done
        fi
done

# now we exit this script :)
exit 0
