diff --git a/sources/bin/check_health.sh b/sources/bin/check_health.sh index 44f9431..5f9a129 100644 --- a/sources/bin/check_health.sh +++ b/sources/bin/check_health.sh @@ -37,7 +37,7 @@ # ------------------------- CONFIGURATION starts here ------------------------- # define the version (YYYY-MM-DD) -typeset -r SCRIPT_VERSION="2018-01-03" +typeset -r SCRIPT_VERSION="2018-03-15" # location of parent directory containing KSH functions/HC plugins typeset -r FPATH_PARENT="/opt/hc/lib" # location of custom HC configuration files @@ -99,6 +99,7 @@ typeset ARG_DEBUG_LEVEL=0 # debug() only by default typeset ARG_DETAIL=0 # for --report typeset ARG_DISPLAY="" # display is STDOUT by default typeset ARG_FAIL_ID="" +typeset ARG_FLIP_RC=0 # swapping EXIT RC is off by default typeset ARG_HC="" typeset ARG_HC_ARGS="" # no extra arguments to HC plug-in by default typeset ARG_HISTORY=0 # include historical events is off by default @@ -320,6 +321,16 @@ then exit 1 fi fi +# --flip-rc +if (( ARG_FLIP_RC != 0 )) +then + # do not allow flip RC for multiple checks + if [[ "${ARG_HC}" = *,* ]] # use =, ksh88 + then + print -u2 "ERROR: flipping RC (return code) is not allowed when executing multiple HC's" + exit 1 + fi +fi # --check-host,--check/--disable/--enable/--run/--show/--archive,--hc if [[ -n "${ARG_HC}" ]] && (( ARG_ACTION == 0 )) then @@ -461,7 +472,7 @@ Execute/report simple health checks (HC) on UNIX hosts. Syntax: ${SCRIPT_DIR}/${SCRIPT_NAME} [--help] | [--help-terse] | [--version] | [--list=] | [--list-core] | [--fix-symlinks] | (--disable-all | enable-all) | (--check-host | ((--archive | --check | --enable | --disable | --run | --show) --hc= [--config-file=] [hc-args="])) - [--display=] ([--debug] [--debug-level=]) [--no-monitor] [--no-log] [--no-lock] + [--display=] ([--debug] [--debug-level=]) [--no-monitor] [--no-log] [--no-lock] [--flip-rc] [--notify=] [--mail-to=] [--sms-to= --sms-provider=] [--report= ( ([--last] | [--today]) | ([--reverse] [--id= [--detail]] [--with-history]) ) ] @@ -475,7 +486,7 @@ Parameters: --archive : move events from the HC log file into archive log files --check : display HC state. --check-host : execute all configured HC(s) (see check_host.conf) ---config-file : custom configuration file for a HC, may only be specified when executing a single HC plugin. +--config-file : custom configuration file for a HC (may only be specified when executing a single HC plugin) --debug : run script in debug mode --debug-level : level of debugging information to show (0,1,2) --detail : show detailed info on failed HC event (will show STDOUT+STDERR logs) @@ -485,6 +496,8 @@ Parameters: --enable : enable HC(s). --enable-all : enable all HCs. --fix-symlinks : update symbolic links for the KSH autoloader. +--flip-rc : exit the health checker with the RC (return code) of the HC plugin instead of its own RC (will be discarded) + This option may only be specified when executing a single HC plugin --hc : list of health checks to be executed (comma-separated) (see also --list-hc) --hc-args : extra arguments to be passed to an individual HC. Arguments must be comma-separated and enclosed in double quotes (example: --hc_args="arg1,arg2=value,arg3"). @@ -693,6 +706,9 @@ do fix_symlinks exit 0 ;; + -flip-rc|--flip-rc) + ARG_FLIP_RC=1 + ;; -hc=*) ARG_HC="${CMD_PARAMETER#-hc=}" ;; @@ -1049,6 +1065,8 @@ case ${ARG_ACTION} in # reset FAIL_ID & HC failure storage (also for failed HCs) handle_hc "${HC_RUN}" + # exit with return code from handle_hc() (see --flip-rc) + EXIT_CODE=$? rm -f ${HC_MSG_FILE} >/dev/null 2>&1 done ;; diff --git a/sources/lib/core/include_core.sh b/sources/lib/core/include_core.sh index 5b62bd9..e840d2c 100644 --- a/sources/lib/core/include_core.sh +++ b/sources/lib/core/include_core.sh @@ -577,7 +577,7 @@ return 0 # @(#) FUNCTION: handle_hc() # DOES: handle HC results # EXPECTS: 1=HC name [string], $HC_MSG_FILE temporary file -# RETURNS: 0 +# RETURNS: 0 or $HC_STC_RC # REQUIRES: die(), display_*(), notify_*(), warn() function handle_hc { @@ -588,6 +588,7 @@ typeset I=0 typeset MAX_I=0 typeset HC_STDOUT_LOG_SHORT="" typeset HC_STDERR_LOG_SHORT="" +typeset HC_STC_RC=0 set -A HC_MSG_STC set -A HC_MSG_TIME set -A HC_MSG_TEXT @@ -774,6 +775,7 @@ then if (( HC_MSG_STC[${I}] != 0 )) then printf "%s${SEP}\n" "${HC_FAIL_ID}" >>${HC_LOG} + HC_STC_RC=$(( HC_STC_RC + 1 )) else printf "\n" >>${HC_LOG} fi @@ -843,7 +845,13 @@ then fi fi -return 0 +# --flip-rc: pass RC of HC plugin back +if (( ARG_FLIP_RC == 0 )) +then + return 0 +else + return ${HC_STC_RC} +fi } # -----------------------------------------------------------------------------