check_dns: use system default DNS server if none was provided

This commit is contained in:
Cyberes 2024-02-25 11:40:00 -07:00
parent 3fd8751595
commit f6ad41298e
1 changed files with 12 additions and 5 deletions

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# Default values # Default values
dns_server="1.1.1.1" dns_server=""
query_domain="" query_domain=""
warning_time=200 warning_time=200
critical_time=250 critical_time=250
@ -20,16 +20,23 @@ while getopts "s:d:w:c:" flag; do
esac esac
done done
# Check if DNS server is provided if [[ -z $query_domain && ${query_domain+x} ]]; then
if [ -z "$dns_server" ]; then echo "Error: query domain not provided."
echo "Error: DNS server not provided"
echo "Usage: $0 -s <dns_server> -d <query_domain> -w <warning_time> -c <critical_time>" echo "Usage: $0 -s <dns_server> -d <query_domain> -w <warning_time> -c <critical_time>"
exit 1 exit 1
fi fi
# Perform DNS resolution check and measure the time # Perform DNS resolution check and measure the time
start_time=$(date +%s%N) start_time=$(date +%s%N)
result=$(dig @$dns_server $query_domain +short)
if [[ -z "$dns_server" ]]; then
# The DNS server was not provided.
result=$(dig "$query_domain" +short)
dns_server="the default server"
else
result=$(dig @$dns_server "$query_domain" +short)
fi
end_time=$(date +%s%N) end_time=$(date +%s%N)
response_time=$(((end_time - start_time) / 1000000)) response_time=$(((end_time - start_time) / 1000000))
perfdata="response_time=${response_time}ms;${warning_time};${critical_time};0;" perfdata="response_time=${response_time}ms;${warning_time};${critical_time};0;"