monitoring/check_dhcp.sh

264 lines
6.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# - Which Service should be checkked
# -
# - This parameter MUST be given
# -
_service_name="DHCP"
# - The binary of the service
# -
# - If not given, script dries to determin it.
# -
_check_binary="$(which dhcpd)"
# - The systemd service file
# -
# - If not given, script dries to determin it.
#
#_SYSTEMD_SERVICE="isc-dhcp-server"
# - The sysv init script of the service
# -
# - If not given, script dries to determin it.
#
_SYSV_INIT_SCRIPT="isc-dhcp-server"
#---------------------------------------
#-----------------------------
# Base Function(s)
#-----------------------------
#---------------------------------------
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"
}
#---------------------------------------
#-----------------------------
# Check some prerequisites
#-----------------------------
#---------------------------------------
LOCK_DIR=`mktemp -d`
# - Running in a terminal?
# -
if [[ -t 1 ]] ; then
terminal=true
LOGGING=true
else
terminal=false
LOGGING=false
fi
if [[ -n "$_service_name" ]]; then
service_name=$_service_name
else
fatal "$(basename $0): No Service given"
fi
# - Is Service installed ?
# -
if [[ -z "$_check_binary" ]]; then
_check_binary="$(which ${service_name,,}d)"
if [[ -z "$_check_binary" ]] ; then
_check_binary="$(which ${service_name,,})"
fi
fi
if [[ -z "$_check_binary" ]]; then
fatal "$(basename $0): $service_name Service seems NOT to be installed"
else
check_binary="$_check_binary"
check_string_ps="$check_binary"
fi
# - Systemd supported ?
# -
systemd=$(which systemd)
systemctl=$(which systemctl)
systemd_supported=false
if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then
systemd_supported=true
fi
SYSTEMD_SERVICE=
SYSV_INIT_SCRIPT=
if $systemd_supported ; then
if [[ -n "$_SYSTEMD_SERVICE" ]] ; then
if systemctl -t service list-unit-files \
| grep -e "^$_SYSTEMD_SERVICE" \
| grep -q -E "(enabled|disabled)" 2> /devnull ; then
SYSTEMD_SERVICE="$(systemctl -t service list-unit-files | grep -e "^$_SYSTEMD_SERVICE" | awk '{print$1}' | head -1)"
fi
else
if systemctl -t service list-unit-files \
| grep -e "^${service_name,,}d" \
| grep -q -E "(enabled|disabled)" 2> /devnull ; then
SYSTEMD_SERVICE="$(systemctl -t service list-unit-files | grep -e "^${service_name,,}d" | awk '{print$1}' | head -1)"
elif systemctl -t service list-unit-files \
| grep -e "^${service_name,,}" \
| grep -q -E "(enabled|disabled)" 2> /devnull ; then
SYSTEMD_SERVICE="$(systemctl -t service list-unit-files | grep -e "^${service_name,,}" | awk '{print$1}' | head -1)"
fi
fi
fi
if [[ -z "$SYSTEMD_SERVICE" ]]; then
if [[ -n "$_SYSV_INIT_SCRIPT" ]]; then
if [[ -x "/etc/init.d/$_SYSV_INIT_SCRIPT" ]]; then
SYSV_INIT_SCRIPT="/etc/init.d/$_SYSV_INIT_SCRIPT"
fi
fi
if [[ -z "$SYSV_INIT_SCRIPT" ]] ; then
if [[ -x "/etc/init.d/${service_name,,}" ]]; then
SYSV_INIT_SCRIPT="/etc/init.d/${service_name,,}"
elif [[ -x "/etc/init.d/${service_name,,}d" ]]; then
SYSV_INIT_SCRIPT="/etc/init.d/${service_name,,}d"
fi
fi
fi
if [[ -z "$SYSTEMD_SERVICE" ]] && [[ -z "$SYSV_INIT_SCRIPT" ]] ; then
fatal "Neither an init-script nor a service file for $service_name found!"
fi
#---------------------------------------
#-----------------------------
# Check if SSH service is running
#-----------------------------
#---------------------------------------
if $LOGGING ; then
echo -e "\n Check if $service_name service is running.."
echo -e " ================================="
fi
if ! ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}" | grep -v grep > /dev/null ; then
error "$service_name service seems to be down! Trying to restart service now.."
if [[ -n "$SYSTEMD_SERVICE" ]] ; then
$systemctl daemon-reload > /dev/null 2> ${LOCK_DIR}/err_msg.log
if [[ $? -ne 0 ]]; then
error "$(cat ${LOCK_DIR}/err_msg.log)"
fi
sleep 2
$systemctl stop $SYSTEMD_SERVICE > /dev/null 2> ${LOCK_DIR}/err_msg.log
if [[ $? -ne 0 ]]; then
error "$(cat ${LOCK_DIR}/err_msg.log)"
fi
sleep 10
$systemctl start $SYSTEMD_SERVICE > /dev/null 2> ${LOCK_DIR}/err_msg.log
if [[ $? -ne 0 ]]; then
error "$(cat ${LOCK_DIR}/err_msg.log)"
fi
else
$SYSV_INIT_SCRIPT stop > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
error "Stopping $service_name service failed!"
fi
sleep 10
$SYSV_INIT_SCRIPT start > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
error "Starting $service_name service 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 $service_name service failed!"
else
ok "$service_name service is up and running."
fi
else
if $LOGGING ; then
ok "$service_name service is up and running."
fi
fi
rm -rf $LOCK_DIR
exit 0