diff --git a/.gitignore b/.gitignore index 7c9b3a5..a3b58e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -check_webservice_load.conf -BAK/* +*.swp +/conf/*.conf +/BAK/* diff --git a/check_zombies_php-cgi.sh b/OLD/check_zombies_php-cgi.sh similarity index 100% rename from check_zombies_php-cgi.sh rename to OLD/check_zombies_php-cgi.sh diff --git a/check_amavis.pl b/check_amavis.pl index 1797bd9..2aba967 100755 --- a/check_amavis.pl +++ b/check_amavis.pl @@ -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 # - diff --git a/check_cert_for_service.sh b/check_cert_for_service.sh new file mode 100755 index 0000000..2007c7e --- /dev/null +++ b/check_cert_for_service.sh @@ -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 + diff --git a/check_dhcp.sh b/check_dhcp.sh new file mode 100755 index 0000000..3b4de0f --- /dev/null +++ b/check_dhcp.sh @@ -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 diff --git a/check_dns.sh b/check_dns.sh index c505b85..79045e0 100755 --- a/check_dns.sh +++ b/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; diff --git a/check_dyndns.sh b/check_dyndns.sh index c7fdd4a..e094c16 100755 --- a/check_dyndns.sh +++ b/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; diff --git a/check_nginx_service.sh b/check_nginx_service.sh new file mode 100755 index 0000000..9277edb --- /dev/null +++ b/check_nginx_service.sh @@ -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 + diff --git a/check_ntpd.sh b/check_ntpd.sh index 4307ea4..744adf5 100755 --- a/check_ntpd.sh +++ b/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 diff --git a/check_postfix.sh b/check_postfix.sh new file mode 100755 index 0000000..2629e21 --- /dev/null +++ b/check_postfix.sh @@ -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 + + diff --git a/check_remote_websites.sh b/check_remote_websites.sh new file mode 100755 index 0000000..f320f23 --- /dev/null +++ b/check_remote_websites.sh @@ -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 diff --git a/check_samba4_service.sh b/check_samba4_service.sh index 2a57f9f..16c6439 100755 --- a/check_samba4_service.sh +++ b/check_samba4_service.sh @@ -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 diff --git a/check_service.sh b/check_service.sh new file mode 100755 index 0000000..e9d1995 --- /dev/null +++ b/check_service.sh @@ -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 diff --git a/check_ssh.sh b/check_ssh.sh new file mode 100755 index 0000000..5485b16 --- /dev/null +++ b/check_ssh.sh @@ -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 + + diff --git a/check_sympa_service.sh b/check_sympa_service.sh new file mode 100755 index 0000000..900437d --- /dev/null +++ b/check_sympa_service.sh @@ -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 + diff --git a/check_vpn.sh b/check_vpn.sh index 0467e2f..f0d644d 100755 --- a/check_vpn.sh +++ b/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 diff --git a/check_webservice_load.sh b/check_webservice_load.sh index dfae912..ec663cb 100755 --- a/check_webservice_load.sh +++ b/check_webservice_load.sh @@ -10,6 +10,20 @@ conf_file="${working_dir}/conf/check_webservice_load.conf" #----------------------------- #--------------------------------------- +# - Defaults +# - +check_load=false +check_mysql=false +check_postgresql=false +check_apache=false +check_nginx=false +check_php_fpm=false +check_redis=false +check_website=false + +ommit_curl_check_nginx=false +vserver_guest=false + if [[ ! -f "$conf_file" ]]; then echo "" echo -e " [ Fatal ] Configuration file '$(basename ${conf_file})' not found!" @@ -40,34 +54,53 @@ LOCK_DIR=`mktemp -d` fatal(){ echo "" - echo -e " [ Fatal ] $*" + if $terminal ; then + echo -e " [ \033[31m\033[1mFatal\033[m ] $*" + else + echo -e " [ Fatal ] $*" + fi echo "" - echo -e "\tScript terminated.." - echo "" - exit 1 + rm -rf $LOCK_DIR + exit 1 } error (){ echo "" - echo -e " [ Error ] $*" + if $terminal ; then + echo -e " [ \033[31m\033[1mError\033[m ] $*" + else + echo " [ Error ] $*" + fi echo "" } warn (){ echo "" - echo -e " [ Warn ] $*" + if $terminal ; then + echo -e " [ \033[33m\033[1mWarn\033[m ] $*" + else + echo " [ Warn ] $*" + fi echo "" } info (){ echo "" - echo -e " [ Info ] $*" + if $terminal ; then + echo -e " [ \033[32m\033[1mInfo\033[m ] $*" + else + echo " [ Info ] $*" + fi echo "" } ok (){ echo "" - echo -e " [ Ok ] $*" + if $terminal ; then + echo -e " [ \033[32m\033[1mOk\033[m ] $*" + else + echo " [ Ok ] $*" + fi echo "" } @@ -86,8 +119,8 @@ trim() { #--------------------------------------- if [[ -t 1 ]] ; then - terminal=true - LOGGING=true + terminal=true + LOGGING=true else terminal=false LOGGING=false @@ -113,6 +146,12 @@ if [ -z "`which realpath`" ]; then fatal 'It seems "realpath" is not installed, but needed!' fi +if $check_apache || $check_nginxA ; then + if [ -z "$(which curl)" ]; then + fatal 'It seems "curl" is not installed, but needed!' + fi +fi + if $check_load ; then if [ -z "`which bc`" ]; then fatal 'It seems "bc" is not installed, but needed!' @@ -121,12 +160,16 @@ fi # - Systemd supported ? ## - -systemd=$(which systemd) -systemctl=$(which systemctl) - systemd_supported=false -if [[ -n "$systemd" ]] && [[ -n "$systemd" ]] ; then - systemd_supported=true + +if ! $vserver_guest ; then + + systemd=$(which systemd) + systemctl=$(which systemctl) + + if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then + systemd_supported=true + fi fi @@ -141,12 +184,52 @@ if $check_mysql ; then fi fi - MYSQLD_SAFE=`realpath $(which mysqld_safe) 2>/dev/null` - if [ -z "$MYSQLD_SAFE" ]; then - if [ -x "/usr/local/mysql/bin/mysqld_safe" ]; then - MYSQLD_SAFE=`realpath "/usr/local/mysql/bin/mysqld_safe"` - else - fatal 'Command \"mysqld_safe\" not found!' + MYSQL_INIT_SCRIPT="" + MYSQLD_SERVICE_FILE="" + if $systemd_supported ; then + # - Is Service exclusive controlled by systemd + # - + if systemctl -t service list-unit-files \ + | grep -e "^mysql" \ + | grep -q -E "(enabled|disabled)" 2> /devnull ; then + + MYSQLD_SERVICE_FILE=$(systemctl -t service list-unit-files \ + | grep -e "^mysql" \ + | awk '{print$1}'\ + | head -1) + fi + + fi + + if [[ -z "$MYSQLD_SERVICE_FILE" ]] ; then + if [ -x "$(realpath /etc/init.d/mysql.server)" ]; then + MYSQL_INIT_SCRIPT="/etc/init.d/mysql.server" + elif [ -x "$(realpath /etc/init.d/mysql)" ]; then + MYSQL_INIT_SCRIPT="/etc/init.d/mysql" + fi + fi + + if [[ -z "$MYSQL_INIT_SCRIPT" ]] && [[ -z "$MYSQLD_SERVICE_FILE" ]]; then + fatal 'Neither an init-script nor a service file for MySQL found!' + elif [[ -n "$MYSQL_INIT_SCRIPT" ]] ; then + MYSQL_PS_CHECK_STRING="$(dirname "$MYSQLD")/(mysqld_safe |$(basename "$MYSQLD") )" + #MYSQL_PS_CHECK_STRING="(bin/mysqld_safe |bin/mysqld )" + else + MYSQL_PS_CHECK_STRING="$MYSQLD" + fi + + if [[ -z "$MYSQL_PS_CHECK_STRING" ]]; then + fatal "Cannot determin a check string for output of command 'ps ax'" + fi + + if [[ -n "$MYSQL_INIT_SCRIPT" ]] ; then + MYSQLD_SAFE=`realpath $(which mysqld_safe) 2>/dev/null` + if [ -z "$MYSQLD_SAFE" ]; then + if [ -x "/usr/local/mysql/bin/mysqld_safe" ]; then + MYSQLD_SAFE=`realpath "/usr/local/mysql/bin/mysqld_safe"` + else + fatal 'Command \"mysqld_safe\" not found!' + fi fi fi @@ -159,13 +242,33 @@ if $check_mysql ; then fi fi - MYSQL_INIT_SCRIPT="" - if [ -x "/etc/init.d/mysql.server" ]; then - MYSQL_INIT_SCRIPT="/etc/init.d/mysql.server" - elif [ -x "/etc/init.d/mysql" ]; then - MYSQL_INIT_SCRIPT="/etc/init.d/mysql" - else - fatal 'Cannot find init-script for MySQL server!' +fi + +if $check_postgresql ; then + + PSQL_SERVICE_FILE="" + PSQL_INIT_SCRIPT="" + if $systemd_supported ; then + # - Is Service exclusive controlled by systemd + # - + if systemctl -t service list-unit-files \ + | grep -e "^ postgresql" \ + | grep -q -E "(enabled|disabled)" 2> /devnull ; then + + PSQL_SERVICE_FILE=$(systemctl -t service list-unit-files \ + | grep -e "^postgresql" \ + | awk '{print$1}') + fi + fi + + if [[ -z "$PSQL_SERVICE_FILE" ]] ; then + if [ -x "/etc/init.d/postgresql" ]; then + PSQL_INIT_SCRIPT="/etc/init.d/postgresql" + fi + fi + + if [[ -z "$PSQL_INIT_SCRIPT" ]] && [[ -z "$PSQL_SERVICE_FILE" ]] ; then + fatal "Neither an init-script nor a service file for 'postgesql' databaseservice found!" fi fi @@ -184,14 +287,73 @@ if $check_apache ; then fi fi + APACHE_SERVICE_FILE="" APACHE_INIT_SCRIPT="" - if [ -x "/etc/init.d/apache2" ]; then - APACHE_INIT_SCRIPT="/etc/init.d/apache2" - elif [ -x "/etc/init.d/apachectl" ]; then - APACHE_INIT_SCRIPT="/etc/init.d/apachectl" - else - fatal 'Cannot find init-script for Apache web server!' + if $systemd_supported ; then + # - Is Service exclusive controlled by systemd + # - + if systemctl -t service list-unit-files \ + | grep -e "^apache" \ + | grep -q -E "(enabled|disabled)" 2> /devnull ; then + + APACHE_SERVICE_FILE=$(systemctl -t service list-unit-files \ + | grep -e "^apache" \ + | awk '{print$1}') + + fi fi + + if [[ -z "$APACHE_SERVICE_FILE" ]] ; then + if [ -x "/etc/init.d/apache2" ]; then + APACHE_INIT_SCRIPT="/etc/init.d/apache2" + elif [ -x "/etc/init.d/apachectl" ]; then + APACHE_INIT_SCRIPT="/etc/init.d/apachectl" + fi + fi + + if [[ -z "$APACHE_INIT_SCRIPT" ]] && [[ -z "$APACHE_SERVICE_FILE" ]] ; then + fatal 'Neither an init-script nor a service file for apache2 found!' + fi +fi + +if $check_nginx ; then + + NGINXD="$(realpath $(which nginx))" + if [ -z "$NGINXD" ]; then + if [ -x "/usr/local/nginx/bin/nginx" ]; then + NGINXD="$(realpath "/usr/local/nginx/bin/nginx")" + elif [ -x "/usr/local/nginx/sbin/nginx" ]; then + NGINXD="$(realpath "/usr/local/nginx/sbin/nginx")" + else + fatal 'Command \"nginx\" not found!' + fi + fi + + NGINX_SERVICE_FILE="" + NGINX_INIT_SCRIPT="" + if $systemd_supported ; then + # - Is Service exclusive controlled by systemd + # - + if systemctl -t service list-unit-files \ + | grep -e "^nginx" \ + | grep -q -E "(enabled|disabled)" 2> /devnull ; then + + NGINX_SERVICE_FILE=$(systemctl -t service list-unit-files \ + | grep -e "^nginx" \ + | awk '{print$1}') + fi + fi + + if [[ -z "$NGINX_SERVICE_FILE" ]] ; then + if [ -x "/etc/init.d/nginx" ]; then + NGINX_INIT_SCRIPT="/etc/init.d/nginx" + fi + fi + + if [[ -z "$NGINX_INIT_SCRIPT" ]] && [[ -z "$NGINX_SERVICE_FILE" ]] ; then + fatal "Neither an init-script nor a service file for 'nginx' webservice found!" + fi + fi declare -A start_stop_method_php_fpm @@ -269,6 +431,44 @@ if $check_php_fpm ; then done fi +if $check_redis ; then + + REDISD="$(which redis-server)" + if [ -z "$REDISD" ]; then + if [ -x "/usr/sbin/redis-server" ]; then + REDISD="/usr/sbin/redis-server" + else + fatal 'Command \"redis-server\" not found!' + fi + fi + + REDIS_SERVICE_FILE="" + REDIS_INIT_SCRIPT="" + if $systemd_supported ; then + # - Is Service exclusive controlled by systemd + # - + if systemctl -t service list-unit-files \ + | grep -e "^redis-server" \ + | grep -q -E "(enabled|disabled)" 2> /devnull ; then + + REDIS_SERVICE_FILE=$(systemctl -t service list-unit-files \ + | grep -e "^redis-server" \ + | awk '{print$1}') + fi + fi + + if [[ -z "$REDIS_SERVICE_FILE" ]] ; then + if [ -x "/etc/init.d/redis-server" ]; then + REDIS_INIT_SCRIPT="/etc/init.d/redis-server" + fi + fi + + if [[ -z "$REDIS_INIT_SCRIPT" ]] && [[ -z "$REDIS_SERVICE_FILE" ]] ; then + fatal "Neither an init-script nor a service file for 'nginx' webservice found!" + fi + +fi + if $check_website ; then if [[ -n "$php_version_of_working_url" ]] && [[ "$php_version_of_working_url" != "None" ]] ; then @@ -363,7 +563,11 @@ stop_apache() { ## - Stop Apache Webservice ## - - $APACHE_INIT_SCRIPT stop > /dev/null 2>&1 + if [[ -n "$APACHE_SERVICE_FILE" ]] ; then + systemctl stop "$APACHE_SERVICE_FILE" > /dev/null 2>&1 + else + $APACHE_INIT_SCRIPT stop > /dev/null 2>&1 + fi sleep 12 @@ -414,6 +618,88 @@ stop_apache() { i=i+1 done +} + +stop_nginx() { + + send_msg=$1 + send_msg=${send_msg:=true} + + ## - Stop Nginx Webservice + ## - + if [[ -n "$NGINX_SERVICE_FILE" ]] ; then + systemctl stop "$NGINX_SERVICE_FILE" > /dev/null 2>&1 + else + $NGINX_INIT_SCRIPT stop > /dev/null 2>&1 + fi + + sleep 12 + + declare -i i=0 + PIDS=$(ps aux | grep "$NGINXD" | grep -v grep | awk '{print$2}') + + while [ "X$PIDS" != "X" ]; do + + if [ $i -eq 0 ]; then + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Error ]: Stopping Nginx Webservice failed !!!" >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Info ]; Going to kill remaining nginx-processes.." >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + + error "Stopping Nginx Webservice failed !!!" + info "Going to kill remaining nginx-processes.." + fi + + if [ $i -gt 10 ]; then + + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Error ]: Killing remaining nginx-processes failed !!!" >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + + error "Killing remaining nginx-processes failed !!!" + + if $send_msg ; then + + subject="[ Error ]: Stopping Nginx Webservice on `hostname -f` failed -- $datum" + msg="\n\n`cat $LOCK_DIR/extra_msg.txt`\n" + + for _to_address in $to_addresses ; do + echo -e "To:${_to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \ + | /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $_to_address + done + + fi + + break + fi + + for _PID in $PIDS ; do + kill $_PID > /dev/null 2>&1 + done + + sleep 2 + PIDS=$(ps aux | grep "$NGINXD" | grep -v grep | awk '{print$2}') + i=i+1 + done + + SUB_PIDS=$(ps aux | grep "$(basename $NGINXD):" | grep -v grep | awk '{print$2}') + + if [[ "X${SUB_PIDS}X" != 'XX' ]]; then + + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Error ]: Found processes '$(basename $NGINXD):'." >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Info ]; Going to kill remaining nginx-processes.." >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + + error "Found processes '$(basename $NGINXD):'." + info "Going to kill processes '$(basename $NGINXD):'.." + + for _PID in $SUB_PIDS ; do + kill $_PID > /dev/null 2>&1 + done + fi } @@ -424,14 +710,18 @@ stop_mysql() { ## - Stop MySQL Service ## - - $MYSQL_INIT_SCRIPT stop > /dev/null 2>&1 + if [[ -n "$MYSQL_INIT_SCRIPT" ]]; then + $MYSQL_INIT_SCRIPT stop > /dev/null 2>&1 + else + systemctl stop "$MYSQLD_SERVICE_FILE" > /dev/null 2>&1 + fi _msg=false - sleep 2 + sleep 5 declare -i i=0 - PIDS=$(ps aux | grep "$MYSQLD_SAFE" | grep -v grep | awk '{print$2}') + PIDS="$(ps aux | grep -E "${MYSQL_PS_CHECK_STRING}" | grep -v grep | awk '{print$2}')" while [ "X$PIDS" != "X" ]; do if [ $i -eq 0 ]; then @@ -445,13 +735,13 @@ stop_mysql() { info "Going to kill MySQL Processes (mysqld_safe).." fi - if [ $i -gt 20 ]; then + if [ $i -gt 10 ]; then echo "" >> $LOCK_DIR/extra_msg.txt - echo -e "\t[ Error ]: Killing MySQL Processes (mysqld_safe) failed !!!" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Error ]: Killing MySQL Processes failed !!!" >> $LOCK_DIR/extra_msg.txt echo "" >> $LOCK_DIR/extra_msg.txt - error "Killing MySQL Processes (mysqld_safe) failed !!!" + error "Killing MySQL Processes failed !!!" _msg=true @@ -459,52 +749,15 @@ stop_mysql() { fi for _PID in $PIDS ; do - kill -9 $_PID > /dev/null 2>&1 + kill -15 $_PID > /dev/null 2>&1 done - sleep 2 + sleep 5 - PIDS=$(ps aux | grep "$MYSQLD_SAFE" | grep -v grep | awk '{print$2}') + PIDS="$(ps aux | grep -E "${MYSQL_PS_CHECK_STRING}" | grep -v grep | awk '{print$2}')" i=i+1 done - PIDS=$(ps aux | grep "$MYSQLD" | grep -v grep | awk '{print$2}') - declare -i i=0 - while [ "X$PIDS" != "X" ]; do - - if [ $i -eq 0 ]; then - echo "" >> $LOCK_DIR/extra_msg.txt - echo -e "\t[ Error ]: MySQL Processes (mysqld) still exists !!!" >> $LOCK_DIR/extra_msg.txt - echo "" >> $LOCK_DIR/extra_msg.txt - echo -e "\t[ Info ]; Going to kill remaining MySQL Processes (mysqld).." >> $LOCK_DIR/extra_msg.txt - echo "" >> $LOCK_DIR/extra_msg.txt - - error "MySQL Processes (mysqld) still exists !!!" - info "Going to kill remaining MySQL Processes (mysqld).." - fi - - if [ $i -gt 20 ]; then - - echo "" >> $LOCK_DIR/extra_msg.txt - echo -e "\t[ Error ]: Killing remaining MySQL Processes (mysqld) failed !!!" >> $LOCK_DIR/extra_msg.txt - echo "" >> $LOCK_DIR/extra_msg.txt - - error "Killing remaining MySQL Processes (mysqld) failed !!!" - - _msg=true - - break - fi - - for _PID in $PIDS ; do - kill -9 $_PID > /dev/null 2>&1 - done - sleep 2 - PIDS=i$(ps aux | grep "$MYSQLD" | grep -v grep | awk '{print$2}') - i=i+1 - - done - if $send_msg && $_msg ; then subject="[ Error ]: Stopping MySQL Engine on `hostname -f` failed -- $datum" msg="\n\n`cat $LOCK_DIR/extra_msg.txt`\n" @@ -516,6 +769,91 @@ stop_mysql() { fi } +stop_postgresql() { + + send_msg=$1 + send_msg=${send_msg:=true} + + ## - Stop PostgreSQL database service + ## - + if [[ -n "$PSQL_SERVICE_FILE" ]] ; then + systemctl stop "$PSQL_SERVICE_FILE" > /dev/null 2>&1 + else + $PSQL_INIT_SCRIPT stop > /dev/null 2>&1 + fi + + sleep 12 + + declare -i i=0 + + CHILD_PID="$(ps aux | grep -E "\ postgres:\ " | grep -v grep | tail -n 1 | awk '{print$2}')" + PSQL_MAIN_PID="$(ps -o ppid= -p $CHILD_PID 2> /dev/null)" + + while [ "X$PSQL_MAIN_PID" != "X" ]; do + + if [ $i -eq 0 ]; then + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Error ]: Stopping PostgreSQL database service failed !!!" >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Info ]; Going to kill remaining postgres-processes.." >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + + error "Stopping PostgreSQL database service failed !!!" + info "Going to kill remaining postgres-processes.." + fi + + if [ $i -gt 10 ]; then + + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Error ]: Killing remaining postgres-processes failed !!!" >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + + error "Killing remaining postgres-processes failed !!!" + + if $send_msg ; then + + subject="[ Error ]: Stopping PostgreSQL database service on `hostname -f` failed -- $datum" + msg="\n\n`cat $LOCK_DIR/extra_msg.txt`\n" + + for _to_address in $to_addresses ; do + echo -e "To:${_to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \ + | /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $_to_address + done + + fi + + break + fi + + kill $PSQL_MAIN_PID > /dev/null 2>&1 + + sleep 2 + + CHILD_PID="$(ps aux | grep -E "\ postgres:\ " | grep -v grep | tail -n 1 | awk '{print$2}')" + PSQL_MAIN_PID="$(ps -o ppid= -p $CHILD_PID)" + i=i+1 + done + + CHILD_PIDS=$(ps aux | grep -E "postgres:" | grep -v grep | awk '{print$2}') + + if [[ "X${CHILD_PIDS}X" != 'XX' ]]; then + + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Error ]: Found processes 'postgres:'." >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Info ]; Going to kill remaining postgres-processes.." >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + + error "Found processes 'postgres:'." + info "Going to kill processes 'postgres:'.." + + for _PID in $CHILD_PIDS ; do + kill $_PID > /dev/null 2>&1 + done + fi + +} + stop_php_fpm() { __version=$1 @@ -633,15 +971,84 @@ stop_php_fpm() { } +stop_redis() { + + send_msg=$1 + send_msg=${send_msg:=true} + + ## - Stop Redis Caching Service + ## - + if [[ -n "$REDIS_SERVICE_FILE" ]] ; then + systemctl stop "$REDIS_SERVICE_FILE" > /dev/null 2>&1 + else + $REDIS_INIT_SCRIPT stop > /dev/null 2>&1 + fi + + sleep 12 + + declare -i i=0 + PIDS=$(ps aux | grep "$REDISD" | grep -v grep | awk '{print$2}') + + while [ "X$PIDS" != "X" ]; do + + if [ $i -eq 0 ]; then + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Error ]: Stopping Redis Caching Sservice failed !!!" >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Info ]; Going to kill remaining 'redis-server'-processes.." >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + + error "Stopping Redis Caching Service failed !!!" + info "Going to kill remaining 'redis-server'-processes.." + fi + + if [ $i -gt 10 ]; then + + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Error ]: Killing remaining 'redis-server'-processes failed !!!" >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + + error "Killing remaining 'redis-server'-processes failed !!!" + + if $send_msg ; then + + subject="[ Error ]: Stopping Redis Caching Service on `hostname -f` failed -- $datum" + msg="\n\n`cat $LOCK_DIR/extra_msg.txt`\n" + + for _to_address in $to_addresses ; do + echo -e "To:${_to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \ + | /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $_to_address + done + + fi + + break + fi + + for _PID in $PIDS ; do + kill -9 $_PID > /dev/null 2>&1 + done + sleep 2 + PIDS=$(ps aux | grep "$REDISD" | grep -v grep | awk '{print$2}') + i=i+1 + done + +} + start_mysql() { send_msg=$1 send_msg=${send_msg:=true} - $MYSQL_INIT_SCRIPT start > /dev/null 2>&1 - sleep 2 + if [[ -n "$MYSQL_INIT_SCRIPT" ]]; then + $MYSQL_INIT_SCRIPT start > /dev/null 2>&1 + else + systemctl start "$MYSQLD_SERVICE_FILE" > /dev/null 2>&1 + fi - NEWPID=$(ps aux | grep "$MYSQLD_SAFE" | grep -v grep | grep root | awk '{print$2}') + sleep 5 + + NEWPID="$(ps aux | grep -E "${MYSQL_PS_CHECK_STRING}" | grep -v grep | awk '{print$2}' | head -1) " if [ "X${NEWPID}X" = "XX" ]; then @@ -682,12 +1089,77 @@ start_mysql() { fi } +start_postgresql() { + + send_msg=$1 + send_msg=${send_msg:=true} + + + ## - Start PostgreSQL Webservice + ## - + if [[ -n "$PSQL_SERVICE_FILE" ]] ; then + systemctl start "$PSQL_SERVICE_FILE" > /dev/null 2>&1 + else + $PSQL_INIT_SCRIPT start > /dev/null 2>&1 + fi + sleep 2 + + CHILD_PID="$(ps aux | grep -E "\ postgres:\ " | grep -v grep | tail -n 1 | awk '{print$2}')" + NEWPID="$(ps -o ppid= -p $CHILD_PID 2> /dev/null)" + + if [ "X${NEWPID}X" = "XX" ]; then + + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Error ]: I started the 'PostgreSQL' database server, but service is not running !!!" >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + + error "I started the 'PostgreSQL' database server, but service is not running !!!" + + datum=`date +"%d.%m.%Y %H:%Mh"` + + subject="[ Error ]: PostgreSQL database service on `hostname -f` NOT RUNNING -- $datum" + msg="\n\n`cat $LOCK_DIR/extra_msg.txt`\n" + + for _to_address in $to_addresses ; do + echo -e "To:${_to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \ + | /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $_to_address + done + + else + + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\tI started the 'PostgreSQL' database server. the new PID is $NEWPID" >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + + ok "I started the 'PostgreSQL' database server. the new PID is $NEWPID" + + fi + + if $send_msg ; then + datum=`date +"%d.%m.%Y %H:%Mh"` + subject="Restarting PostgreSQL on `hostname -f` invoked -- $datum" + msg=`cat $LOCK_DIR/extra_msg.txt` + + for _to_address in $to_addresses ; do + echo -e "To:${_to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \ + | /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $_to_address + done + fi +} + start_apache() { send_msg=$1 send_msg=${send_msg:=true} - $APACHE_INIT_SCRIPT start > /dev/null 2>&1 + + ## - Start Apache Webservice + ## - + if [[ -n "$APACHE_SERVICE_FILE" ]] ; then + systemctl start "$APACHE_SERVICE_FILE" > /dev/null 2>&1 + else + $APACHE_INIT_SCRIPT start > /dev/null 2>&1 + fi sleep 2 NEWPID=$(ps aux | grep "$HTTPD" | grep -v grep | grep root | awk '{print$2}') @@ -732,6 +1204,63 @@ start_apache() { fi } +start_nginx() { + + send_msg=$1 + send_msg=${send_msg:=true} + + + ## - Start Nginx Webservice + ## - + if [[ -n "$NGINX_SERVICE_FILE" ]] ; then + systemctl start "$NGINX_SERVICE_FILE" > /dev/null 2>&1 + else + $NGINX_INIT_SCRIPT start > /dev/null 2>&1 + fi + sleep 2 + + NEWPID=$(ps aux | grep "$NGINXD" | grep -v grep | grep root | awk '{print$2}') + + if [ "X${NEWPID}X" = "XX" ]; then + + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Error ]: I started the 'Nginx' webserver, but service is not running !!!" >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + + error "I started the 'Nginx' webserver, but service is not running !!!" + + datum=`date +"%d.%m.%Y %H:%Mh"` + + subject="[ Error ]: Nginx Webservice on `hostname -f` NOT RUNNING -- $datum" + msg="\n\n`cat $LOCK_DIR/extra_msg.txt`\n" + + for _to_address in $to_addresses ; do + echo -e "To:${_to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \ + | /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $_to_address + done + + else + + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\tI started the 'Nginx' webserver. the new PID is $NEWPID" >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + + ok "I started the 'NGINX' webserver. the new PID is $NEWPID" + + fi + + if $send_msg ; then + datum=`date +"%d.%m.%Y %H:%Mh"` + subject="Restarting Nginx on `hostname -f` invoked -- $datum" + msg=`cat $LOCK_DIR/extra_msg.txt` + + for _to_address in $to_addresses ; do + echo -e "To:${_to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \ + | /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $_to_address + done + fi +} + start_php_fpm() { __version=$1 @@ -800,6 +1329,63 @@ start_php_fpm() { fi } +start_redis() { + + send_msg=$1 + send_msg=${send_msg:=true} + + + ## - Start Redis Caching Service + ## - + if [[ -n "$REDIS_SERVICE_FILE" ]] ; then + systemctl start "$REDIS_SERVICE_FILE" > /dev/null 2>&1 + else + $REDIS_INIT_SCRIPT start > /dev/null 2>&1 + fi + sleep 2 + + NEWPID=$(ps aux | grep "$REDISD" | grep -v grep | awk '{print$2}') + + if [ "X${NEWPID}X" = "XX" ]; then + + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\t[ Error ]: I started the 'Redis' Server, but service is not running !!!" >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + + error "I started the 'Redis' Server, but service is not running !!!" + + datum=`date +"%d.%m.%Y %H:%Mh"` + + subject="[ Error ]: Redis Caching Service on `hostname -f` NOT RUNNING -- $datum" + msg="\n\n`cat $LOCK_DIR/extra_msg.txt`\n" + + for _to_address in $to_addresses ; do + echo -e "To:${_to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \ + | /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $_to_address + done + + else + + echo "" >> $LOCK_DIR/extra_msg.txt + echo -e "\tI started the 'Redis' Server. the new PID is $NEWPID" >> $LOCK_DIR/extra_msg.txt + echo "" >> $LOCK_DIR/extra_msg.txt + + ok "I started the 'Redis' Server. the new PID is $NEWPID" + + fi + + if $send_msg ; then + datum=`date +"%d.%m.%Y %H:%Mh"` + subject="Restarting Redis Server on `hostname -f` invoked -- $datum" + msg=`cat $LOCK_DIR/extra_msg.txt` + + for _to_address in $to_addresses ; do + echo -e "To:${_to_address}\n${content_type}\nSubject:$subject\n\n${msg}\n" \ + | /usr/sbin/sendmail -F "Webservice Monitor" -f $from_address $_to_address + done + fi +} + restart_apache() { ## - Stop Apache Webservice @@ -813,9 +1399,22 @@ restart_apache() { start_apache } +restart_nginx() { + + ## - Stop Apache Webservice + ## - + stop_nginx + + sleep 2 + + ## - Start Apache Webservice + ## - + start_nginx +} + restart_mysql(){ - ## - Stop MySQL Servive + ## - Stop MySQL Service ## - stop_mysql @@ -826,6 +1425,19 @@ restart_mysql(){ start_mysql } +restart_postgresql(){ + + ## - Stop PostgreSQL Service + ## - + stop_postgresql + + sleep 2 + + ## - Start MySQL Service + ## - + start_postgresql +} + restart_php_fpm(){ ## - Stop MySQL Servive @@ -839,6 +1451,19 @@ restart_php_fpm(){ start_php_fpm $1 } +restart_redis(){ + + ## - Stop MySQL Servive + ## - + stop_redis + + sleep 2 + + ## - Start MySQL Service + ## - + start_redis +} + graceful_restart_php_fpm() { __version=$1 @@ -1062,7 +1687,7 @@ if $check_mysql ; then echo -e "\nChecking Mysql databse service on \"`hostname -f`\".." fi - PID=`ps aux | grep "$MYSQLD_SAFE" | grep -v grep | awk '{print$2}'` + PID="$(ps aux | grep -E "${MYSQL_PS_CHECK_STRING}" | grep -v grep | awk '{print$2}')" if [ "X${PID}X" = "XX" ];then @@ -1093,6 +1718,42 @@ if $check_mysql ; then fi +#--------------------------------------- +#----------------------------- +# Check Postgresql database service.. +#----------------------------- +#--------------------------------------- + +if $check_postgresql ; then + + echo "" > $LOCK_DIR/extra_msg.txt + + if $LOGGING ; then + echo -e "\nChecking PostgreSQL database service on \"`hostname -f`\".." + fi + + CHILD_PID="$(ps aux | grep -E "\ postgres:\ " | grep -v grep | tail -n 1 | awk '{print$2}')" + PSQL_MAIN_PID="$(ps -o ppid= -p $CHILD_PID 2> /dev/null)" + + if [ "X${PSQL_MAIN_PID}X" = "XX" ];then + + echo -e "\nPostgreSQL database service is not running! - Try to start it.." > $LOCK_DIR/extra_msg.txt + echo -e "===============================================================" >> $LOCK_DIR/extra_msg.txt + + error "PostgeSQL database service is not running! - Try to start it.." + + restart_postgresql + + else + + if $LOGGING ; then + ok "PostgreSQL database server on \"`hostname -f`\" seems to be running.." + fi + + fi +fi + + #--------------------------------------- #----------------------------- # Check Apache webservice.. @@ -1143,6 +1804,60 @@ if $check_apache ; then fi +#--------------------------------------- +#----------------------------- +# Check Nginx webservice.. +#----------------------------- +#--------------------------------------- + +if $check_nginx ; then + + echo "" > $LOCK_DIR/extra_msg.txt + + if $LOGGING ; then + echo -e "\nChecking Nginx webservice on \"`hostname -f`\".." + fi + + PID=`ps aux | grep "$NGINXD" | grep -e "^root" | grep -v grep | awk '{print$2}'` + if [ "X${PID}X" = "XX" ];then + + echo -e "\nNginx Webservice is not running! - Try to start it.." > $LOCK_DIR/extra_msg.txt + echo -e "====================================================" >> $LOCK_DIR/extra_msg.txt + + error "Nginx webservice is not running! - Try to start it.." + + restart_nginx + + else + if ! $ommit_curl_check_nginx ; then + + if ! $(curl -Is -m 10 http://$curl_check_host | head -n 1 | grep "200 OK" > /dev/null) ; then + echo -e "\nNginx Webservice seems to be running, but is NOT responding! - Try to restart service.." >> $LOCK_DIR/extra_msg.txt + echo -e "=======================================================================================" >> $LOCK_DIR/extra_msg.txt + + error "Nginx Webservice seems to be running, but is NOT responding! - Try to restart service.." + + restart_nginx + + else + + if $LOGGING ; then + ok "Nginx Webserver on \"`hostname -f`\" seems to be running.." + fi + + fi + + else + + if $LOGGING ; then + ok "Nginx Webserver on \"`hostname -f`\" seems to be running.." + fi + + fi + fi +fi + + #--------------------------------------- #----------------------------- # Check PHP-FPM engine.. @@ -1190,6 +1905,40 @@ if $check_php_fpm ; then fi +#--------------------------------------- +#----------------------------- +# Check Redis Caching Service.. +#----------------------------- +#--------------------------------------- + +if $check_redis ; then + + echo "" > $LOCK_DIR/extra_msg.txt + + if $LOGGING ; then + echo -e "\nChecking Redis Caching Service on \"`hostname -f`\".." + fi + + PID=`ps aux | grep "$REDISD" | grep -v grep | awk '{print$2}'` + if [ "X${PID}X" = "XX" ];then + + echo -e "\nRedis Caching Service is not running! - Try to start it.." > $LOCK_DIR/extra_msg.txt + echo -e "=========================================================" >> $LOCK_DIR/extra_msg.txt + + error "Redis Caching Service is not running! - Try to start it.." + + restart_redis + + else + + if $LOGGING ; then + ok "Redis Caching Service on \"`hostname -f`\" seems to be running.." + fi + + fi +fi + + #--------------------------------------- #----------------------------- # Check website.. diff --git a/conf/check_cert_for_service.conf.sample b/conf/check_cert_for_service.conf.sample new file mode 100644 index 0000000..d7c3ac0 --- /dev/null +++ b/conf/check_cert_for_service.conf.sample @@ -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="" + diff --git a/conf/check_remote_websites.conf.sample b/conf/check_remote_websites.conf.sample new file mode 100644 index 0000000..5e00e25 --- /dev/null +++ b/conf/check_remote_websites.conf.sample @@ -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="" diff --git a/conf/check_webservice_load.conf.sample b/conf/check_webservice_load.conf.sample index a686268..8df392f 100644 --- a/conf/check_webservice_load.conf.sample +++ b/conf/check_webservice_load.conf.sample @@ -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 # - diff --git a/remove_zombies_command.sh b/remove_zombies_command.sh new file mode 100755 index 0000000..fc16341 --- /dev/null +++ b/remove_zombies_command.sh @@ -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 diff --git a/remove_zombies_user.sh b/remove_zombies_user.sh new file mode 100755 index 0000000..76979b0 --- /dev/null +++ b/remove_zombies_user.sh @@ -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 + +\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 + 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