check_dns.sh: redisign of the whole script.

This commit is contained in:
Christoph 2020-12-27 14:11:47 +01:00
parent dbe86ff8e4
commit bfc18b4131

View File

@ -5,6 +5,8 @@ if [[ -f "/usr/sbin/named" ]] ; then
check_string_ps="/usr/sbin/named" check_string_ps="/usr/sbin/named"
fi fi
check_file="/tmp/dns-check-failed"
alternate_addr="oopen.de google.com heise.de debian.org ubuntu.com" alternate_addr="oopen.de google.com heise.de debian.org ubuntu.com"
@ -18,7 +20,7 @@ fi
# - # -
service_name=bind9 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(){ fatal(){
echo "" echo ""
if $terminal ; then if $terminal ; then
@ -85,16 +95,6 @@ ok (){
echo "" echo ""
} }
fatal(){
echo ""
echo -e " [ Fatal ] $*"
echo ""
echo -e "\tScript terminated.."
echo ""
rm -rf $LOCK_DIR
exit 1
}
trim() { trim() {
local var="$*" local var="$*"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
@ -102,22 +102,55 @@ trim() {
echo -n "$var" echo -n "$var"
} }
blank_line() {
#--------------------------------------- if $terminal ; then
#----------------------------- echo ""
# Check some prerequisites
#-----------------------------
#---------------------------------------
# - Running in a terminal?
# -
if [[ -t 1 ]] ; then
terminal=true
LOGGING=true
else
terminal=false
LOGGING=false
fi 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
# ----------
# - Running in a terminal? # - Running in a terminal?
# - # -
@ -159,25 +192,48 @@ if $LOGGING ; then
echo -e " ======================================" echo -e " ======================================"
fi fi
for ip_addr in $alternate_addr ; do for host in $alternate_addr ; do
ping -c3 $ip_addr >> /dev/null 2>&1
if [ $? -eq 0 ]; then
PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}') ip_addr="$(dig +short $host 2> /dev/null)"
if [[ "X${PID}" = "X" ]]; then
break
else
#if [[ $? -eq 0 ]] && [[ -n "$ip_addr" ]] ; then
if [[ $? -eq 0 ]] && is_valid_ipv4 $ip_addr ; then
if $LOGGING ; then if $LOGGING ; then
ok "Bind Nameservice is up and running." ok "Bind Nameservice is up and running."
fi fi
exit 0 if [[ -f "$check_file" ]]; then
rm -f "$check_file" > /dev/null 2>&1
fi fi
clean_up 0
fi fi
done; done;
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.." 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 if $systemd_supported ; then
$systemctl stop $service_name > /dev/null 2> ${LOCK_DIR}/err_msg.log $systemctl stop $service_name > /dev/null 2> ${LOCK_DIR}/err_msg.log
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
@ -214,8 +270,10 @@ done
if [[ "X${PID}" = "X" ]] ; then if [[ "X${PID}" = "X" ]] ; then
error "Restarting Bind Nameservice failed!" error "Restarting Bind Nameservice failed!"
clean_up 1
else else
ok "Bind Nameservice is up and running." ok "Bind Nameservice is up and running."
fi fi
exit 0;
clean_up 0;