2023-06-21 17:57:46 -06:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Default values
|
2024-02-25 11:40:00 -07:00
|
|
|
dns_server=""
|
2023-06-21 17:57:46 -06:00
|
|
|
query_domain=""
|
|
|
|
warning_time=200
|
|
|
|
critical_time=250
|
|
|
|
|
|
|
|
# Parse flag arguments
|
|
|
|
while getopts "s:d:w:c:" flag; do
|
|
|
|
case "${flag}" in
|
|
|
|
s) dns_server="${OPTARG}" ;;
|
|
|
|
d) query_domain="${OPTARG}" ;;
|
|
|
|
w) warning_time="${OPTARG}" ;;
|
|
|
|
c) critical_time="${OPTARG}" ;;
|
|
|
|
*)
|
|
|
|
echo "Usage: $0 -s <dns_server> -d <query_domain> -w <warning_time> -c <critical_time>"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2024-02-25 11:40:00 -07:00
|
|
|
if [[ -z $query_domain && ${query_domain+x} ]]; then
|
|
|
|
echo "Error: query domain not provided."
|
2023-06-21 17:57:46 -06:00
|
|
|
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
|
2024-02-25 15:35:14 -07:00
|
|
|
start_time=$(date +%s)
|
2024-02-25 11:40:00 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-02-25 15:35:14 -07:00
|
|
|
end_time=$(date +%s)
|
|
|
|
response_time=$((end_time - start_time))
|
2023-06-21 17:57:46 -06:00
|
|
|
perfdata="response_time=${response_time}ms;${warning_time};${critical_time};0;"
|
|
|
|
|
|
|
|
# Check response time against warning and critical levels
|
2024-02-25 18:59:35 -07:00
|
|
|
if [ -z "$result" ] || echo "$result" | grep -Eq "(no servers could be reached|communications error)"; then
|
2023-06-21 17:57:46 -06:00
|
|
|
echo "CRITICAL - DNS resolution failed for $query_domain on $dns_server | $perfdata"
|
|
|
|
exit 2
|
|
|
|
elif [ "$critical_time" -gt 0 ] && [ "$response_time" -gt "$critical_time" ]; then
|
|
|
|
echo "CRITICAL - DNS resolution took ${response_time}ms for $query_domain on $dns_server | $perfdata"
|
|
|
|
exit 2
|
|
|
|
elif [ "$warning_time" -gt 0 ] && [ "$response_time" -gt "$warning_time" ]; then
|
|
|
|
echo "WARNING - DNS resolution took ${response_time}ms for $query_domain on $dns_server | $perfdata"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "OK - resolved $query_domain to $result using $dns_server in ${response_time}ms | $perfdata"
|
|
|
|
exit 0
|
|
|
|
fi
|