Skip to content

Instantly share code, notes, and snippets.

@colinmollenhour
Last active April 2, 2024 16:35
Show Gist options
  • Save colinmollenhour/88c8d5cb99e524c1b0e00fb9899fb4bc to your computer and use it in GitHub Desktop.
Save colinmollenhour/88c8d5cb99e524c1b0e00fb9899fb4bc to your computer and use it in GitHub Desktop.
DNS resolver test - reports when DNS resolution is greater than 1 second
#!/bin/bash
if [[ -z $1 ]] || [[ $1 = "--help" ]]; then
echo "Usage: $0 <resolver_ip> [<fqdn> [<docker_container>]]"
exit 1
fi
resolver_ip=$1
record=shops.myshopify.com
if [[ -n $2 ]]; then
record="$2"
fi
cmd=dig
if [[ -n $3 ]]; then
cmd="docker exec $3 dig"
fi
count=0
errors=0
total_time=0
total_runs=0
echo "Starting test resolving $record with $resolver_ip at $(date)"
# Trap interrupt signal (Ctrl+C) to print summary
trap 'stop' INT
function stop() {
end_time=$(date +%s.%N)
total_time=$(echo "$end_time - $first_time" | bc)
echo -e "\nSlow runs: $count, errors: $errors, total runs: $total_runs in $total_time seconds"
exit 0
}
first_time=$(date +%s.%N)
while true; do
start_time=$(date +%s.%N)
# Run the dig command with the provided resolver IP address
result=$($cmd "$record" A @"$resolver_ip" 2>&1)
return_code=$?
((total_runs++))
end_time=$(date +%s.%N)
elapsed_time=$(echo "$end_time - $start_time" | bc)
# Check if the command took longer than 1 second
if (( $(echo "$elapsed_time > 1.0" | bc -l) )); then
echo "$(date '+%Y-%m-%d %H:%M:%S') - Command took $elapsed_time seconds"
((count++))
fi
# Print the result if there was an issue (non-zero return code)
if [ $return_code -ne 0 ]; then
echo "Error ($return_code): $result"
((errors++))
fi
# Sleep for a moment before the next iteration
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment