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