monitoring/check_postfwd.sh

257 lines
6.3 KiB
Bash
Executable File

#!/usr/bin/env bash
service_name=postfwd
#---------------------------------------
#-----------------------------
# 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"
}
trim_double_quotes() {
local var="$*"
var="${var#"${var%%[!\"]*}"}" # remove leading whitespace characters
var="${var%"${var##*[!\"]}"}" # remove trailing whitespace characters
echo -n "$var"
}
read_env_from_file() {
_file="$*"
while read _ENV _REST ; do
_ENV="$(trim $_ENV)"
if [[ "$_ENV" =~ .{2,}=.+ ]]; then
_key="${_ENV%=*}"
_val="${_ENV##*=}"
# - Remove leading / trailling double quotes
# - _val="${_val%\"}"
# - _val="${_val#\"}"
# -
_val="$(trim_double_quotes $_val)"
if [[ -n "$(trim $_key)" ]] && [[ -n "$(trim $_val)" ]] ; then
export $_ENV
fi
fi
done < "$_file"
}
#---------------------------------------
#-----------------------------
# 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
# - 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 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
if [[ -z "$SYSTEMD_SERVICE" ]]; 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
if [[ -z "$SYSTEMD_SERVICE" ]] && [[ -z "$SYSV_INIT_SCRIPT" ]] ; then
fatal "Neither an init-script nor a service file for $service_name found!"
fi
# - Determin check_string_ps ..
# -
if [[ -n "$SYSTEMD_SERVICE" ]] ; then
_systemd_service_file=""
if [[ -f "/etc/systemd/system/$SYSTEMD_SERVICE" ]] ; then
_systemd_service_file="/etc/systemd/system/$SYSTEMD_SERVICE"
elif [[ -f "/lib/systemd/system/$SYSTEMD_SERVICE" ]] ; then
_systemd_service_file="/lib/systemd/system/$SYSTEMD_SERVICE"
fi
if [[ -n "$_systemd_service_file" ]]; then
check_string_ps="$(trim $(cat "$_systemd_service_file" | grep ExecStart | grep -o -E "ExecStart\s*=\s*[^[:space:]]+" | cut -d "=" -f2))"
fi
else
read_env_from_file $SYSV_INIT_SCRIPT
_check_string_ps="$(trim $(cat "$SYSV_INIT_SCRIPT" | grep DAEMON | grep -o -E "DAEMON\s*=\s*[^[:space:]]+" | cut -d "=" -f2))"
check_string_ps="$(eval echo "$_check_string_ps")"
fi
#---------------------------------------
#-----------------------------
# Check if 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