From 404ab8a89186ce7ced39e5181e7da6029badde65 Mon Sep 17 00:00:00 2001 From: Christoph Date: Fri, 22 Oct 2021 14:22:17 +0200 Subject: [PATCH] check_webservice_load.sh: add parameter 'CONFLICTING_SCRIPTS' - don't run this script if anny given conflicting scripts are running. --- check_webservice_load.sh | 161 +++++++++++++++++-------- conf/check_webservice_load.conf.sample | 19 +++ 2 files changed, 127 insertions(+), 53 deletions(-) diff --git a/check_webservice_load.sh b/check_webservice_load.sh index f5be039..fd8714c 100755 --- a/check_webservice_load.sh +++ b/check_webservice_load.sh @@ -24,21 +24,6 @@ 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!" - echo "" - echo -e "\tScript terminated.." - echo "" - exit 1 -else - source "$conf_file" -fi - - - - - # - Lock directory exists, until the script ends. So # - we can check, if a previos instanze is already running. # - @@ -52,6 +37,16 @@ LOCK_DIR=`mktemp -d` #----------------------------- #--------------------------------------- +clean_up() { + + # Perform program exit housekeeping + rm -rf "$LOCK_DIR" + if $LOGGING ; then + echo "" + fi + exit $1 +} + fatal(){ echo "" if $terminal ; then @@ -141,6 +136,103 @@ trim() { echo -n "$var" } +## - #--------------------------------------- +## - #----------------------------- +## - # 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 'rm -rf "$LOCK_DIR"' 0 2 15 +## - +## - else +## - +## - datum=`date +"%d.%m.%Y"` +## - +## - msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\tExiting now.." +## - +## - echo "" +## - echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running." +## - echo "" +## - echo -e "\tExiting now.." +## - echo "" +## - +## - for _to_address in $to_addresses ; do +## - echo -e "To:${_to_address}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \ +## - | sendmail -F "Error `hostname -f`" -f $from_address $_to_address +## - done +## - +## - exit 1 +## - +## - fi + +## - Remove lockdir when the script finishes, or when it receives a signal +trap 'rm -rf "$LOCK_DIR"' 0 2 15 + +# ------------- +# --- Read Configurations from $conf_file +# ------------- + +# Some default values +# +DEFAULT_CONFLICTING_SCRIPTS="/root/bin/monitoring/check_local_webservice.sh:/tmp/check_local_webservice.LOCK" + +if [[ ! -f "$conf_file" ]]; then + echo "" + echo -e " [ Fatal ] Configuration file '$(basename ${conf_file})' not found!" + echo "" + echo -e "\tScript terminated.." + echo "" + clean_up 1 +else + source "$conf_file" +fi + +[[ -n "$CONFLICTING_SCRIPTS" ]] || CONFLICTING_SCRIPTS="$DEFAULT_CONFLICTING_SCRIPTS" + + +# - Stop here, if these give scripts are running +# - +if [[ ${#CONFLICTING_SCRIPTS} -gt 0 ]] ; then + + _stop_running=false + for _val in $CONFLICTING_SCRIPTS ; do + + IFS=':' read -a _val_arr <<< "${_val}" + + _script_name="$(basename ${_val_arr[0]})" + + if [[ -n "${_val_arr[1]}" ]] && [[ -d "${_val_arr[1]}" ]] ; then + _stop_running=true + else + + check_string_ps="${_val_arr[0]}" + + if ps -e f | grep -E "\s+${check_string_ps}" | grep -v grep | grep -v -E "\s+vim\s+" > /dev/null ; then + _stop_running=true + fi + fi + + if $_stop_running ; then + + echo "" + echo "[ Error ]: The \"${_script_name}\" script is currently running, but it conflicts with this script." + echo "" + echo " Exiting now.." + echo "" + + clean_up 1 + + fi + + + done +fi + #--------------------------------------- #----------------------------- @@ -564,43 +656,6 @@ if $check_website ; then fi 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 'rm -rf "$LOCK_DIR"' 0 2 15 -## - -## - else -## - -## - datum=`date +"%d.%m.%Y"` -## - -## - msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\tExiting now.." -## - -## - echo "" -## - echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running." -## - echo "" -## - echo -e "\tExiting now.." -## - echo "" -## - -## - for _to_address in $to_addresses ; do -## - echo -e "To:${_to_address}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \ -## - | sendmail -F "Error `hostname -f`" -f $from_address $_to_address -## - done -## - -## - exit 1 -## - -## - fi - -## - Remove lockdir when the script finishes, or when it receives a signal -trap 'rm -rf "$LOCK_DIR"' 0 2 15 - #--------------------------------------- #----------------------------- @@ -2256,4 +2311,4 @@ if $check_website ; then fi -exit 0 +clean_up 0 diff --git a/conf/check_webservice_load.conf.sample b/conf/check_webservice_load.conf.sample index 8df392f..0adabfc 100644 --- a/conf/check_webservice_load.conf.sample +++ b/conf/check_webservice_load.conf.sample @@ -14,6 +14,25 @@ # --- +# - CONFLICTING_SCRIPTS +# - +# - The scripts listed here conflict with this script. If one of these scripts +# - is currently running, this script will be stopped. +# - +# - In addition to the script, a LOCK directory can also be specified which is +# - connected to it. +# - +# - Example: +# - CONFLICTING_SCRIPTS=" +# - /root/bin/monitoring/check_webservice_load.sh +# - /root/bin/monitoring/check_remote_websites.sh:/tmp/check_remote_websites.LOCK +# - +# - Defaults to: +# - CONFLICTING_SCRIPTS="/root/bin/monitoring/check_local_webservice.sh:/tmp/check_local_webservice.LOCK" +# - +#CONFLICTING_SCRIPTS="" + + # - What to check # - check_load=true