diff --git a/check_dns.sh b/check_dns.sh index 79045e0..2f0b568 100755 --- a/check_dns.sh +++ b/check_dns.sh @@ -5,6 +5,8 @@ if [[ -f "/usr/sbin/named" ]] ; then check_string_ps="/usr/sbin/named" fi +check_file="/tmp/dns-check-failed" + alternate_addr="oopen.de google.com heise.de debian.org ubuntu.com" @@ -18,7 +20,7 @@ fi # - service_name=bind9 -LOCK_DIR=`mktemp -d` +LOCK_DIR="/tmp/$(basename $0).$$.LOCK" #--------------------------------------- @@ -27,6 +29,14 @@ LOCK_DIR=`mktemp -d` #----------------------------- #--------------------------------------- +clean_up() { + + # Perform program exit housekeeping + rm -rf "$LOCK_DIR" + blank_line + exit $1 +} + fatal(){ echo "" if $terminal ; then @@ -85,16 +95,6 @@ ok (){ echo "" } -fatal(){ - echo "" - echo -e " [ Fatal ] $*" - echo "" - echo -e "\tScript terminated.." - echo "" - rm -rf $LOCK_DIR - exit 1 -} - trim() { local var="$*" var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters @@ -102,12 +102,55 @@ trim() { echo -n "$var" } +blank_line() { + if $terminal ; then + echo "" + fi +} + + +## - Test of valid IPv4 Address +## - +## - Returns 0 if valid, > 0 otherwise +## - +is_valid_ipv4() { + local -a octets=( ${1//\./ } ) + local RETURNVALUE=0 + + # return an error if the IP doesn't have exactly 4 octets + [[ ${#octets[@]} -ne 4 ]] && return 1 + + for octet in ${octets[@]} + do + if [[ ${octet} =~ ^[0-9]{1,3}$ ]] + then # shift number by 8 bits, anything larger than 255 will be > 0 + ((RETURNVALUE += octet>>8 )) + else # octet wasn't numeric, return error + return 1 + fi + done + return ${RETURNVALUE} +} + + +# ---------- +# - Jobhandling +# ---------- + +# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM +# - +trap clean_up SIGHUP SIGINT SIGTERM + +# - Create lock directory '$LOCK_DIR" +# +mkdir "$LOCK_DIR" + + + +# ---------- +# - Some checks +# ---------- -#--------------------------------------- -#----------------------------- -# Check some prerequisites -#----------------------------- -#--------------------------------------- # - Running in a terminal? # - @@ -119,16 +162,6 @@ else LOGGING=false fi -# - Running in a terminal? -# - -if [[ -t 1 ]] ; then - terminal=true - LOGGING=true -else - terminal=false - LOGGING=false -fi - if [[ -z $check_string_ps ]]; then fatal "$(basename $0): Bind Nameservice seems NOT to be installed" fi @@ -159,63 +192,88 @@ if $LOGGING ; then echo -e " ======================================" fi -for ip_addr in $alternate_addr ; do - ping -c3 $ip_addr >> /dev/null 2>&1 - if [ $? -eq 0 ]; then +for host in $alternate_addr ; do - PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}') - if [[ "X${PID}" = "X" ]]; then - break - else - - if $LOGGING ; then - ok "Bind Nameservice is up and running." - fi - exit 0 + ip_addr="$(dig +short $host 2> /dev/null)" + + #if [[ $? -eq 0 ]] && [[ -n "$ip_addr" ]] ; then + if [[ $? -eq 0 ]] && is_valid_ipv4 $ip_addr ; then + if $LOGGING ; then + ok "Bind Nameservice is up and running." fi + if [[ -f "$check_file" ]]; then + rm -f "$check_file" > /dev/null 2>&1 + fi + clean_up 0 fi + done; -error "Bind Nameservice seems to be down! Trying to restart service now.." +if [[ ! -f "$check_file" ]] ; then + touch /tmp/dns-check-failed + if $LOGGING ; then + warn "There was no correct answer from the 'bind' name service. + + This time i will go on, but if next time the bind service still answers + incorrect, i'll do something." + fi + clean_up 1 +fi + +PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}') +if [[ "X${PID}" = "X" ]]; then + + error "Bind Nameservice seems to be down! Trying to restart service now.." + +else + + error "A \"named\" process is running, but bind service did no answer in a correct way. + + ** Probably you have a network problem ** + + What the heck: I restart the bind9 service anyway - Maybe that will help." +fi if $systemd_supported ; then - $systemctl stop $service_name > /dev/null 2> ${LOCK_DIR}/err_msg.log - if [[ $? -ne 0 ]]; then - error "$(cat ${LOCK_DIR}/err_msg.log)" - fi - sleep 10 - $systemctl start $service_name > /dev/null 2> ${LOCK_DIR}/err_msg.log - if [[ $? -ne 0 ]]; then - error "$(cat ${LOCK_DIR}/err_msg.log)" - fi + $systemctl stop $service_name > /dev/null 2> ${LOCK_DIR}/err_msg.log + if [[ $? -ne 0 ]]; then + error "$(cat ${LOCK_DIR}/err_msg.log)" + fi + sleep 10 + $systemctl start $service_name > /dev/null 2> ${LOCK_DIR}/err_msg.log + if [[ $? -ne 0 ]]; then + error "$(cat ${LOCK_DIR}/err_msg.log)" + fi else - $bind_init_script stop > /dev/null 2>&1 - if [[ $? -ne 0 ]]; then - error "Stopping Bind Nameservice failed!" - fi - sleep 10 - $bind_init_script start > /dev/null 2>&1 - if [[ $? -ne 0 ]]; then - error "Starting Bind Nameservice failed!" - fi + $bind_init_script stop > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + error "Stopping Bind Nameservice failed!" + fi + sleep 10 + $bind_init_script start > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + error "Starting Bind Nameservice failed!" + fi fi declare -i counter=0 PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}') while [[ "X${PID}" = "X" ]]; do - sleep 1 - PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}') - if [[ $counter -gt 10 ]]; then - break - else - ((counter++)) - fi + sleep 1 + PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}') + if [[ $counter -gt 10 ]]; then + break + else + ((counter++)) + fi done if [[ "X${PID}" = "X" ]] ; then - error "Restarting Bind Nameservice failed!" + error "Restarting Bind Nameservice failed!" + clean_up 1 else - ok "Bind Nameservice is up and running." + ok "Bind Nameservice is up and running." fi + -exit 0; +clean_up 0;