icinga2-checks/check_external_ip_dynamic.sh

53 lines
1002 B
Bash
Executable File

#!/bin/bash
usage() {
echo "Usage: $0 -i <IP checker service> -d <domain to resolve> -r <DNS server to query>"
exit 1
}
IP_CHECKER=""
DOMAIN=""
DNS_SERVER=""
while getopts "i:d:r:" opt; do
case ${opt} in
i )
IP_CHECKER=$OPTARG
;;
d )
DOMAIN=$OPTARG
;;
r )
DNS_SERVER=$OPTARG
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
usage
exit 1
;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
usage
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ -z "$IP_CHECKER" ] || [ -z "$DOMAIN" ] || [ -z "$DNS_SERVER" ]; then
echo "All parameters are required"
usage
exit 1
fi
CURRENT_IP=$(curl -s "$IP_CHECKER")
RESOLVED_IP=$(dig +short @$DNS_SERVER $DOMAIN | head -n 1)
if [ "$CURRENT_IP" == "$RESOLVED_IP" ]; then
echo "OK - $DOMAIN resolves to $CURRENT_IP"
exit 1
else
echo "CRITICAL - $DOMAIN does not resolve to $CURRENT_IP. Resolved: $RESOLVED_IP"
exit 2
fi