Last active
February 20, 2025 16:32
-
-
Save colinmollenhour/88c8d5cb99e524c1b0e00fb9899fb4bc to your computer and use it in GitHub Desktop.
DNS resolver test - reports when DNS resolution is greater than 1 second
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
echo "Using docker container: $3" | |
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 | |
if [[ "$resolver_ip" = "-" ]]; then | |
result=$($cmd "$record" A 2>&1) | |
else | |
result=$($cmd "$record" A @"$resolver_ip" 2>&1) | |
fi | |
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