For a project I'm working on, we were having intermittent DNS issues. To try and figure out what's going on, I wanted to be able to monitor and track DNS response times, as close as I can get.
I started out with this article - http://www.zabbix.com/wiki/howto/monitor/services/monitor_dns_and_ntp_services_on_your_network which gave me the details on how to go about monitoring DNS, however their script would only tell me a yes or no if the query worked. I wanted times.
I modified the script to use dig, which gives a query time - however, by default, the dig query uses the local resolver, and if that has the domain cached, the next requests will be 0 ms. That we can't have. Using dig, we can add the +trace to the command which will manually go and do all the required polls to get the final answer. This means, we can track how long the query took from start to finish. But, of course, dig gives you query times for each request, but not the total time. Enter the command time - which lets us know how long a command took. Wrapping it all together gives the following script. Combine with the above link, and some tweaks from http://www.zabbix.com/documentation/1.8/manual/config/user_parameters and you have a pretty handy script.
#!/bin/sh
if test -z "$1" ; then
echo "You need to supply a DNS server to check. Quitting"
exit 0;
fi
DOMAIN=$1
MYTIME=$((time dig $DOMAIN +trace) 2>&1| grep real | cut -dm -f 2 | sed -e 's/[s.]//g')
if [ $? -eq 0 ]; then
echo $MYTIME
else
echo 0
fi