From f6ad41298e8eab30751e398e9660086bab43801a Mon Sep 17 00:00:00 2001 From: Cyberes Date: Sun, 25 Feb 2024 11:40:00 -0700 Subject: [PATCH] check_dns: use system default DNS server if none was provided --- check_dns.sh | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/check_dns.sh b/check_dns.sh index cf108b7..848c543 100755 --- a/check_dns.sh +++ b/check_dns.sh @@ -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 -d -w -c " 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;"