Monitor response time of DNS query using dig

Problem this snippet solves:

This monitor uses dig to verify performance of an expected response from load balanced DNS servers. It accepts 2 command-line arguments: the name to be looked up, and the time it takes the query to respond.

NOTE: Use external monitors only when a built-in monitor won't do the trick. This example is intended to demonstrate the basic use of dig in an external monitor to test UDP-based name services.

How to use this snippet:

  1. Create a new file containing the code below on the LTM filesystem. Recommended location is /config/eav. Permissions on the file must be 700 or better, giving root rwx access to the file. 2. Create a monitor profile of type "External" with the following values:

    • External Program: . . the name of the script file created in step 1
    • Arguments:
    • * " ", where string has x.x.x.x format for A lookup and ::::::: format for AAAA lookups
    • 3. Adjust the interval and timeout as appropriate for your application

Code :

#!/bin/sh
# (c) Copyright 1996-2007 F5 Networks, Inc.
#
# This software is confidential and may contain trade secrets that are the
# property of F5 Networks, Inc.  No part of the software may be disclosed
# to other parties without the express written consent of F5 Networks, Inc.
# It is against the law to copy the software.  No part of the software may
# be reproduced, transmitted, or distributed in any form or by any means,
# electronic or mechanical, including photocopying, recording, or information
# storage and retrieval systems, for any purpose without the express written
# permission of F5 Networks, Inc.  Our services are only available for legal
# users of the program, for instance in the event that we extend our services
# by offering the updating of files via the Internet.
#
#
# HOW TO
# Create External Monitor
# Set External Program to /config/eav/
# Set Arguments to "FQDM QueryTime" where QueryTime is the acceptable time a query can be 
# considered good. (i.e., "www.google.com 30" means that google must return back a query time
# 30 ms or less to be considered UP.
#
#
# these arguments supplied automatically for all external monitors:
# $1 = IP (nnn.nnn.nnn.nnn notation or hostname)
# $2 = port (decimal, host byte order) -- not used in this monitor, assumes default port 53
# $3 = name to be looked up
# $4 = Query Performance number (How long in ms can a query take before it’s marked down)
# Version 1.0


# This is the to remove the IPV6 notation that is added that is passed from the F5.
node_ip=`echo $1 | sed 's/::ffff://'`
# This the query Performance number, which is passed from the F5 into the following variable.
qtime=${4}

pidfile="/var/run/`basename $0`.$node_ip..$2.pid"
if [ -f $pidfile ]
then
   kill -9 `cat $pidfile` > /dev/null 2>&1
fi
echo "$$" > $pidfile

status=`dig @${node_ip} ${3} | grep Query | awk '{ if ($4<="'"$qtime"'") {print "0" }else print"1" }'` > /dev/null 2>&1

if [ $status -eq 0 ]
then
    echo "UP"
fi

rm -f $pidfile

#bottom of file
Published Mar 18, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment