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,12 +102,55 @@ trim() {
echo -n "$var" 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? # - Running in a terminal?
# - # -
@ -119,16 +162,6 @@ else
LOGGING=false LOGGING=false
fi 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 if [[ -z $check_string_ps ]]; then
fatal "$(basename $0): Bind Nameservice seems NOT to be installed" fatal "$(basename $0): Bind Nameservice seems NOT to be installed"
fi fi
@ -159,63 +192,88 @@ 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 $LOGGING ; then #if [[ $? -eq 0 ]] && [[ -n "$ip_addr" ]] ; then
ok "Bind Nameservice is up and running." if [[ $? -eq 0 ]] && is_valid_ipv4 $ip_addr ; then
fi if $LOGGING ; then
exit 0 ok "Bind Nameservice is up and running."
fi fi
if [[ -f "$check_file" ]]; then
rm -f "$check_file" > /dev/null 2>&1
fi
clean_up 0
fi fi
done; 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 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
error "$(cat ${LOCK_DIR}/err_msg.log)" error "$(cat ${LOCK_DIR}/err_msg.log)"
fi fi
sleep 10 sleep 10
$systemctl start $service_name > /dev/null 2> ${LOCK_DIR}/err_msg.log $systemctl start $service_name > /dev/null 2> ${LOCK_DIR}/err_msg.log
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
error "$(cat ${LOCK_DIR}/err_msg.log)" error "$(cat ${LOCK_DIR}/err_msg.log)"
fi fi
else else
$bind_init_script stop > /dev/null 2>&1 $bind_init_script stop > /dev/null 2>&1
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
error "Stopping Bind Nameservice failed!" error "Stopping Bind Nameservice failed!"
fi fi
sleep 10 sleep 10
$bind_init_script start > /dev/null 2>&1 $bind_init_script start > /dev/null 2>&1
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
error "Starting Bind Nameservice failed!" error "Starting Bind Nameservice failed!"
fi fi
fi fi
declare -i counter=0 declare -i counter=0
PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}') PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}')
while [[ "X${PID}" = "X" ]]; do while [[ "X${PID}" = "X" ]]; do
sleep 1 sleep 1
PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}') PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}')
if [[ $counter -gt 10 ]]; then if [[ $counter -gt 10 ]]; then
break break
else else
((counter++)) ((counter++))
fi fi
done 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;