Merge branch 'master' of https://git.oopen.de/script/monitoring
This commit is contained in:
commit
0611d0a2ad
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
check_webservice_load.conf
|
||||
BAK/*
|
||||
*.swp
|
||||
/conf/*.conf
|
||||
/BAK/*
|
||||
|
@ -93,8 +93,8 @@ if ($result =~/2.7.[01] Ok, discarded/) {
|
||||
exit $STATES{OK};
|
||||
} else {
|
||||
print "[ Warning ]: Respond of amavisd-new service is not as expected !\n";
|
||||
print " amavisd-new returned:\n $result\n";
|
||||
print "\n\nRestart Service amavisd-new now..";
|
||||
print " amavisd-new returned:\n $result\n\n";
|
||||
print "\n\nRestart Service amavisd-new now..\n";
|
||||
|
||||
# - Restart Service amavisd-new
|
||||
# -
|
||||
|
411
check_cert_for_service.sh
Executable file
411
check_cert_for_service.sh
Executable file
@ -0,0 +1,411 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# ===============================================================
|
||||
# - Don't make definitions here! Do this at the configuration file
|
||||
# ================================================================
|
||||
|
||||
working_dir="$(dirname $(realpath $0))"
|
||||
conf_file="${working_dir}/conf/check_cert_for_service.conf"
|
||||
|
||||
# ----------
|
||||
# -
|
||||
# - Script checks, whether the certificate for mumble services are up to date. If
|
||||
# - newer versions than the installed one found, script changes the installed
|
||||
# - key/cert to the latest version.
|
||||
# -
|
||||
# - Note !!
|
||||
# - This script is very special to the server environment of machine 'o13-il.oopen.de'
|
||||
# -
|
||||
# ----------
|
||||
|
||||
LOCK_DIR="/tmp/check_cert_${service_name}.sh.LOCK"
|
||||
log_file="${LOCK_DIR}/check_cert_${service_name}.log"
|
||||
|
||||
restart_service=false
|
||||
|
||||
|
||||
# -------------
|
||||
# --- Some functions
|
||||
# -------------
|
||||
|
||||
clean_up() {
|
||||
|
||||
# Perform program exit housekeeping
|
||||
rm -rf "$LOCK_DIR"
|
||||
exit $1
|
||||
}
|
||||
|
||||
echononl(){
|
||||
if $terminal ; then
|
||||
echo X\\c > /tmp/shprompt$$
|
||||
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
|
||||
echo -e -n "$*\\c" 1>&2
|
||||
else
|
||||
echo -e -n "$*" 1>&2
|
||||
fi
|
||||
rm /tmp/shprompt$$
|
||||
fi
|
||||
}
|
||||
|
||||
fatal(){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
|
||||
echo ""
|
||||
echo -e " \033[31m\033[1mScript was interupted\033[m!"
|
||||
else
|
||||
echo " [ Fatal ]: $*"
|
||||
echo ""
|
||||
echo " Script was terminated...."
|
||||
fi
|
||||
echo ""
|
||||
clean_up 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[1mWarning\033[m ]: $*"
|
||||
else
|
||||
echo " [ Warning ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
info() {
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
|
||||
echo ""
|
||||
fi
|
||||
}
|
||||
|
||||
echo_failed(){
|
||||
if $terminal ; then
|
||||
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
|
||||
fi
|
||||
}
|
||||
|
||||
echo_done() {
|
||||
if $terminal ; then
|
||||
echo -e "\033[75G[ \033[32mok\033[m ]"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# -------------
|
||||
# - Job is already running?
|
||||
# -------------
|
||||
|
||||
# - If job already runs, stop execution..
|
||||
# -
|
||||
if mkdir "$LOCK_DIR" 2> /dev/null ; then
|
||||
|
||||
# - Remove lockdir when the script finishes, or when it receives a signal
|
||||
# -
|
||||
trap clean_up SIGHUP SIGINT SIGTERM
|
||||
|
||||
else
|
||||
|
||||
msg="A previos instance of script \"`basename $0`\" seems already be running."
|
||||
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e "[ \033[31m\033[1mFatal\033[m ]: $msg"
|
||||
echo ""
|
||||
echo -e " \033[31m\033[1mScript was interupted\033[m!"
|
||||
else
|
||||
echo " [ Fatal ]: $msg"
|
||||
echo ""
|
||||
echo " Script was interupted!"
|
||||
fi
|
||||
echo
|
||||
|
||||
exit 1
|
||||
|
||||
fi
|
||||
|
||||
|
||||
# -------------
|
||||
# --- Some checks
|
||||
# -------------
|
||||
|
||||
# - Running in a terminal?
|
||||
# -
|
||||
if [[ -t 1 ]] ; then
|
||||
terminal=true
|
||||
else
|
||||
terminal=false
|
||||
fi
|
||||
|
||||
|
||||
# - Read Configurations from $conf_file
|
||||
# -
|
||||
if [[ ! -f "$conf_file" ]]; then
|
||||
fatal " Configuration file '$(basename ${conf_file})' not found!"
|
||||
else
|
||||
source $conf_file
|
||||
fi
|
||||
|
||||
# - Systemd supported ?
|
||||
# -
|
||||
systemd_supported=false
|
||||
systemd=$(which systemd)
|
||||
systemctl=$(which systemctl)
|
||||
|
||||
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|generated)" 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,,}-server" \
|
||||
| grep -q -E "(enabled|disabled|generated)" 2> /devnull ; then
|
||||
|
||||
SYSTEMD_SERVICE="$(systemctl -t service list-unit-files | grep -e "^${service_name,,}-server" | awk '{print$1}' | head -1)"
|
||||
elif systemctl -t service list-unit-files \
|
||||
| grep -e "^${service_name,,}" \
|
||||
| grep -q -E "(enabled|disabled|generated)" 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"
|
||||
elif [[ -x "/etc/init.d/${service_name,,}-server" ]]; then
|
||||
SYSV_INIT_SCRIPT="/etc/init.d/${service_name,,}-server"
|
||||
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
|
||||
|
||||
#echo "SYSTEMD_SERVICE: $SYSTEMD_SERVICE"
|
||||
#echo "SYSV_INIT_SCRIPT: $SYSV_INIT_SCRIPT"
|
||||
#clean_up 0
|
||||
|
||||
|
||||
|
||||
# -------------
|
||||
# - Don't run script, if any give path for cert/key does not exists
|
||||
# -------------
|
||||
|
||||
if [[ ! -f "$cert_installed" ]] ; then
|
||||
fatal "Installed Certificate '$cert_installed' for service '${service_name}' not found!"
|
||||
elif [[ ! -f "$cert_newest" ]] ; then
|
||||
fatal "Newest Certificate '$cert_newest' for service '${service_name}' not found!"
|
||||
elif [[ ! -f "$key_installed" ]]; then
|
||||
fatal "Installed Key '$key_installed' for service '${service_name}' not found!"
|
||||
elif [[ ! -f "$key_newest" ]] ; then
|
||||
fatal "Newest Key '$key_newest' for service '${service_name}' not found!"
|
||||
fi
|
||||
|
||||
|
||||
# -------------
|
||||
# - Check if key/cert are up to date, change them if needed.
|
||||
# -------------
|
||||
|
||||
if ! diff "$(realpath "$cert_installed")" "$(realpath "$cert_newest")" > /dev/null 2>&1 ; then
|
||||
|
||||
_failed=false
|
||||
|
||||
warn "Certificate for service '${service_name}' is outdated!
|
||||
|
||||
Try to update certificate and key.."
|
||||
|
||||
echononl " Update certificat for for service '${service_name}' .."
|
||||
> $log_file
|
||||
if [[ -h "$cert_installed" ]] ; then
|
||||
rm "$(realpath "$cert_installed")" >> $log_file 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_failed=true
|
||||
fi
|
||||
fi
|
||||
|
||||
rm "$cert_installed" >> $log_file 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_failed=true
|
||||
fi
|
||||
|
||||
cp -a "$(realpath "$cert_newest")" "$(dirname "$cert_installed")" >> $log_file 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_failed=true
|
||||
fi
|
||||
|
||||
chown ${service_user}:$service_group "$(dirname "$cert_installed")/$(basename "$(realpath "$cert_newest")")" >> $log_file 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_failed=true
|
||||
fi
|
||||
|
||||
ln -s "$(basename "$(realpath "$cert_newest")")" "$cert_installed" >> $log_file 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_failed=true
|
||||
fi
|
||||
|
||||
if $_failed ; then
|
||||
echo_failed
|
||||
error "$(cat "$log_file")"
|
||||
else
|
||||
echo_done
|
||||
fi
|
||||
|
||||
if ! $_failed ; then
|
||||
|
||||
_failed=false
|
||||
|
||||
echononl " Update key for service '${service_name}' .."
|
||||
if [[ -h "$key_installed" ]] ; then
|
||||
rm "$(realpath "$key_installed")" >> $log_file 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_failed=true
|
||||
fi
|
||||
fi
|
||||
|
||||
rm "$key_installed" > $log_file 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_failed=true
|
||||
fi
|
||||
|
||||
cp -a "$(realpath "$key_newest")" "$(dirname "$key_installed")" >> $log_file 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_failed=true
|
||||
fi
|
||||
|
||||
chown ${service_user}:$service_group "$(dirname "$key_installed")/$(basename "$(realpath "$key_newest")")" >> $log_file 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_failed=true
|
||||
fi
|
||||
|
||||
ln -s "$(basename "$(realpath "$key_newest")")" "$key_installed" >> $log_file 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_failed=true
|
||||
fi
|
||||
|
||||
if $_failed ; then
|
||||
echo_failed
|
||||
error "$(cat "$log_file")"
|
||||
else
|
||||
echo_done
|
||||
restart_service=true
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
if ! $_failed ; then
|
||||
if $terminal ; then
|
||||
info "Certificate/Key for service '${service_name}' is now up to date"
|
||||
else
|
||||
echo ""
|
||||
echo " [ Info ]: Certificate/Key for service '${service_name}' is now up to date"
|
||||
echo ""
|
||||
fi
|
||||
else
|
||||
error "Updating Certificate/Key for service '${service_name}' failed!"
|
||||
fi
|
||||
|
||||
else
|
||||
up_to_date=true
|
||||
info "Certificate for service '${service_name}' is up to date!"
|
||||
fi
|
||||
|
||||
if $restart_service ; then
|
||||
|
||||
_failed=false
|
||||
echononl "Going to restart Service '${service_name}' .."
|
||||
if [[ -n "$SYSTEMD_SERVICE" ]] ; then
|
||||
$systemctl daemon-reload > $log_file 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_failed=true
|
||||
fi
|
||||
sleep 2
|
||||
|
||||
$systemctl stop $SYSTEMD_SERVICE >> $log_file 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_failed=true
|
||||
fi
|
||||
sleep 10
|
||||
|
||||
$systemctl start $SYSTEMD_SERVICE >> $log_file 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_failed=true
|
||||
fi
|
||||
if $_failed ; then
|
||||
echo_failed
|
||||
error "$($log_file)"
|
||||
else
|
||||
echo_done
|
||||
fi
|
||||
else
|
||||
$SYSV_INIT_SCRIPT stop > $log_file 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_failed=true
|
||||
fi
|
||||
sleep 10
|
||||
$SYSV_INIT_SCRIPT start >> $log_file 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_failed=true
|
||||
fi
|
||||
if $_failed ; then
|
||||
echo_failed
|
||||
error "$($log_file)"
|
||||
else
|
||||
echo_done
|
||||
fi
|
||||
fi
|
||||
|
||||
if ! $_failed ; then
|
||||
|
||||
declare -i counter=0
|
||||
PID=$(ps -e f | grep -E "${check_string_ps}"| grep -v grep | awk '{print$1}')
|
||||
while [[ "X${PID}" = "X" ]]; do
|
||||
if [[ $counter -gt 10 ]]; then
|
||||
_failed=true
|
||||
break
|
||||
else
|
||||
((counter++))
|
||||
fi
|
||||
sleep 1
|
||||
PID=$(ps -e f | grep -E "${check_string_ps}"| grep -v grep | awk '{print$1}')
|
||||
done
|
||||
|
||||
fi
|
||||
|
||||
if $_failed ; then
|
||||
error "Restarting service '${service_name}' failed!"
|
||||
else
|
||||
if $terminal ; then
|
||||
info "'${service_name}' Service was restarted. the new pid is '$PID'."
|
||||
else
|
||||
echo ""
|
||||
echo " '${service_name}' Service was restarted. the new pid is '$PID'."
|
||||
echo ""
|
||||
fi
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
clean_up 0
|
||||
|
263
check_dhcp.sh
Executable file
263
check_dhcp.sh
Executable file
@ -0,0 +1,263 @@
|
||||
#!/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
|
215
check_dns.sh
215
check_dns.sh
@ -1,20 +1,221 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||
check_string_ps=""
|
||||
if [[ -f "/usr/sbin/named" ]] ; then
|
||||
check_string_ps="/usr/sbin/named"
|
||||
fi
|
||||
|
||||
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=`mktemp -d`
|
||||
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# 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 ""
|
||||
}
|
||||
|
||||
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
|
||||
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
|
||||
echo -n "$var"
|
||||
}
|
||||
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Check some prerequisites
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
# - Running in a terminal?
|
||||
# -
|
||||
if [[ -t 1 ]] ; then
|
||||
terminal=true
|
||||
LOGGING=true
|
||||
else
|
||||
terminal=false
|
||||
LOGGING=false
|
||||
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
|
||||
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 ip_addr in $alternate_addr ; do
|
||||
ping -c3 $ip_addr >/dev/null 2> /dev/null
|
||||
ping -c3 $ip_addr >> /dev/null 2>&1
|
||||
if [ $? -eq 0 ]; then
|
||||
exit 0
|
||||
|
||||
PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}')
|
||||
if [[ "X${PID}" = "X" ]]; then
|
||||
break
|
||||
else
|
||||
|
||||
if $LOGGING ; then
|
||||
ok "Bind Nameservice is up and running."
|
||||
fi
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
done;
|
||||
|
||||
/etc/init.d/bind9 restart
|
||||
error "Bind Nameservice seems to be down! Trying to restart service now.."
|
||||
|
||||
echo ""
|
||||
echo "[ Info ]: Restartet Nameservice bind on host `hostname --long` at `date +\"%d.%m.%Y %H:%M:%S\"`"
|
||||
echo ""
|
||||
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!"
|
||||
else
|
||||
ok "Bind Nameservice is up and running."
|
||||
fi
|
||||
|
||||
exit 0;
|
||||
|
184
check_dyndns.sh
184
check_dyndns.sh
@ -1,8 +1,25 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||
|
||||
function usage() {
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Predifined Variables
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
conf_file="/etc/ddclient.conf"
|
||||
ddclient_script="/usr/sbin/ddclient"
|
||||
|
||||
_SKIP_IP=false
|
||||
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Functions
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
usage() {
|
||||
|
||||
echo
|
||||
|
||||
@ -15,93 +32,136 @@ function usage() {
|
||||
}
|
||||
|
||||
|
||||
fatal(){
|
||||
echo ""
|
||||
echo -e " [ Fatal ] $*"
|
||||
echo ""
|
||||
echo -e "\tScript terminated.."
|
||||
echo ""
|
||||
exit 1
|
||||
}
|
||||
info (){
|
||||
echo ""
|
||||
echo -e " [ Info ] $*"
|
||||
echo ""
|
||||
}
|
||||
|
||||
ok (){
|
||||
echo ""
|
||||
echo -e " [ Ok ] $*"
|
||||
echo ""
|
||||
}
|
||||
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Evaluate command line
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
[ $# -eq "0" -o $# -gt "2" ] && usage "wrong number of arguments"
|
||||
|
||||
DYNDNS_NAME=$1
|
||||
|
||||
_SKIP_IP=false
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Some checks
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
|
||||
|
||||
## - only run ddclient, if it is installed
|
||||
## -
|
||||
if [ ! -x "/usr/sbin/ddclient" ]; then
|
||||
exit 0
|
||||
# - Only run ddclient, if it is installed
|
||||
# -
|
||||
if [[ ! -x "$ddclient_script" ]]; then
|
||||
fatal "Script '${ddclient_script}' not found!"
|
||||
fi
|
||||
|
||||
# - Configuration present?
|
||||
# -
|
||||
if [[ ! -f "$conf_file" ]]; then
|
||||
fatal "ddclient Configuration '${conf_file}' not found!"
|
||||
fi
|
||||
|
||||
if [ -f "/etc/ddclient.conf" ]; then
|
||||
|
||||
## - read variable "if" from configurationd file "/etc/ddclient.conf"
|
||||
## -
|
||||
eval `sed -n 's/\(if=[^ ,]*\)/\1/p' /etc/ddclient.conf`
|
||||
|
||||
## - check if interface used by dyndns is present
|
||||
##
|
||||
if [ -n "$if" ];then
|
||||
for _IFACE in `ifconfig | grep -e"^ppp" | cut -d" " -f1 `; do
|
||||
if [ "$_IFACE" = $if ];then
|
||||
PPP_IFACE=$_IFACE
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
## - exit if no connection for dydns interface exists
|
||||
## -
|
||||
if [ "X" = "X$PPP_IFACE" ]; then
|
||||
echo "No interface for dydns exists. exiting now.."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
else
|
||||
_SKIP_IP=true
|
||||
fi
|
||||
# - Running on a terminal?
|
||||
# -
|
||||
if [[ -t 1 ]] ; then
|
||||
LOGGING=true
|
||||
else
|
||||
## - No configuration file found, so do not run
|
||||
## -
|
||||
echo "No configuration file \"/etc/ddclient.conf\" found, so do not run"
|
||||
exit 0
|
||||
LOGGING=false
|
||||
fi
|
||||
|
||||
# - Check, if ddclient uses a local interface (variable "if")
|
||||
# -
|
||||
# - if true, interface name will be stored into variable 'if'
|
||||
# -
|
||||
eval `sed -n 's/\(if=[^ ,]*\)/\1/p' /etc/ddclient.conf`
|
||||
|
||||
# - Check if interface used by dyndns is present
|
||||
#
|
||||
if [ -n "$if" ];then
|
||||
for _IFACE in `ifconfig | grep -e"^ppp" | cut -d" " -f1 `; do
|
||||
if [ "$_IFACE" = $if ];then
|
||||
PPP_IFACE=$_IFACE
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
## - exit if no connection for dydns interface exists
|
||||
## -
|
||||
if [ "X" = "X$PPP_IFACE" ]; then
|
||||
fatal "No interface for dydns exists."
|
||||
fi
|
||||
|
||||
else
|
||||
_SKIP_IP=true
|
||||
fi
|
||||
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Update DynDNS IP-Address if needed
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
if ! $_SKIP_IP ; then
|
||||
|
||||
PPP_REMOTE=`ifconfig $PPP_IFACE 2>/dev/null | grep -e "P-[tz]-P" | cut -d":" -f3 | cut -d " " -f1`
|
||||
PPP_LOCAL=`ifconfig $PPP_IFACE 2>/dev/null | grep -e "P-[tz]-P" | cut -d":" -f2 | cut -d " " -f1`
|
||||
fi
|
||||
|
||||
|
||||
if [ "X$PPP_REMOTE" != "X" -a "X$PPP_LOCAL" != "X" ] ; then
|
||||
|
||||
if $_SKIP_IP ; then
|
||||
|
||||
# Load defaults
|
||||
if [ -f /etc/default/ddclient ]; then
|
||||
|
||||
source /etc/default/ddclient
|
||||
if [ "$run_daemon" = "true" ]; then
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
/usr/sbin/ddclient
|
||||
|
||||
else
|
||||
if [ "X$PPP_REMOTE" != "X" -a "X$PPP_LOCAL" != "X" ] ; then
|
||||
|
||||
DYNDNS_IP=`dig $DYNDNS_NAME +short`
|
||||
#echo "$DYNDNS_NAME: $DYNDNS_IP - Local (PPP) IP: $PPP_LOCAL"
|
||||
|
||||
if [ "$DYNDNS_IP" != "$PPP_LOCAL" ] ; then
|
||||
|
||||
## - Run ddclient with the IP address of the ppp device
|
||||
## -
|
||||
echo "try to update ipadress entry at dyndns.org.."
|
||||
echo
|
||||
# - Run ddclient with the IP address of the ppp device
|
||||
# -
|
||||
info "Try to update ip-adress entry at dyndns.org.."
|
||||
/usr/sbin/ddclient -syslog -ip $PPP_LOCAL
|
||||
else
|
||||
if $LOGGING ; then
|
||||
ok "DynDNS IP is up-to-date"
|
||||
fi
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
else
|
||||
|
||||
# - Load defaults
|
||||
source /etc/default/ddclient
|
||||
if [ "$run_daemon" = "true" ]; then
|
||||
if $LOGGING ; then
|
||||
info "'ddclient' is configured to run as daemon. So nothing to do here."
|
||||
fi
|
||||
else
|
||||
if $LOGGING ; then
|
||||
info "Going to run '${ddclient_script}'.."
|
||||
fi
|
||||
/usr/sbin/ddclient -file $conf_file
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
|
||||
exit 0;
|
||||
|
269
check_nginx_service.sh
Executable file
269
check_nginx_service.sh
Executable file
@ -0,0 +1,269 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
working_dir="$(dirname $(realpath $0))"
|
||||
|
||||
|
||||
# -------------
|
||||
# - Some Variables
|
||||
# -------------
|
||||
|
||||
LOCK_DIR="/tmp/$(basename $0).LOCK"
|
||||
|
||||
service_name="nginx"
|
||||
alert_email_arr="ckubu@oopen.de ckubu@gmx.de"
|
||||
sender_address="check_${service_name}@$(hostname -f)"
|
||||
|
||||
check_string_ps="nginx: master"
|
||||
|
||||
restart_needed=false
|
||||
|
||||
# -------------
|
||||
# - Job is already running?
|
||||
# -------------
|
||||
|
||||
# - If job already runs, stop execution..
|
||||
# -
|
||||
if mkdir "$LOCK_DIR" 2> /dev/null ; then
|
||||
|
||||
## - Remove lockdir when the script finishes, or when it receives a signal
|
||||
trap clean_up SIGHUP SIGINT SIGTERM
|
||||
|
||||
else
|
||||
|
||||
datum="$(date +"%d.%m.%Y %H:%M")"
|
||||
|
||||
msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\n Exiting now.."
|
||||
|
||||
echo ""
|
||||
echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running."
|
||||
echo ""
|
||||
echo -e " Exiting now.."
|
||||
echo ""
|
||||
|
||||
for _email in ${alert_email_arr[@]} ; do
|
||||
echo -e "To:${_email}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \
|
||||
| sendmail -F "Error `hostname -f`" -f $sender_address $_email
|
||||
done
|
||||
|
||||
exit 1
|
||||
|
||||
fi
|
||||
|
||||
|
||||
# -------------
|
||||
# - Some functions
|
||||
# -------------
|
||||
|
||||
clean_up() {
|
||||
|
||||
# Perform program exit housekeeping
|
||||
rm -rf "$LOCK_DIR"
|
||||
exit $1
|
||||
}
|
||||
|
||||
echononl(){
|
||||
if $terminal ; then
|
||||
echo X\\c > /tmp/shprompt$$
|
||||
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
|
||||
echo -e -n "$*\\c" 1>&2
|
||||
else
|
||||
echo -e -n "$*" 1>&2
|
||||
fi
|
||||
rm /tmp/shprompt$$
|
||||
fi
|
||||
}
|
||||
|
||||
fatal(){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
|
||||
echo ""
|
||||
echo -e " \033[31m\033[1mScript was interupted\033[m!"
|
||||
else
|
||||
echo " [ Fatal ]: $*"
|
||||
echo ""
|
||||
echo " Script was terminated...."
|
||||
fi
|
||||
echo ""
|
||||
clean_up 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[1mWarning\033[m ]: $*"
|
||||
else
|
||||
echo "[ Warning ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
info (){
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
|
||||
echo ""
|
||||
fi
|
||||
}
|
||||
|
||||
echo_done() {
|
||||
if $terminal ; then
|
||||
echo -e "\033[75G[ \033[32mdone\033[m ]"
|
||||
fi
|
||||
}
|
||||
echo_failed(){
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
|
||||
fi
|
||||
}
|
||||
echo_skipped() {
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# -------------
|
||||
# - Check some prerequisites
|
||||
# -------------
|
||||
|
||||
# - Running in a terminal?
|
||||
# -
|
||||
if [[ -t 1 ]] ; then
|
||||
terminal=true
|
||||
else
|
||||
terminal=false
|
||||
fi
|
||||
|
||||
# - Systemd supported ?
|
||||
# -
|
||||
systemd=$(which systemd)
|
||||
systemctl=$(which systemctl)
|
||||
|
||||
systemd_supported=false
|
||||
if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then
|
||||
systemd_supported=true
|
||||
fi
|
||||
|
||||
# - Check How to start/stop service
|
||||
# -
|
||||
SYSTEMD_UNIT_FILE=""
|
||||
SYSY_INIT_SCRIPT=""
|
||||
if $systemd_supported ; then
|
||||
SYSTEMD_UNIT_FILE="$(systemctl list-unit-files | grep -E "${service_name}[^@]*\.service" | grep "enabled" | cut -d' ' -f1)"
|
||||
fi
|
||||
|
||||
if [[ -z "$SYSTEMD_UNIT_FILE" ]]; then
|
||||
SYSY_INIT_SCRIPT="$(service --status-all | awk '{print$4}' | grep ${service_name} | head -1)"
|
||||
fi
|
||||
|
||||
if [[ -z "$SYSTEMD_UNIT_FILE" ]] && [[ -z "$SYSY_INIT_SCRIPT" ]] ; then
|
||||
fatal "Neither an init-script nor a service file for ${service_name} service found!"
|
||||
fi
|
||||
|
||||
|
||||
# -------------
|
||||
# - Main Part of Script
|
||||
# -------------
|
||||
|
||||
if $terminal ; then
|
||||
echo -e "\n Check if ${service_name} service is running.."
|
||||
echo -e " ==================================="
|
||||
fi
|
||||
|
||||
PIDS="$(ps ax | grep -E "${check_string_ps}" | grep -v grep | awk '{print$1}')"
|
||||
if [[ -z "$PIDS" ]]; then
|
||||
restart_needed=true
|
||||
fi
|
||||
|
||||
if $restart_needed ;then
|
||||
|
||||
error "${service_name} service seems to be down. Try to (re)start .."
|
||||
|
||||
echononl " Stop service '${service_name}' first .."
|
||||
if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then
|
||||
systemctl stop $SYSTEMD_UNIT_FILE > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_done
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
else
|
||||
/etc/init.d/$SYSY_INIT_SCRIPT stop > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_done
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
fi
|
||||
|
||||
sleep 2
|
||||
|
||||
echononl " Start Service '${service_name}' .."
|
||||
if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then
|
||||
systemctl start $SYSTEMD_UNIT_FILE > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_done
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
else
|
||||
/etc/init.d/$SYSY_INIT_SCRIPT start > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_done
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
declare -i count=0
|
||||
|
||||
restart_sucessfully=false
|
||||
while [[ $count -lt 10 ]] && ! $restart_sucessfully ; do
|
||||
|
||||
((count++))
|
||||
|
||||
sleep 1
|
||||
|
||||
NEW_PID="$(ps ax | grep -E "${check_string_ps}" | grep -v grep | awk '{print$1}')"
|
||||
if [[ -z "$NEW_PID" ]]; then
|
||||
continue
|
||||
fi
|
||||
restart_sucessfully=true
|
||||
break
|
||||
|
||||
done
|
||||
|
||||
if ! $restart_sucessfully ; then
|
||||
error "Restarting service '${service_name}' failed."
|
||||
else
|
||||
|
||||
info "Service '${service_name}' was restarted and is now up and running.
|
||||
The new PID is $NEW_PID"
|
||||
|
||||
if ! $terminal ; then
|
||||
echo ""
|
||||
echo "[ Success ]: Service '${service_name}' was restarted and is now up and running."
|
||||
echo " The new PID is $NEW_PID"
|
||||
echo ""
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
else
|
||||
info "Service ${service_name} is up and running."
|
||||
fi
|
||||
|
||||
clean_up 0
|
||||
|
263
check_ntpd.sh
263
check_ntpd.sh
@ -1,22 +1,263 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
## - Check if ntp service is running. (Re)start service, if
|
||||
## - it is down.
|
||||
## -
|
||||
# - Which Service should be checkked
|
||||
# -
|
||||
# - This parameter MUST be given
|
||||
# -
|
||||
_service_name="NTP"
|
||||
|
||||
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||
# - The binary of the service
|
||||
# -
|
||||
# - If not given, script dries to determin it.
|
||||
# -
|
||||
_check_binary="$(which ntpd)"
|
||||
|
||||
if ! ps ax | grep /usr/sbin/ntpd | grep -v grep > /dev/null ; then
|
||||
# - The systemd service file
|
||||
# -
|
||||
# - If not given, script dries to determin it.
|
||||
#
|
||||
_SYSTEMD_SERVICE="ntp"
|
||||
|
||||
echo -e "\n\tNTP Daemon is not running. so i'm going to (re)start the ntp service..\n"
|
||||
# - The sysv init script of the service
|
||||
# -
|
||||
# - If not given, script dries to determin it.
|
||||
#
|
||||
#_SYSV_INIT_SCRIPT="ntp"
|
||||
|
||||
/etc/init.d/ntp stop
|
||||
|
||||
kill -9 `cat /var/run/ntpd.pid 2>/dev/null` > /dev/null 2>&1
|
||||
rm -f /var/run/ntpd.pid
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Base Function(s)
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
## - Start ntp daemon
|
||||
/etc/init.d/ntp start
|
||||
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 "$_SYSV_INIT_SCRIPT" ]]; then
|
||||
SYSV_INIT_SCRIPT="$_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
|
||||
|
195
check_postfix.sh
Executable file
195
check_postfix.sh
Executable file
@ -0,0 +1,195 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
check_string_ps=""
|
||||
if [[ -x /usr/lib/postfix/sbin/master ]]; then
|
||||
check_string_ps="/usr/lib/postfix/sbin/master"
|
||||
elif [[ -x "/usr/lib/postfix/master" ]] ; then
|
||||
check_string_ps="/usr/lib/postfix/master"
|
||||
elif [[ -x "/usr/libexec/postfix/master" ]] ; then
|
||||
check_string_ps="/usr/libexec/postfix/master"
|
||||
fi
|
||||
|
||||
postfix_init_script=""
|
||||
if [[ -x "/etc/init.d/postfix" ]] ; then
|
||||
postfix_init_script="/etc/init.d/postfix"
|
||||
fi
|
||||
|
||||
LOCK_DIR=`mktemp -d`
|
||||
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# 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
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
# - 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): Postfix 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 $postfix_init_script ]]; then
|
||||
fatal "$(basename $0): Missing Postfix Init-Script!"
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Check if Postfix Mailservice is running
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
if $LOGGING ; then
|
||||
echo -e "\n Check if Postfix Mailservice is running.."
|
||||
echo -e " ========================================="
|
||||
fi
|
||||
if ! ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}" | grep -v grep > /dev/null ; then
|
||||
error "Postfix Mailservice seems to be down! Trying to restart service now.."
|
||||
|
||||
if $systemd_supported ; 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 postfix > /dev/null 2> ${LOCK_DIR}/err_msg.log
|
||||
if [[ $? -ne 0 ]]; then
|
||||
error "$(cat ${LOCK_DIR}/err_msg.log)"
|
||||
fi
|
||||
sleep 10
|
||||
$systemctl start postfix > /dev/null 2> ${LOCK_DIR}/err_msg.log
|
||||
if [[ $? -ne 0 ]]; then
|
||||
error "$(cat ${LOCK_DIR}/err_msg.log)"
|
||||
fi
|
||||
else
|
||||
$postfix_init_script stop > /dev/null 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
error "Stopping Postfix Mailservice failed!"
|
||||
fi
|
||||
sleep 10
|
||||
$postfix_init_script start > /dev/null 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
error "Starting Postfix Mailservice 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 Postfix Mailservice failed!"
|
||||
else
|
||||
ok "Postfix Mailservice is up and running."
|
||||
fi
|
||||
|
||||
else
|
||||
if $LOGGING ; then
|
||||
ok "Postfix Mailservice is up and running."
|
||||
fi
|
||||
fi
|
||||
|
||||
rm -rf $LOCK_DIR
|
||||
exit 0
|
||||
|
||||
|
259
check_remote_websites.sh
Executable file
259
check_remote_websites.sh
Executable file
@ -0,0 +1,259 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
|
||||
working_dir="$(dirname $(realpath $0))"
|
||||
conf_file="${working_dir}/conf/check_remote_websites.conf"
|
||||
|
||||
LOCK_DIR="/tmp/check_remote_websites.LOCK"
|
||||
|
||||
declare -a alert_email_arr
|
||||
|
||||
# -------------
|
||||
# --- Read Configurations from $conf_file
|
||||
# -------------
|
||||
|
||||
if [[ ! -f "$conf_file" ]]; then
|
||||
echo ""
|
||||
echo -e " [ Fatal ] Configuration file '$(basename ${conf_file})' not found!"
|
||||
echo ""
|
||||
echo -e "\tScript terminated.."
|
||||
echo ""
|
||||
exit 1
|
||||
else
|
||||
source "$conf_file"
|
||||
fi
|
||||
|
||||
for _email in $alert_email_addresses ; do
|
||||
alert_email_arr+=("$_email")
|
||||
done
|
||||
|
||||
[[ -n "$sender_address" ]] || sender_address="check_websites@$(hostname -f)"
|
||||
[[ -n "$content_type" ]] || content_type='Content-Type: text/plain;\n charset="utf-8"'
|
||||
|
||||
[[ -n "$TIME_OUT" ]] || TIME_OUT=240
|
||||
TIME_OUT_MAX="$(expr ${TIME_OUT} + 5)"
|
||||
|
||||
|
||||
# -------------
|
||||
# - Job is already running?
|
||||
# -------------
|
||||
|
||||
# - If job already runs, stop execution..
|
||||
# -
|
||||
if mkdir "$LOCK_DIR" 2> /dev/null ; then
|
||||
|
||||
## - Remove lockdir when the script finishes, or when it receives a signal
|
||||
trap clean_up SIGHUP SIGINT SIGTERM
|
||||
|
||||
else
|
||||
|
||||
datum="$(date +"%d.%m.%Y %H:%M")"
|
||||
|
||||
msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\n Exiting now.."
|
||||
|
||||
echo ""
|
||||
echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running."
|
||||
echo ""
|
||||
echo -e " Exiting now.."
|
||||
echo ""
|
||||
|
||||
for _email in ${alert_email_arr[@]} ; do
|
||||
echo -e "To:${_email}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \
|
||||
| sendmail -F "Error `hostname -f`" -f $sender_address $_email
|
||||
done
|
||||
|
||||
exit 1
|
||||
|
||||
fi
|
||||
|
||||
|
||||
# -------------
|
||||
# --- Some functions
|
||||
# -------------
|
||||
|
||||
clean_up() {
|
||||
|
||||
# Perform program exit housekeeping
|
||||
rm -rf "$LOCK_DIR"
|
||||
exit $1
|
||||
}
|
||||
|
||||
echononl(){
|
||||
if $terminal && $LOGGING ; then
|
||||
echo X\\c > /tmp/shprompt$$
|
||||
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
|
||||
echo -e -n "$*\\c" 1>&2
|
||||
else
|
||||
echo -e -n "$*" 1>&2
|
||||
fi
|
||||
rm /tmp/shprompt$$
|
||||
fi
|
||||
}
|
||||
|
||||
fatal(){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e "[ \033[31m\033[1mError\033[m ]: $*"
|
||||
echo ""
|
||||
echo -e "\t\033[31m\033[1mScript was interupted\033[m!"
|
||||
else
|
||||
echo " [ Fatal ]: $*"
|
||||
echo ""
|
||||
echo " Script was terminated...."
|
||||
fi
|
||||
echo ""
|
||||
clean_up 1
|
||||
}
|
||||
|
||||
error (){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e "\t[ \033[31m\033[1mError\033[m ]: $*"
|
||||
else
|
||||
echo "[ Error ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
warn (){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
|
||||
else
|
||||
echo "[ Warning ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
echo_done() {
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[32mdone\033[m ]"
|
||||
else
|
||||
if $LOGGING ; then
|
||||
echo " [ done ]"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
echo_ok() {
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[32mok\033[m ]"
|
||||
else
|
||||
if $LOGGING ; then
|
||||
echo " [ ok ]"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
echo_failed(){
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
|
||||
else
|
||||
if $LOGGING ; then
|
||||
echo " [ failed ]"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
echo_skipped() {
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
|
||||
else
|
||||
if $LOGGING ; then
|
||||
echo " [ skipped ]"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
# -------------
|
||||
# --- Check some prerequisites
|
||||
# -------------
|
||||
|
||||
# - 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
|
||||
|
||||
|
||||
if $LOGGING ; then
|
||||
echo ""
|
||||
fi
|
||||
|
||||
if [[ -n "$WEBSITES_TO_CHECK" ]] ; then
|
||||
for _site in $WEBSITES_TO_CHECK ; do
|
||||
echononl " Check site \033[1m$_site\033[m .."
|
||||
|
||||
declare -i i=0
|
||||
_sucess=false
|
||||
while [[ $i -lt 3 ]] ; do
|
||||
response="$(curl --max-time $TIME_OUT --connect-timeout 30 \
|
||||
-I -k -L --write-out %{http_code} --silent --output /dev/null $_site 2> ${LOCK_DIR}/err.msg)"
|
||||
if [[ "$response" -eq 200 ]]; then
|
||||
echo_ok
|
||||
_sucess=true
|
||||
break
|
||||
fi
|
||||
sleep 2
|
||||
((i++))
|
||||
done
|
||||
|
||||
if ! $_success ; then
|
||||
echo_failed
|
||||
if [[ -s "$(cat ${LOCK_DIR}/err.msg)" ]] ; then
|
||||
error "$(cat ${LOCK_DIR}/err.msg)"
|
||||
fi
|
||||
websites_failed_arr+=("$_site")
|
||||
fi
|
||||
|
||||
done
|
||||
else
|
||||
warn "No Website to check given (empty var 'WEBSITES_TO_CHECK')"
|
||||
fi
|
||||
|
||||
if [[ ${#websites_failed_arr[@]} -gt 0 ]] ; then
|
||||
#if [[ ${#websites_failed_arr[@]} -eq 1 ]] ; then
|
||||
# err_msg="\n[ Error ]: Website ${websites_failed_arr[0]} does NOT respond as exspected\n"
|
||||
# error "Website ${websites_failed_arr[0]} does NOT respond as exspected"
|
||||
#else
|
||||
error "Some Websites do not respond as expected:"
|
||||
err_msg="\n[ Error ]: Some Websites do not respond as expected:\n\n"
|
||||
for _site in ${websites_failed_arr[@]} ; do
|
||||
err_msg+=" $_site\n"
|
||||
if $LOGGING ; then
|
||||
echo -e "\t \033[1m$_site\033[m"
|
||||
#echo -e "\n\tWebsite \033[1m$_site\033[m does NOT respond as exspected"
|
||||
else
|
||||
echo " $_site"
|
||||
fi
|
||||
done
|
||||
#fi
|
||||
|
||||
datum="$(date +"%d.%m.%Y %H:%M")"
|
||||
for _email in ${alert_email_arr[@]} ; do
|
||||
echo -e "To:${_email}\n${content_type}\nSubject:Error: Website(s) not reachable - ${datum}\n$err_msg" \
|
||||
| /usr/sbin/sendmail -F "$company - Check Websites" -f $sender_address $_email
|
||||
done
|
||||
fi
|
||||
|
||||
if $LOGGING ; then
|
||||
echo ""
|
||||
fi
|
||||
clean_up 0
|
||||
|
||||
#curl --max-time 35 --connect-timeout 30 -L --write-out %{http_code} --silent --output /dev/null https:/ats.warenform.de
|
@ -1,28 +1,263 @@
|
||||
#"!/usr/bin/env bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
PIDS=`ps aux | grep "/usr/local/samba/sbin/samba" | grep -v grep | awk '{print$2}'`
|
||||
working_dir="$(dirname $(realpath $0))"
|
||||
|
||||
|
||||
service_name="samba"
|
||||
check_string_ps="(sbin/smbd| sbin/samba )"
|
||||
|
||||
# -------------
|
||||
# - Some Variables
|
||||
# -------------
|
||||
|
||||
LOCK_DIR="/tmp/$(basename $0).LOCK"
|
||||
|
||||
|
||||
# -------------
|
||||
# - Job is already running?
|
||||
# -------------
|
||||
|
||||
# - If job already runs, stop execution..
|
||||
# -
|
||||
if mkdir "$LOCK_DIR" 2> /dev/null ; then
|
||||
|
||||
## - Remove lockdir when the script finishes, or when it receives a signal
|
||||
trap clean_up SIGHUP SIGINT SIGTERM
|
||||
|
||||
else
|
||||
|
||||
datum="$(date +"%d.%m.%Y %H:%M")"
|
||||
|
||||
msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\n Exiting now.."
|
||||
|
||||
echo ""
|
||||
echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running."
|
||||
echo ""
|
||||
echo -e " Exiting now.."
|
||||
echo ""
|
||||
|
||||
for _email in ${alert_email_arr[@]} ; do
|
||||
echo -e "To:${_email}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \
|
||||
| sendmail -F "Error `hostname -f`" -f $sender_address $_email
|
||||
done
|
||||
|
||||
exit 1
|
||||
|
||||
if [ "X${PIDS}X" = "XX" ];then
|
||||
echo -e "\n[Error]: Samba Service seems to be down. Try to (re)start .."
|
||||
/etc/init.d/samba4 stop > /dev/null 2>&1
|
||||
/etc/init.d/samba4 start > /dev/null
|
||||
sleep 2
|
||||
NEW_PIDS=`ps aux | grep "/usr/local/samba/sbin/samba" | grep -v grep | awk '{print$2}'`
|
||||
if [ "X${NEW_PIDS}X" = "XX" ]; then
|
||||
echo -e ""
|
||||
echo -e "\t[Error]: Restarting samba services failed !!"
|
||||
echo -e "\n\t[Error]: Restarting samba services failed !!\n"
|
||||
echo -e ""
|
||||
else
|
||||
PIDS=""
|
||||
for pid in $NEW_PIDS ; do
|
||||
PIDS="$PIDS $pid"
|
||||
done
|
||||
echo -e
|
||||
echo -e "\tI have restarted the samba services. The new PIDs are $PIDS"
|
||||
echo -e
|
||||
fi
|
||||
fi
|
||||
|
||||
exit 0
|
||||
|
||||
# -------------
|
||||
# - Some functions
|
||||
# -------------
|
||||
|
||||
clean_up() {
|
||||
|
||||
# Perform program exit housekeeping
|
||||
rm -rf "$LOCK_DIR"
|
||||
exit $1
|
||||
}
|
||||
|
||||
echononl(){
|
||||
if $terminal ; then
|
||||
echo X\\c > /tmp/shprompt$$
|
||||
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
|
||||
echo -e -n "$*\\c" 1>&2
|
||||
else
|
||||
echo -e -n "$*" 1>&2
|
||||
fi
|
||||
rm /tmp/shprompt$$
|
||||
fi
|
||||
}
|
||||
|
||||
fatal(){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
|
||||
echo ""
|
||||
echo -e " \033[31m\033[1mScript was interupted\033[m!"
|
||||
else
|
||||
echo " [ Fatal ]: $*"
|
||||
echo ""
|
||||
echo " Script was terminated...."
|
||||
fi
|
||||
echo ""
|
||||
clean_up 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[1mWarning\033[m ]: $*"
|
||||
else
|
||||
echo "[ Warning ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
info (){
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
|
||||
echo ""
|
||||
fi
|
||||
}
|
||||
|
||||
echo_done() {
|
||||
if $terminal ; then
|
||||
echo -e "\033[75G[ \033[32mdone\033[m ]"
|
||||
fi
|
||||
}
|
||||
echo_failed(){
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
|
||||
fi
|
||||
}
|
||||
echo_skipped() {
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# -------------
|
||||
# - Check some prerequisites
|
||||
# -------------
|
||||
|
||||
# - Running in a terminal?
|
||||
# -
|
||||
if [[ -t 1 ]] ; then
|
||||
terminal=true
|
||||
else
|
||||
terminal=false
|
||||
fi
|
||||
|
||||
# - Systemd supported ?
|
||||
# -
|
||||
systemd=$(which systemd)
|
||||
systemctl=$(which systemctl)
|
||||
|
||||
systemd_supported=false
|
||||
if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then
|
||||
systemd_supported=true
|
||||
fi
|
||||
|
||||
# - Check How to start/stop service
|
||||
# -
|
||||
SYSTEMD_UNIT_FILE=""
|
||||
SYSY_INIT_SCRIPT=""
|
||||
if $systemd_supported ; then
|
||||
SYSTEMD_UNIT_FILE="$(systemctl list-unit-files | grep -E "${service_name}[^@]*\.service" | grep "enabled" | cut -d' ' -f1)"
|
||||
fi
|
||||
|
||||
if [[ -z "$SYSTEMD_UNIT_FILE" ]]; then
|
||||
SYSY_INIT_SCRIPT="$(service --status-all | awk '{print$4}' | grep ${service_name} | head -1)"
|
||||
fi
|
||||
|
||||
if [[ -z "$SYSTEMD_UNIT_FILE" ]] && [[ -z "$SYSY_INIT_SCRIPT" ]] ; then
|
||||
fatal "Neither an init-script nor a service file for ${service_name} service found!"
|
||||
fi
|
||||
|
||||
|
||||
# -------------
|
||||
# - Main Part of Script
|
||||
# -------------
|
||||
|
||||
if $terminal ; then
|
||||
echo -e "\n Check if Service '${service_name}' is running.."
|
||||
echo -e " ====================================="
|
||||
fi
|
||||
|
||||
PIDS="$(ps ax | grep -E "$check_string_ps" | grep -v grep | awk '{print$1}')"
|
||||
|
||||
if [[ -z "$PIDS" ]];then
|
||||
|
||||
error "$service_name service seems to be down. Try to (re)start .."
|
||||
|
||||
echononl " Stop Service '${service_name}' first .."
|
||||
if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then
|
||||
systemctl stop $SYSTEMD_UNIT_FILE > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_done
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
else
|
||||
/etc/init.d/$SYSY_INIT_SCRIPT stop > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_done
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
fi
|
||||
|
||||
sleep 2
|
||||
|
||||
echononl " Start Service '${service_name}' .."
|
||||
if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then
|
||||
systemctl start $SYSTEMD_UNIT_FILE > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_done
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
else
|
||||
/etc/init.d/$SYSY_INIT_SCRIPT start > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_done
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
declare -i count=0
|
||||
|
||||
while [[ $count -lt 10 ]] && [[ -z "${PIDS}" ]]; do
|
||||
sleep 1
|
||||
PIDS="$(ps ax | grep -E "$check_string_ps" | grep -v grep | awk '{print$1}')"
|
||||
((count++))
|
||||
done
|
||||
|
||||
if [[ -z "${PIDS}" ]] ; then
|
||||
error "Restarting Service '${service_name}' failed"
|
||||
else
|
||||
|
||||
NEW_PIDS=""
|
||||
count=1
|
||||
for _pid in $PIDS ; do
|
||||
if [[ $count -eq 1 ]] ; then
|
||||
NEW_PIDS="$_pid"
|
||||
else
|
||||
NEW_PIDS="$NEW_PIDS $_pid"
|
||||
fi
|
||||
((count++))
|
||||
done
|
||||
|
||||
info "Service '${service_name}' was restarted and is now up and running.
|
||||
The new PID is $NEW_PIDS"
|
||||
|
||||
if ! $terminal ; then
|
||||
echo ""
|
||||
echo "[ Success ]: Service '${service_name}' was restarted and is now up and running."
|
||||
echo " The new PIDs are $NEW_PIDS"
|
||||
echo ""
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
else
|
||||
info "Service '${service_name}' is up and running."
|
||||
fi
|
||||
|
||||
clean_up 0
|
||||
|
||||
|
221
check_service.sh
Executable file
221
check_service.sh
Executable file
@ -0,0 +1,221 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# 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 "$1" ]] ; then
|
||||
service_name=$1
|
||||
else
|
||||
fatal "$(basename $0): No Service given"
|
||||
fi
|
||||
|
||||
# - Is Service installed ?
|
||||
# -
|
||||
_check_binary="$(which ${service_name,,}d)"
|
||||
if [[ -z "$_check_binary" ]] ; then
|
||||
_check_binary="$(which ${service_name,,})"
|
||||
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 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
|
||||
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# 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
|
185
check_ssh.sh
Executable file
185
check_ssh.sh
Executable file
@ -0,0 +1,185 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
sshd_binary="$(which sshd)"
|
||||
|
||||
check_string_ps="$sshd_binary"
|
||||
|
||||
LOCK_DIR=`mktemp -d`
|
||||
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Base Function(s)
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
fatal(){
|
||||
echo ""
|
||||
echo -e " [ Fatal ] $*"
|
||||
echo ""
|
||||
echo -e "\tScript terminated.."
|
||||
echo ""
|
||||
rm -rf $LOCK_DIR
|
||||
exit 1
|
||||
}
|
||||
|
||||
error (){
|
||||
echo ""
|
||||
echo -e " [ Error ] $*"
|
||||
echo ""
|
||||
}
|
||||
|
||||
warn (){
|
||||
echo ""
|
||||
echo -e " [ Warn ] $*"
|
||||
echo ""
|
||||
}
|
||||
|
||||
info (){
|
||||
echo ""
|
||||
echo -e " [ Info ] $*"
|
||||
echo ""
|
||||
}
|
||||
|
||||
ok (){
|
||||
echo ""
|
||||
echo -e " [ Ok ] $*"
|
||||
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
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
# - Running in a terminal?
|
||||
# -
|
||||
if [[ -t 1 ]] ; then
|
||||
terminal=true
|
||||
LOGGING=true
|
||||
else
|
||||
terminal=false
|
||||
LOGGING=false
|
||||
fi
|
||||
|
||||
# - Is SSH Service installed ?
|
||||
# -
|
||||
if [[ -z "$sshd_binary" ]]; then
|
||||
fatal "$(basename $0): SSH Service seems NOT to be installed"
|
||||
else
|
||||
check_string_ps="$sshd_binary"
|
||||
fi
|
||||
|
||||
|
||||
# - Systemd supported ?
|
||||
# -
|
||||
systemd=$(which systemd)
|
||||
systemctl=$(which systemctl)
|
||||
|
||||
systemd_supported=false
|
||||
if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then
|
||||
systemd_supported=true
|
||||
fi
|
||||
|
||||
SSHD_SERVICE_FILE=
|
||||
SSHD_SYSV_INIT_SCRIPT=
|
||||
|
||||
if $systemd_supported ; then
|
||||
if systemctl -t service list-unit-files \
|
||||
| grep -e "^ssh" \
|
||||
| grep -q -E "(enabled|disabled)" 2> /devnull ; then
|
||||
|
||||
SSHD_SERVICE_FILE="$(systemctl -t service list-unit-files | grep -e "^ssh" | awk '{print$1}' | head -1)"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -z "$SSHD_SERVICE_FILE" ]]; then
|
||||
if [[ -x "/etc/init.d/ssh" ]]; then
|
||||
SSHD_SYSV_INIT_SCRIPT="/etc/init.d/ssh"
|
||||
elif [[ -x "/etc/init.d/sshd" ]]; then
|
||||
SSHD_SYSV_INIT_SCRIPT="/etc/init.d/sshd"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -z "$SSHD_SERVICE_FILE" ]] && [[ -z "$SSHD_SYSV_INIT_SCRIPT" ]] ; then
|
||||
fatal 'Neither an init-script nor a service file for SSH found!'
|
||||
fi
|
||||
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Check if SSH service is running
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
if $LOGGING ; then
|
||||
echo -e "\n Check if SSH service is running.."
|
||||
echo -e " ================================="
|
||||
fi
|
||||
if ! ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}" | grep -v grep > /dev/null ; then
|
||||
error "SSH service seems to be down! Trying to restart service now.."
|
||||
|
||||
if [[ -n "$SSHD_SERVICE_FILE" ]] ; 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 $SSHD_SERVICE_FILE > /dev/null 2> ${LOCK_DIR}/err_msg.log
|
||||
if [[ $? -ne 0 ]]; then
|
||||
error "$(cat ${LOCK_DIR}/err_msg.log)"
|
||||
fi
|
||||
sleep 10
|
||||
$systemctl start $SSHD_SERVICE_FILE > /dev/null 2> ${LOCK_DIR}/err_msg.log
|
||||
if [[ $? -ne 0 ]]; then
|
||||
error "$(cat ${LOCK_DIR}/err_msg.log)"
|
||||
fi
|
||||
else
|
||||
$SSHD_SYSV_INIT_SCRIPT stop > /dev/null 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
error "Stopping SSH service failed!"
|
||||
fi
|
||||
sleep 10
|
||||
$SSHD_SYSV_INIT_SCRIPT start > /dev/null 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
error "Starting SSH 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 SSH service failed!"
|
||||
else
|
||||
ok "SSH service is up and running."
|
||||
fi
|
||||
|
||||
else
|
||||
if $LOGGING ; then
|
||||
ok "SSH service is up and running."
|
||||
fi
|
||||
fi
|
||||
|
||||
rm -rf $LOCK_DIR
|
||||
exit 0
|
||||
|
||||
|
294
check_sympa_service.sh
Executable file
294
check_sympa_service.sh
Executable file
@ -0,0 +1,294 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
working_dir="$(dirname $(realpath $0))"
|
||||
|
||||
|
||||
# -------------
|
||||
# - Some Variables
|
||||
# -------------
|
||||
|
||||
LOCK_DIR="/tmp/$(basename $0).LOCK"
|
||||
|
||||
service_name="sympa"
|
||||
alert_email_arr="ckubu@oopen.de ckubu@gmx.de"
|
||||
sender_address="check_${service_name}@$(hostname -f)"
|
||||
|
||||
sympa_commands="
|
||||
/usr/local/sympa/bin/sympa_msg.pl
|
||||
/usr/local/sympa/bin/bulk.pl
|
||||
/usr/local/sympa/bin/archived.pl
|
||||
/usr/local/sympa/bin/bounced.pl
|
||||
/usr/local/sympa/bin/task_manager.pl
|
||||
"
|
||||
|
||||
check_string_ps="(/usr/local/sympa/bin/sympa_msg.pl|/usr/local/sympa/bin/bulk.pl|/usr/local/sympa/bin/archived.pl|/usr/local/sympa/bin/bounced.pl|/usr/local/sympa/bin/task_manager.pl)"
|
||||
|
||||
restart_needed=false
|
||||
|
||||
# -------------
|
||||
# - Job is already running?
|
||||
# -------------
|
||||
|
||||
# - If job already runs, stop execution..
|
||||
# -
|
||||
if mkdir "$LOCK_DIR" 2> /dev/null ; then
|
||||
|
||||
## - Remove lockdir when the script finishes, or when it receives a signal
|
||||
trap clean_up SIGHUP SIGINT SIGTERM
|
||||
|
||||
else
|
||||
|
||||
datum="$(date +"%d.%m.%Y %H:%M")"
|
||||
|
||||
msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\n Exiting now.."
|
||||
|
||||
echo ""
|
||||
echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running."
|
||||
echo ""
|
||||
echo -e " Exiting now.."
|
||||
echo ""
|
||||
|
||||
for _email in ${alert_email_arr[@]} ; do
|
||||
echo -e "To:${_email}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \
|
||||
| sendmail -F "Error `hostname -f`" -f $sender_address $_email
|
||||
done
|
||||
|
||||
exit 1
|
||||
|
||||
fi
|
||||
|
||||
|
||||
# -------------
|
||||
# - Some functions
|
||||
# -------------
|
||||
|
||||
clean_up() {
|
||||
|
||||
# Perform program exit housekeeping
|
||||
rm -rf "$LOCK_DIR"
|
||||
exit $1
|
||||
}
|
||||
|
||||
echononl(){
|
||||
if $terminal ; then
|
||||
echo X\\c > /tmp/shprompt$$
|
||||
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
|
||||
echo -e -n "$*\\c" 1>&2
|
||||
else
|
||||
echo -e -n "$*" 1>&2
|
||||
fi
|
||||
rm /tmp/shprompt$$
|
||||
fi
|
||||
}
|
||||
|
||||
fatal(){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
|
||||
echo ""
|
||||
echo -e " \033[31m\033[1mScript was interupted\033[m!"
|
||||
else
|
||||
echo " [ Fatal ]: $*"
|
||||
echo ""
|
||||
echo " Script was terminated...."
|
||||
fi
|
||||
echo ""
|
||||
clean_up 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[1mWarning\033[m ]: $*"
|
||||
else
|
||||
echo "[ Warning ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
info (){
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
|
||||
echo ""
|
||||
fi
|
||||
}
|
||||
|
||||
echo_done() {
|
||||
if $terminal ; then
|
||||
echo -e "\033[75G[ \033[32mdone\033[m ]"
|
||||
fi
|
||||
}
|
||||
echo_failed(){
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
|
||||
fi
|
||||
}
|
||||
echo_skipped() {
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# -------------
|
||||
# - Check some prerequisites
|
||||
# -------------
|
||||
|
||||
# - Running in a terminal?
|
||||
# -
|
||||
if [[ -t 1 ]] ; then
|
||||
terminal=true
|
||||
else
|
||||
terminal=false
|
||||
fi
|
||||
|
||||
# - Systemd supported ?
|
||||
# -
|
||||
systemd=$(which systemd)
|
||||
systemctl=$(which systemctl)
|
||||
|
||||
systemd_supported=false
|
||||
if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then
|
||||
systemd_supported=true
|
||||
fi
|
||||
|
||||
# - Check How to start/stop service
|
||||
# -
|
||||
SYSTEMD_UNIT_FILE=""
|
||||
SYSY_INIT_SCRIPT=""
|
||||
if $systemd_supported ; then
|
||||
SYSTEMD_UNIT_FILE="$(systemctl list-unit-files | grep -E "${service_name}[^@]*\.service" | grep "enabled" | cut -d' ' -f1)"
|
||||
fi
|
||||
|
||||
if [[ -z "$SYSTEMD_UNIT_FILE" ]]; then
|
||||
SYSY_INIT_SCRIPT="$(service --status-all | awk '{print$4}' | grep ${service_name} | head -1)"
|
||||
fi
|
||||
|
||||
if [[ -z "$SYSTEMD_UNIT_FILE" ]] && [[ -z "$SYSY_INIT_SCRIPT" ]] ; then
|
||||
fatal "Neither an init-script nor a service file for ${service_name} service found!"
|
||||
fi
|
||||
|
||||
|
||||
# -------------
|
||||
# - Main Part of Script
|
||||
# -------------
|
||||
|
||||
if $terminal ; then
|
||||
echo -e "\n Check if ${service_name} service is running.."
|
||||
echo -e " ==================================="
|
||||
fi
|
||||
|
||||
for _command in $sympa_commands ; do
|
||||
PIDS="$(ps ax | grep -E "${_command}" | grep -v grep | awk '{print$1}')"
|
||||
if [[ -z "$PIDS" ]]; then
|
||||
restart_needed=true
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if $restart_needed ;then
|
||||
|
||||
error "${service_name} service seems to be down. Try to (re)start .."
|
||||
|
||||
echononl " Stop service '${service_name}' first .."
|
||||
if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then
|
||||
systemctl stop $SYSTEMD_UNIT_FILE > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_done
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
else
|
||||
/etc/init.d/$SYSY_INIT_SCRIPT stop > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_done
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
fi
|
||||
|
||||
sleep 2
|
||||
|
||||
echononl " Start Service '${service_name}' .."
|
||||
if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then
|
||||
systemctl start $SYSTEMD_UNIT_FILE > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_done
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
else
|
||||
/etc/init.d/$SYSY_INIT_SCRIPT start > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_done
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
declare -i count=0
|
||||
|
||||
restart_sucessfully=false
|
||||
while [[ $count -lt 10 ]] && ! $restart_sucessfully ; do
|
||||
|
||||
((count++))
|
||||
|
||||
sleep 1
|
||||
|
||||
PIDS=""
|
||||
NEW_PIDS=""
|
||||
declare -i count_2=0
|
||||
for _command in $sympa_commands ; do
|
||||
|
||||
((count_2++))
|
||||
|
||||
PIDS="$(ps ax | grep -E "${_command}" | grep -v grep | awk '{print$1}')"
|
||||
if [[ -z "$PIDS" ]]; then
|
||||
continue
|
||||
else
|
||||
if [[ $count_2 -eq 1 ]] ; then
|
||||
NEW_PIDS="$PIDS"
|
||||
else
|
||||
NEW_PIDS="$NEW_PIDS $PIDS"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
restart_sucessfully=true
|
||||
break
|
||||
|
||||
done
|
||||
|
||||
if ! $restart_sucessfully ; then
|
||||
error "Restarting service '${service_name}' failed."
|
||||
else
|
||||
|
||||
info "Service '${service_name}' was restarted and is now up and running.
|
||||
The new PIDs are $NEW_PIDS"
|
||||
|
||||
if ! $terminal ; then
|
||||
echo ""
|
||||
echo "[ Success ]: Service '${service_name}' was restarted and is now up and running."
|
||||
echo " The new PIDs are $NEW_PIDS"
|
||||
echo ""
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
else
|
||||
info "Service ${service_name} is up and running."
|
||||
fi
|
||||
|
||||
clean_up 0
|
||||
|
219
check_vpn.sh
219
check_vpn.sh
@ -1,13 +1,214 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||
|
||||
if ! ps ax | grep openvpn | grep -v grep > /dev/null ; then
|
||||
echo -e "\n\tOpenVPN is not running. so i'm going to (re)start the vpn service..\n"
|
||||
/etc/init.d/openvpn stop
|
||||
kill -9 `cat /var/run/openvpn.server.pid 2>/dev/null` > /dev/null 2>&1
|
||||
rm -f /var/run/openvpn.server.pid
|
||||
/etc/init.d/openvpn start
|
||||
check_string_ps=""
|
||||
if [[ -f "/usr/sbin/openvpn" ]] ; then
|
||||
check_string_ps="/usr/sbin/openvpn"
|
||||
fi
|
||||
|
||||
exit 0;
|
||||
check_string_ps_plus="--daemon"
|
||||
|
||||
|
||||
# - used, if systemd is NOT supported
|
||||
init_script=""
|
||||
if [[ -x "/etc/init.d/openvpn" ]] ; then
|
||||
init_script="/etc/init.d/openvpn"
|
||||
fi
|
||||
|
||||
# - Used if systemd is supported
|
||||
# -
|
||||
service_name=openvpn
|
||||
|
||||
LOCK_DIR=`mktemp -d`
|
||||
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Base Function(s)
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
clean_up() {
|
||||
|
||||
# Perform program exit housekeeping
|
||||
rm -rf $LOCK_DIR
|
||||
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"
|
||||
}
|
||||
|
||||
# - The trap command allows you to execute a command when a signal
|
||||
# - is received by your script.
|
||||
# -
|
||||
trap clean_up SIGHUP SIGINT SIGTERM
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Check some prerequisites
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
# - Running in a terminal?
|
||||
# -
|
||||
if [[ -t 1 ]] ; then
|
||||
terminal=true
|
||||
LOGGING=true
|
||||
else
|
||||
terminal=false
|
||||
LOGGING=false
|
||||
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
|
||||
fatal "$(basename $0): OpenVPN Service 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 $init_script ]]; then
|
||||
fatal "$(basename $0): Missing Bind Init-Script!"
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Check if OpenVPN Service is running
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
if $LOGGING ; then
|
||||
echo -e "\n Check if OpenVPN Service is running.."
|
||||
echo -e " ====================================="
|
||||
fi
|
||||
|
||||
PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}" | grep "\ ${check_string_ps_plus}\ " | grep -v grep | awk '{print$2}')
|
||||
if [[ "X${PID}" = "X" ]]; then
|
||||
|
||||
error "OpenVPN Service seems to be down! Trying to restart service now.."
|
||||
|
||||
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
|
||||
$init_script stop > /dev/null 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
error "Stopping OpenVPN Service failed!"
|
||||
fi
|
||||
sleep 10
|
||||
$init_script start > /dev/null 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
error "Starting OpenVPN Service failed!"
|
||||
fi
|
||||
fi
|
||||
|
||||
declare -i counter=0
|
||||
PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}" | grep "\ ${check_string_ps_plus}\ " | grep -v grep | awk '{print$2}')
|
||||
while [[ "X${PID}" = "X" ]]; do
|
||||
sleep 1
|
||||
PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}" | grep "\ ${check_string_ps_plus}\ " | grep -v grep | awk '{print$2}')
|
||||
if [[ $counter -gt 10 ]]; then
|
||||
break
|
||||
else
|
||||
((counter++))
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ "X${PID}" = "X" ]] ; then
|
||||
error "Restarting OpenVPN Service failed!"
|
||||
else
|
||||
ok "OpenVPN Service is up and running."
|
||||
fi
|
||||
|
||||
else
|
||||
if $LOGGING ; then
|
||||
ok "OpenVPN Service is up and running."
|
||||
fi
|
||||
fi
|
||||
|
||||
clean_up
|
||||
|
File diff suppressed because it is too large
Load Diff
97
conf/check_cert_for_service.conf.sample
Normal file
97
conf/check_cert_for_service.conf.sample
Normal file
@ -0,0 +1,97 @@
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Settings for script check_cert_for_service.sh
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
|
||||
# - service_name
|
||||
# -
|
||||
# - Name of service.
|
||||
# -
|
||||
# - Note: this var will also be used to determin systemd service file
|
||||
# - or sysVinit script.
|
||||
# -
|
||||
# - Example:
|
||||
# - service_name="Mumble"
|
||||
# - service_name="Prosody"
|
||||
# -
|
||||
#service_name=""
|
||||
|
||||
|
||||
# - check_string_ps
|
||||
# -
|
||||
# - String wich (clearly) identifies the service at the process list (ps)
|
||||
# -
|
||||
# - Example:
|
||||
# - check_string_ps="[[:digit:]]\ /usr/sbin/murmurd"
|
||||
# - check_string_ps=""
|
||||
# -
|
||||
#check_string_ps=""
|
||||
|
||||
|
||||
# - service_user
|
||||
# -
|
||||
# - User under which the service is running.
|
||||
# -
|
||||
# - Example:
|
||||
# - service_user="mumble-server"
|
||||
# - service_user="prosody"
|
||||
# -
|
||||
#service_user=""
|
||||
|
||||
|
||||
# - service_group
|
||||
# -
|
||||
# - Group under which the service is running.
|
||||
# -
|
||||
# - Example:
|
||||
# - service_group="mumble-server"
|
||||
# - service_group="prosody"
|
||||
# -
|
||||
#service_user=""
|
||||
|
||||
|
||||
# - cert_installed
|
||||
# -
|
||||
# - Locataion of certificate read by service
|
||||
# -
|
||||
# - Example:
|
||||
# - cert_installed="/var/lib/mumble-server/fullchain.pem"
|
||||
# - cert_installed="/var/lib/dehydrated/certs/jabber.so36.net/fullchain.pem"
|
||||
# -
|
||||
#cert_installed=""
|
||||
|
||||
|
||||
# - key_installed
|
||||
# -
|
||||
# - Location of the key read by service
|
||||
# -
|
||||
# - Example:
|
||||
# - key_installed="/var/lib/mumble-server/privkey.pem"
|
||||
# - key_installed="/etc/prosody/certs/privkey_jabber.so36.pem"
|
||||
# -
|
||||
#key_installed=""
|
||||
|
||||
|
||||
# - cert_newest
|
||||
# -
|
||||
# - Location of the newest certificate.
|
||||
# -
|
||||
# - Example:
|
||||
# - cert_newest="/var/lib/dehydrated/certs/il-mumble.oopen.de/fullchain.pem"
|
||||
# - cert_newest="/var/lib/dehydrated/certs/jabber.so36.net/fullchain.pem"
|
||||
# -
|
||||
#cert_newest=""
|
||||
|
||||
|
||||
# - key_newest
|
||||
# -
|
||||
# - Location of the newest Key
|
||||
# -
|
||||
# - Example:
|
||||
# - key_newest="/var/lib/dehydrated/certs/il-mumble.oopen.de/privkey.pem"
|
||||
# - key_newest="/var/lib/dehydrated/certs/jabber.so36.net/privkey.pem"
|
||||
# -
|
||||
#key_newest=""
|
||||
|
58
conf/check_remote_websites.conf.sample
Normal file
58
conf/check_remote_websites.conf.sample
Normal file
@ -0,0 +1,58 @@
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Settings for script check_remote_websites.sh
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
# - WEBSITES_TO_CHECK
|
||||
# -
|
||||
# - Contains a list of websites, whichn will be checked
|
||||
# -
|
||||
# - blank seperated list of URLs
|
||||
# -
|
||||
# - Example: WEBSITES_TO_CHECK="
|
||||
# - https://webmail.oopen.de
|
||||
# - https://webmail.initiativenserver.de
|
||||
# - https://webmail.interventionistische-linke.org
|
||||
# - http://www.oopen.de"
|
||||
# -
|
||||
WEBSITES_TO_CHECK=""
|
||||
|
||||
# - TIME_OUT
|
||||
# -
|
||||
# - Timeout for curl request of each website
|
||||
# -
|
||||
# - Defaults to: TIME_OUT=240
|
||||
# -
|
||||
#TIME_OUT=240
|
||||
|
||||
|
||||
# ---
|
||||
# - E-Mail settings for sending script messages
|
||||
# ---
|
||||
|
||||
# - company
|
||||
# -
|
||||
# - Example: company="O.OPEN"
|
||||
# -
|
||||
company=""
|
||||
|
||||
# - sender_address
|
||||
# -
|
||||
# - Defaults to: sender_address="root@$(hostname -f)"
|
||||
# -
|
||||
#sender_address="check_websites@$(hostname -f)"
|
||||
|
||||
# - content_type
|
||||
# -
|
||||
# - Defaults to: content_type='Content-Type: text/plain;\n charset="utf-8"'
|
||||
# -
|
||||
#content_type='Content-Type: text/plain;\n charset="utf-8"'
|
||||
|
||||
# - alert_email_addresses
|
||||
# -
|
||||
# - blank separated list of e-mail addresses
|
||||
#
|
||||
# - Example: alert_email_addresses="ckubu@oopen.de axel@warenform.net"
|
||||
# -
|
||||
alert_email_addresses=""
|
@ -18,10 +18,34 @@
|
||||
# -
|
||||
check_load=true
|
||||
check_mysql=true
|
||||
|
||||
# - PostgreSQL
|
||||
# -
|
||||
# - NOT useful, if more than one PostgreSQL instances are running!
|
||||
# -
|
||||
check_postgresql=false
|
||||
|
||||
check_apache=true
|
||||
check_nginx=false
|
||||
check_php_fpm=true
|
||||
check_redis=false
|
||||
check_website=false
|
||||
|
||||
# - If service is not listen on 127.0.0.1/loclhost, curl check must
|
||||
# - be ommited
|
||||
# -
|
||||
# - Defaults to: ommit_curl_check_nginx=false
|
||||
# -
|
||||
#ommit_curl_check_nginx=false
|
||||
|
||||
# - Is this a vserver guest machine?
|
||||
# -
|
||||
# - Not VSerber guest host does not support systemd!
|
||||
# -
|
||||
# - defaults to: vserver_guest=false
|
||||
# -
|
||||
#vserver_guest=false
|
||||
|
||||
|
||||
# - Additional Settings for check_mysql
|
||||
# -
|
||||
|
240
remove_zombies_command.sh
Executable file
240
remove_zombies_command.sh
Executable file
@ -0,0 +1,240 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
working_dir="$(dirname $(realpath $0))"
|
||||
|
||||
# -------------
|
||||
# - Some Variables
|
||||
# -------------
|
||||
|
||||
LOCK_DIR="/tmp/$(basename $0).LOCK"
|
||||
|
||||
KILL_SIGNAL=HUP
|
||||
|
||||
CHECK_COMMANDS="$*"
|
||||
|
||||
|
||||
# -------------
|
||||
# - Some functions
|
||||
# -------------
|
||||
|
||||
usage() {
|
||||
|
||||
|
||||
[[ -n "$1" ]] && error "$1"
|
||||
|
||||
|
||||
[[ $terminal ]] && echo -e "
|
||||
\033[1mUsage:\033[m
|
||||
|
||||
$(basename $0) COMMAND [COMMAND] ..
|
||||
|
||||
\033[1mDescription\033[m
|
||||
|
||||
Searches for zombie (defunct) child prozesses from given command(s). If such
|
||||
processes exists, the script tries to kill them by sending a HUP signal to
|
||||
the parent process.
|
||||
|
||||
At least one command as a cammand line option must be gicen.
|
||||
|
||||
\033[1mExample:\033[m
|
||||
|
||||
Search for (and delete) defunct child prozesses of parent '/usr/local/php/bin/php-cgi'
|
||||
|
||||
$(basename $0) /usr/local/php/bin/php-cgi
|
||||
|
||||
Search for (and delete) defunct child prozesses of parent processes
|
||||
'/usr/local/sympa/bin/sympa_msg.pl' and '/usr/local/sympa/bin/bulk.pl'
|
||||
|
||||
$(basename $0) /usr/local/sympa/bin/sympa_msg.pl /usr/local/sympa/bin/bulk.pl
|
||||
|
||||
"
|
||||
|
||||
clean_up 1
|
||||
|
||||
}
|
||||
|
||||
clean_up() {
|
||||
|
||||
# Perform program exit housekeeping
|
||||
rm -rf "$LOCK_DIR"
|
||||
exit $1
|
||||
}
|
||||
|
||||
trim() {
|
||||
local var="$*"
|
||||
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
|
||||
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
|
||||
echo -n "$var"
|
||||
}
|
||||
|
||||
fatal(){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
|
||||
echo ""
|
||||
echo -e " \033[31m\033[1mScript was interupted\033[m!"
|
||||
else
|
||||
echo "[ Fatal ]: $*"
|
||||
echo ""
|
||||
echo " Script was terminated...."
|
||||
fi
|
||||
echo ""
|
||||
clean_up 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[1mWarning\033[m ]: $*"
|
||||
else
|
||||
echo "[ Warning ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
info (){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
|
||||
else
|
||||
echo "[ Info ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
info_terminal (){
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
|
||||
echo ""
|
||||
fi
|
||||
}
|
||||
|
||||
ok (){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[32m\033[1mOk\033[m ]: $*"
|
||||
else
|
||||
echo "[ Ok ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
|
||||
# -------------
|
||||
# - Check some prerequisites
|
||||
# -------------
|
||||
|
||||
# - Is this script running on terminal ?
|
||||
# -
|
||||
if [[ -t 1 ]] ; then
|
||||
terminal=true
|
||||
else
|
||||
terminal=false
|
||||
fi
|
||||
|
||||
|
||||
# - Print help?
|
||||
# -
|
||||
if [[ "$(trim $*)" = "-h" ]] || [[ "$(trim $*)" = "--help" ]] ; then
|
||||
usage
|
||||
fi
|
||||
|
||||
if [[ -z "$(which basename)" ]]; then
|
||||
fatal 'It seems "basename" is not installed, but needed!'
|
||||
fi
|
||||
|
||||
if [[ -z "$(which realpath)" ]]; then
|
||||
fatal 'It seems "realpath" is not installed, but needed!'
|
||||
fi
|
||||
|
||||
|
||||
if [[ -z "$CHECK_COMMANDS" ]]; then
|
||||
usage "At least one command must be given."
|
||||
else
|
||||
for _COMMAND in ${CHECK_COMMANDS[@]} ; do
|
||||
if [[ ! -x "$_COMMAND" ]]; then
|
||||
fatal "no binary/script '$_COMMAND' found on the server"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
|
||||
# -------------
|
||||
# - Jobhandling
|
||||
# -------------
|
||||
|
||||
# - If job already runs, stop execution..
|
||||
# -
|
||||
if mkdir "$LOCK_DIR" 2> /dev/null ; then
|
||||
|
||||
## - Remove lockdir when the script finishes, or when it receives a signal
|
||||
trap clean_up SIGHUP SIGINT SIGTERM
|
||||
|
||||
else
|
||||
|
||||
datum="$(date +"%d.%m.%Y %H:%M")"
|
||||
|
||||
fatal "A previos instance of that script \"$(basename $0)\" seems already be running."
|
||||
|
||||
exit 1
|
||||
|
||||
fi
|
||||
|
||||
|
||||
# -------------
|
||||
# Checking ..
|
||||
# -------------
|
||||
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
echo -e " \033[1mCheck for zombie (defunct) child prozesses\033[m"
|
||||
echo " =========================================="
|
||||
fi
|
||||
|
||||
for _COMMAND in ${CHECK_COMMANDS[@]} ; do
|
||||
|
||||
if $(ps -Ao"stat,command" | grep "$(basename ${_COMMAND})" | grep -v grep | grep -e '^[zZ]' > /dev/null 2>&1)
|
||||
then
|
||||
|
||||
warn "Found Zombie Processes '${_COMMAND}' on '$(hostname -f)':"
|
||||
|
||||
ps -Ao"stat,user,pid,ppid,command" | grep "$(basename ${_COMMAND})" | grep -v grep | grep -e '^[zZ]'
|
||||
|
||||
membefore=$(cat /proc/meminfo | grep Committed_AS | awk '{print $2}')
|
||||
|
||||
info "Trying a graceful kill to the concerning parents with signal '$KILL_SIGNAL' .."
|
||||
|
||||
# - Sending the kill signal
|
||||
# -
|
||||
ps -Ao"stat,pid,ppid,command" | grep "$(basename ${_COMMAND})" | grep -v grep | grep -e '^[zZ]' \
|
||||
| awk '{ print $3 }' | sort -u | xargs --no-run-if-empty kill -$KILL_SIGNAL
|
||||
|
||||
if [[ "$?" -eq 0 ]];then
|
||||
sleep 10
|
||||
ok "Cleaning up zombie processes '${_COMMAND}' seems succsessfully finisched"
|
||||
|
||||
memafter=`cat /proc/meminfo | grep Committed_AS | awk '{print $2}'`
|
||||
|
||||
info "`expr $membefore - $memafter` KB RAM has been freed"
|
||||
else
|
||||
error "Cleaning up zombie processes '${_COMMAND}' failed!"
|
||||
fi
|
||||
else
|
||||
info_terminal "No zombie prossses '${_COMMAND}' found."
|
||||
fi
|
||||
done
|
||||
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
fi
|
||||
clean_up 0
|
265
remove_zombies_user.sh
Executable file
265
remove_zombies_user.sh
Executable file
@ -0,0 +1,265 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
working_dir="$(dirname $(realpath $0))"
|
||||
|
||||
# -------------
|
||||
# - Some Variables
|
||||
# -------------
|
||||
|
||||
LOCK_DIR="/tmp/$(basename $0).LOCK"
|
||||
|
||||
KILL_SIGNAL=HUP
|
||||
|
||||
#CHECK_USER="$*"
|
||||
|
||||
|
||||
# -------------
|
||||
# - Some functions
|
||||
# -------------
|
||||
|
||||
usage() {
|
||||
|
||||
|
||||
[[ -n "$1" ]] && error "$1"
|
||||
|
||||
|
||||
[[ $terminal ]] && echo -e "
|
||||
\033[1mUsage:\033[m
|
||||
|
||||
$(basename $0) -u <user>
|
||||
|
||||
\033[1mDescription\033[m
|
||||
|
||||
Searches for zombie (defunct) child prozesses owned by a given user. If such
|
||||
processes exists, the script tries to kill them by sending a HUP signal to
|
||||
the parent process.
|
||||
|
||||
A user must be given using option '-u'.
|
||||
|
||||
\033[1mOptions\033[m
|
||||
|
||||
-h
|
||||
Prints this help.
|
||||
|
||||
-u <user>
|
||||
Searches for zombie (defunct) prozesses owned by this user.
|
||||
|
||||
\033[1mExample:\033[m
|
||||
|
||||
Search for (and delete) defunct child prozesses owned by user 'sympa''
|
||||
|
||||
$(basename $0) -u sympa
|
||||
|
||||
"
|
||||
|
||||
clean_up 1
|
||||
|
||||
}
|
||||
|
||||
clean_up() {
|
||||
|
||||
# Perform program exit housekeeping
|
||||
rm -rf "$LOCK_DIR"
|
||||
exit $1
|
||||
}
|
||||
|
||||
trim() {
|
||||
local var="$*"
|
||||
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
|
||||
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
|
||||
echo -n "$var"
|
||||
}
|
||||
|
||||
fatal(){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
|
||||
echo ""
|
||||
echo -e " \033[31m\033[1mScript was interupted\033[m!"
|
||||
else
|
||||
echo "[ Fatal ]: $*"
|
||||
echo ""
|
||||
echo " Script was terminated...."
|
||||
fi
|
||||
echo ""
|
||||
clean_up 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[1mWarning\033[m ]: $*"
|
||||
else
|
||||
echo "[ Warning ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
info (){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
|
||||
else
|
||||
echo "[ Info ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
info_terminal (){
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
|
||||
echo ""
|
||||
fi
|
||||
}
|
||||
|
||||
ok (){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[32m\033[1mOk\033[m ]: $*"
|
||||
else
|
||||
echo "[ Ok ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
|
||||
# -------------
|
||||
# - Check some prerequisites
|
||||
# -------------
|
||||
|
||||
# - Is this script running on terminal ?
|
||||
# -
|
||||
if [[ -t 1 ]] ; then
|
||||
terminal=true
|
||||
else
|
||||
terminal=false
|
||||
fi
|
||||
|
||||
if [[ -z "$(which basename)" ]]; then
|
||||
fatal 'It seems "basename" is not installed, but needed!'
|
||||
fi
|
||||
|
||||
if [[ -z "$(which realpath)" ]]; then
|
||||
fatal 'It seems "realpath" is not installed, but needed!'
|
||||
fi
|
||||
|
||||
|
||||
|
||||
# -------------
|
||||
# - Jobhandling
|
||||
# -------------
|
||||
|
||||
# - If job already runs, stop execution..
|
||||
# -
|
||||
if mkdir "$LOCK_DIR" 2> /dev/null ; then
|
||||
|
||||
## - Remove lockdir when the script finishes, or when it receives a signal
|
||||
trap clean_up SIGHUP SIGINT SIGTERM
|
||||
|
||||
else
|
||||
|
||||
datum="$(date +"%d.%m.%Y %H:%M")"
|
||||
|
||||
fatal "A previos instance of that script \"$(basename $0)\" seems already be running."
|
||||
|
||||
exit 1
|
||||
|
||||
fi
|
||||
|
||||
|
||||
# -------------
|
||||
# - Read Commanline Arguments
|
||||
# -------------
|
||||
|
||||
while getopts hu: opt ; do
|
||||
case $opt in
|
||||
h) usage ;;
|
||||
u) CHECK_USER=$OPTARG ;;
|
||||
*) usage
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ -z "$CHECK_USER" ]]; then
|
||||
usage "No user (option -u) given"
|
||||
fi
|
||||
|
||||
shift $(expr $OPTIND - 1)
|
||||
[[ "$#" -gt 0 ]] && usage "Wrong number of arguments given!"
|
||||
|
||||
|
||||
# -------------
|
||||
# Checking ..
|
||||
# -------------
|
||||
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
echo -e " \033[1mCheck for zombie (defunct) child prozesses owned by a given user\033[m"
|
||||
echo " ================================================================"
|
||||
fi
|
||||
|
||||
|
||||
if $(ps -Ao"stat,user,command" | grep "${CHECK_USER}" | grep -v grep | grep -e '^[zZ]' > /dev/null 2>&1)
|
||||
then
|
||||
|
||||
warn "Found Zombie Processes '${CHECK_USER}' on '$(hostname -f)':"
|
||||
|
||||
# - Print out the concerning lines frp 'ps' output.
|
||||
# -
|
||||
ps -Ao"stat,user,pid,ppid,command" | grep "$(basename ${CHECK_USER})" | grep -v grep | grep -e '^[zZ]'
|
||||
|
||||
membefore=$(cat /proc/meminfo | grep Committed_AS | awk '{print $2}')
|
||||
|
||||
info "Trying a graceful kill to the concerning parents with signal '$KILL_SIGNAL' .."
|
||||
|
||||
# - Process ID(s) of the zombie process(es)
|
||||
# -
|
||||
ZOMBIE_PIDS="$(ps -Ao"stat,user,pid,ppid,command" | grep "${CHECK_USER}" | grep -v grep | grep -e '^[zZ]' \
|
||||
| awk '{ print $3 }' | sort -u)"
|
||||
|
||||
# - Sending the kill signal ($KILL_SIGNAL) to the parent process ID(s)
|
||||
# -
|
||||
ps -Ao"stat,user,pid,ppid,command" | grep "${CHECK_USER}" | grep -v grep | grep -e '^[zZ]' \
|
||||
| awk '{ print $4 }' | sort -u | xargs --no-run-if-empty kill -$KILL_SIGNAL
|
||||
|
||||
if [[ "$?" -eq 0 ]];then
|
||||
|
||||
declare -i count=0
|
||||
for _PID in $ZOMBIE_PIDS ; do
|
||||
if [[ $count -eq 1 ]]; then
|
||||
KILLED_PIDS="$_PID"
|
||||
else
|
||||
KILLED_PIDS="$KILLED_PIDS $_PID"
|
||||
fi
|
||||
((count++))
|
||||
done
|
||||
|
||||
sleep 10
|
||||
|
||||
ok "Cleaning up zombie processes owned by user '${CHECK_USER}' seems succsessfully finisched"
|
||||
|
||||
ok "$count zombie process(es) with PID(s) '${KILLED_PIDS}' removed from process list"
|
||||
|
||||
memafter=`cat /proc/meminfo | grep Committed_AS | awk '{print $2}'`
|
||||
|
||||
info "`expr $membefore - $memafter` KB RAM has been freed"
|
||||
else
|
||||
error "Cleaning up zombie processes owned by user '${CHECK_USER}' failed!"
|
||||
fi
|
||||
else
|
||||
info_terminal "No zombie prossses owned by user '${CHECK_USER}' found."
|
||||
fi
|
||||
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
fi
|
||||
clean_up 0
|
Loading…
Reference in New Issue
Block a user