check_dns.sh: redisign of the whole script.
This commit is contained in:
parent
dbe86ff8e4
commit
bfc18b4131
126
check_dns.sh
126
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,22 +102,55 @@ trim() {
|
||||
echo -n "$var"
|
||||
}
|
||||
|
||||
blank_line() {
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
fi
|
||||
}
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Check some prerequisites
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
# - Running in a terminal?
|
||||
## - 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
|
||||
# -
|
||||
if [[ -t 1 ]] ; then
|
||||
terminal=true
|
||||
LOGGING=true
|
||||
else
|
||||
terminal=false
|
||||
LOGGING=false
|
||||
fi
|
||||
trap clean_up SIGHUP SIGINT SIGTERM
|
||||
|
||||
# - Create lock directory '$LOCK_DIR"
|
||||
#
|
||||
mkdir "$LOCK_DIR"
|
||||
|
||||
|
||||
|
||||
# ----------
|
||||
# - Some checks
|
||||
# ----------
|
||||
|
||||
|
||||
# - Running in a terminal?
|
||||
# -
|
||||
@ -159,24 +192,47 @@ 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
|
||||
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
|
||||
exit 0
|
||||
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
|
||||
@ -214,8 +270,10 @@ done
|
||||
|
||||
if [[ "X${PID}" = "X" ]] ; then
|
||||
error "Restarting Bind Nameservice failed!"
|
||||
clean_up 1
|
||||
else
|
||||
ok "Bind Nameservice is up and running."
|
||||
fi
|
||||
|
||||
exit 0;
|
||||
|
||||
clean_up 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user