In one of our projects we had a problem with a host taking to long to respond. During our investigation the problem appeared to be in the DNS name resolving. The DNS server took to long to respond to the query. This happened not every time, but occasionally. To pinpoint the problem we made the following bash script to test the response times of the DNS server. The script is called from the Cron every hour, the response times are recorded in a CSV file.

configfile=~/domainlist
logfile=~/log_result

#First check internet connection, when no connection exit.
ping -c 1 -t 100 www.yahoo.com 1>/dev/null 2>/dev/null 
  rc=`echo $?`
#  echo $rc
  if [[ $rc == 1 ]]
  then 
    exit
  else    

#If the logfile is not present, write a first line with domains
if [[ ! -f $logfile ]]
    then
    echo -n "date/time " >>$logfile
    for dm in `cat $configfile`
      do
      echo -n ", $dm"    >>$logfile
      done
    echo "; " >>$logfile
fi

# Write the date to the logfile.
ra=`date`
echo -n "$ra ">>$logfile
# Measure the DNS time for each domain name and write to the 
# logfile 
for dm in `cat $configfile`
do
   ra=`dig $dm |grep Query|awk '{print $4}'`
   echo -n ", $ra">>$logfile
done
echo "; ">>$logfile
fi

The domainlist file shall look like this:

www.google.com
www.aol.com
www.bluedelta.nl

The cron tab shall contain a line like this: To edit the crontab enter crontab -e

47 * * * *  ~/nstime>>/dev/null

The script is named nstime. Make sure the script has execution rights.

chmod +x nstime

After some days we found the problem was with the name-server, it was not reachable occasionally. To solve the problem we installed a caching name-server at the PC which initiated the queries and set the TTL value in the DNS record to 84400 (24 hrs) The caching name server stores the IP address which is connected to the host. When the name server is unreachable, the caching name-server will resolve the host.