280 lines
5.7 KiB
Bash
Executable File
280 lines
5.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
check_string_ps=""
|
|
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"
|
|
|
|
|
|
# - used, if systemd is NOT supported
|
|
bind_init_script=""
|
|
if [[ -x "/etc/init.d/bind9" ]] ; then
|
|
bind_init_script="/etc/init.d/bind9"
|
|
fi
|
|
|
|
# - Used if systemd is supported
|
|
# -
|
|
service_name=bind9
|
|
|
|
LOCK_DIR="/tmp/$(basename $0).$$.LOCK"
|
|
|
|
|
|
#---------------------------------------
|
|
#-----------------------------
|
|
# Base Function(s)
|
|
#-----------------------------
|
|
#---------------------------------------
|
|
|
|
clean_up() {
|
|
|
|
# Perform program exit housekeeping
|
|
rm -rf "$LOCK_DIR"
|
|
blank_line
|
|
exit $1
|
|
}
|
|
|
|
fatal(){
|
|
echo ""
|
|
if $terminal ; then
|
|
echo -e " [ \033[31m\033[1mFatal\033[m ] $*"
|
|
else
|
|
echo -e " [ Fatal ] $*"
|
|
fi
|
|
echo ""
|
|
if $terminal ; then
|
|
echo -e " \033[1mScript terminated\033[m.."
|
|
else
|
|
echo -e " Script terminated.."
|
|
fi
|
|
echo ""
|
|
rm -rf $LOCK_DIR
|
|
exit 1
|
|
}
|
|
|
|
error (){
|
|
echo ""
|
|
if $terminal ; then
|
|
echo -e " [ \033[31m\033[1mError\033[m ] $*"
|
|
else
|
|
echo " [ Error ] $*"
|
|
fi
|
|
echo ""
|
|
}
|
|
|
|
warn (){
|
|
echo ""
|
|
if $terminal ; then
|
|
echo -e " [ \033[33m\033[1mWarn\033[m ] $*"
|
|
else
|
|
echo " [ Warn ] $*"
|
|
fi
|
|
echo ""
|
|
}
|
|
|
|
info (){
|
|
echo ""
|
|
if $terminal ; then
|
|
echo -e " [ \033[32m\033[1mInfo\033[m ] $*"
|
|
else
|
|
echo " [ Info ] $*"
|
|
fi
|
|
echo ""
|
|
}
|
|
|
|
ok (){
|
|
echo ""
|
|
if $terminal ; then
|
|
echo -e " [ \033[32m\033[1mOk\033[m ] $*"
|
|
else
|
|
echo " [ Ok ] $*"
|
|
fi
|
|
echo ""
|
|
}
|
|
|
|
trim() {
|
|
local var="$*"
|
|
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
|
|
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
|
|
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
|
|
# ----------
|
|
|
|
|
|
# - 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
|
|
|
|
# - Systemd supported ?
|
|
# -
|
|
systemd=$(which systemd)
|
|
systemctl=$(which systemctl)
|
|
|
|
systemd_supported=false
|
|
if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then
|
|
systemd_supported=true
|
|
else
|
|
if [[ ! -x $bind_init_script ]]; then
|
|
fatal "$(basename $0): Missing Bind Init-Script!"
|
|
fi
|
|
fi
|
|
|
|
|
|
#---------------------------------------
|
|
#-----------------------------
|
|
# Check if Bind Nameservice is running
|
|
#-----------------------------
|
|
#---------------------------------------
|
|
|
|
if $LOGGING ; then
|
|
echo -e "\n Check if Bind Nameservice is running.."
|
|
echo -e " ======================================"
|
|
fi
|
|
|
|
for host in $alternate_addr ; do
|
|
|
|
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;
|
|
|
|
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
|
|
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
|
|
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
|
|
done
|
|
|
|
if [[ "X${PID}" = "X" ]] ; then
|
|
error "Restarting Bind Nameservice failed!"
|
|
clean_up 1
|
|
else
|
|
ok "Bind Nameservice is up and running."
|
|
fi
|
|
|
|
|
|
clean_up 0;
|