This commit is contained in:
Patrick Van der Veken
2020-01-30 11:43:58 +01:00
23 changed files with 283 additions and 97 deletions
+24 -3
View File
@@ -38,7 +38,7 @@
# ------------------------- CONFIGURATION starts here -------------------------
# define the version (YYYY-MM-DD)
typeset -r SCRIPT_VERSION="2019-07-16"
typeset -r SCRIPT_VERSION="2019-10-24"
# location of parent directory containing KSH functions/HC plugins
typeset -r FPATH_PARENT="/opt/hc/lib"
# location of custom HC configuration files
@@ -94,6 +94,7 @@ typeset HC_RUN=""
typeset HC_FAIL_ID=""
# shellcheck disable=SC2034
typeset HC_FILE_LINE=""
typeset HC_LOG_HEALTHY=""
typeset HC_NOW=""
typeset HC_TIME_OUT=60
typeset HC_MIN_TIME_OUT=30
@@ -101,6 +102,7 @@ typeset HC_MIN_TIME_OUT=30
typeset HC_MSG_VAR=""
typeset HC_STDOUT_LOG=""
typeset HC_STDERR_LOG=""
typeset HC_WILL_FIX=""
# shellcheck disable=SC2034
typeset LINUX_DISTRO=""
# shellcheck disable=SC2034
@@ -613,10 +615,11 @@ Parameters:
--list-core : show the available core plugins (mail,SMS,...)
--list-include : show the available includes/libraries
--log-healthy : log/show also passed health checks. By default this is off when the plugin support this feature.
(can be overridden by --no-log to disable all logging)
(overrides \$HC_LOG_HEALTHY and can itself be overridden by --no-log to disable all logging)
--mail-to : list of e-mail address(es) to which an e-mail alert will be send to [requires mail core plugin]
--newer : show the (failed) events for each HC that are newer than the given date
--no-fix : do not apply fix/healing logic for failed health checks (if available)
(overrides \$HC_WILL_FIX)
--no-lock : disable locking to allow concurrent script executions
--no-log : do not log any messages to the script log file or health check results.
--no-monitor : do not stop the execution of a HC after \$HC_TIME_OUT seconds
@@ -630,7 +633,7 @@ Parameters:
--sms-provider : name of a supported SMS provider (see \$SMS_PROVIDERS) [requires SMS core plugin]
--sms-to : name of person or group to which a sms alert will be send to [requires SMS core plugin]
--timeout : maximum runtime of a HC plugin in seconds (overrides \$HC_TIME_OUT)
--today : show today's (failed) events (HC and their combined STC value)
--today : show (failed) events of today (HC and their combined STC value)
--version : show the timestamp of the script.
--with-history : also include events that have been archived already (reporting)
--with-rc : define RC handling (plugin) when --flip-rc is used
@@ -743,6 +746,24 @@ else
. "${CONFIG_FILE}"
fi
# reconcile global settings w/ cmd-line parameters
if (( ARG_LOG_HEALTHY == 0 ))
then
case "${HC_LOG_HEALTHY}" in
yes|YES|Yes)
ARG_LOG_HEALTHY=1
;;
esac
fi
if (( ARG_NO_FIX == 0 ))
then
case "${HC_WILL_FIX}" in
no|NO|No)
ARG_NO_FIX=1
;;
esac
fi
return 0
}
+8 -1
View File
@@ -30,7 +30,7 @@
# RETURNS: 0
function version_include_core
{
typeset _VERSION="2019-06-18" # YYYY-MM-DD
typeset _VERSION="2019-11-03" # YYYY-MM-DD
print "INFO: $0: ${_VERSION#version_*}"
@@ -76,6 +76,13 @@ grep ".*${LOG_SEP}${HC_NAME}${LOG_SEP}" ${HC_LOG} 2>/dev/null |\
cut -f1-2 -d'-' 2>/dev/null | sort -u 2>/dev/null |\
while read -r YEAR_MONTH
do
# skip messages with bogus date
if [[ -z "${YEAR_MONTH}" ]]
then
warn "skipping bogus log lines (lines without proper datestamp)"
continue
fi
# find all messages for that YEAR-MONTH combination
grep "${YEAR_MONTH}.*${LOG_SEP}${HC_NAME}${LOG_SEP}" ${HC_LOG} >${TMP1_FILE}
TODO_LOG_COUNT=$(wc -l ${TMP1_FILE} 2>/dev/null | cut -f1 -d' ' 2>/dev/null)
@@ -33,7 +33,7 @@ function check_linux_autofs
{
# ------------------------- CONFIGURATION starts here -------------------------
typeset _CONFIG_FILE="${CONFIG_DIR}/$0.conf"
typeset _VERSION="2019-07-14" # YYYY-MM-DD
typeset _VERSION="2019-10-24" # YYYY-MM-DD
typeset _SUPPORTED_PLATFORMS="Linux" # uname -s match
# shellcheck disable=SC2034
typeset _HC_CAN_FIX=1 # plugin has fix/healing logic?
@@ -136,6 +136,13 @@ fi
# check if autofs is enabled
log "checking if autofs daemon is enabled"
_HAS_SERVICE=$(linux_has_service "autofs")
(( $? > 0)) && {
_MSG="error in function {linux_has_service}"
log_hc "$0" 1 "${_MSG}"
# dump debug info
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && dump_logs
return 1
}
if (( _HAS_SERVICE == 2 ))
then
_MSG="autofs service is enabled"
@@ -147,6 +154,13 @@ then
# check if autofs is running
log "checking if autofs daemon is active"
_IS_ACTIVE=$(linux_runs_service autofs)
(( $? > 0)) && {
_MSG="error in function {linux_runs_service }"
log_hc "$0" 1 "${_MSG}"
# dump debug info
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && dump_logs
return 1
}
if (( _IS_ACTIVE > 0 ))
then
_MSG="autofs daemon is running"
@@ -170,9 +184,23 @@ then
sleep ${_SLEEP_TIME}
_RETRY_COUNT=$(( _RETRY_COUNT + 1 ))
_IS_ACTIVE=$(linux_runs_service "autofs")
(( $? > 0)) && {
_MSG="error in function {linux_runs_service }"
log_hc "$0" 1 "${_MSG}"
# dump debug info
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && dump_logs
return 1
}
done
# check again if autofs is running
_IS_ACTIVE=$(linux_runs_service "autofs")
(( $? > 0)) && {
_MSG="error in function {linux_runs_service }"
log_hc "$0" 1 "${_MSG}"
# dump debug info
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && dump_logs
return 1
}
if (( _IS_ACTIVE > 0 ))
then
_MSG="autofs daemon is running"
@@ -30,6 +30,8 @@
# @(#) 2019-01-24: arguments fix [Patrick Van der Veken]
# @(#) 2019-03-09: added support for --log-healthy [Patrick Van der Veken]
# @(#) 2019-03-16: replace 'which' [Patrick Van der Veken]
# @(#) 2019-11-01: added support for configuration parameters 'check_type' and
# @(#) 'httpd_bin' [Patrick Van der Veken]
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#******************************************************************************
@@ -38,9 +40,10 @@
function check_linux_httpd_status
{
# ------------------------- CONFIGURATION starts here -------------------------
typeset _CONFIG_FILE="${CONFIG_DIR}/$0.conf"
typeset _HTTPD_INIT_SCRIPT="/etc/init.d/httpd"
typeset _HTTPD_SYSTEMD_SERVICE="httpd.service"
typeset _VERSION="2019-03-16" # YYYY-MM-DD
typeset _VERSION="2019-11-01" # YYYY-MM-DD
typeset _SUPPORTED_PLATFORMS="Linux" # uname -s match
# ------------------------- CONFIGURATION ends here ---------------------------
@@ -49,11 +52,15 @@ typeset _SUPPORTED_PLATFORMS="Linux" # uname -s match
init_hc "$0" "${_SUPPORTED_PLATFORMS}" "${_VERSION}"
typeset _ARGS=$(data_comma2space "$*")
typeset _ARG=""
typeset _CHECK_SYSTEMD_SERVICE=0
typeset _HTTPD_BIN=""
typeset _MSG=""
typeset _STC=0
typeset _CHECK_SYSTEMD_SERVICE=0
typeset _CFG_HEALTHY=""
typeset _LOG_HEALTHY=0
typeset _CFG_HTTPD_BIN=""
typeset _HTTPD_BIN=""
typeset _CFG_CHECK_TYPE=""
typeset _DO_PGREP=0
typeset _RC=0
# handle arguments (originally comma-separated)
@@ -66,6 +73,50 @@ do
esac
done
# handle configuration file
[[ -n "${ARG_CONFIG_FILE}" ]] && _CONFIG_FILE="${ARG_CONFIG_FILE}"
if [[ ! -r ${_CONFIG_FILE} ]]
then
warn "unable to read configuration file at ${_CONFIG_FILE}"
return 1
fi
# read configuration values
_CFG_CHECK_TYPE=$(_CONFIG_FILE="${_CONFIG_FILE}" data_get_lvalue_from_config 'check_type')
case "${_CFG_CHECK_TYPE}" in
pgrep|Pgrep|PGREP)
_DO_PGREP=1
log "using pgrep process check (config override)"
;;
sysv|Sysv|SYSV)
LINUX_INIT="sysv"
log "using init based process check (config override)"
;;
systemd|Systemd|SYSTEMD)
LINUX_INIT="systemd"
log "using systemd based process check (config override)"
;;
*)
# no overrides
:
;;
esac
_CFG_HTTPD_BIN=$(_CONFIG_FILE="${_CONFIG_FILE}" data_get_lvalue_from_config 'httpd_bin')
if [[ -n "${_CFG_HTTPD_BIN}" ]]
then
_HTTPD_BIN="${_CFG_HTTPD_BIN}"
log "setting httpd path to {${_HTTPD_BIN}} (config override)"
fi
_CFG_HEALTHY=$(_CONFIG_FILE="${_CONFIG_FILE}" data_get_lvalue_from_config 'log_healthy')
case "${_CFG_HEALTHY}" in
yes|YES|Yes)
_LOG_HEALTHY=1
;;
*)
# do not override hc_arg
(( _LOG_HEALTHY > 0 )) || _LOG_HEALTHY=0
;;
esac
# log_healthy
(( ARG_LOG_HEALTHY > 0 )) && _LOG_HEALTHY=1
if (( _LOG_HEALTHY > 0 ))
@@ -80,44 +131,58 @@ else
log "not logging/showing passed health checks"
fi
# check httpd (if specified)
if [[ -z "${_HTTPD_BIN}" ]]
then
_HTTPD_BIN="$(command -v httpd 2>>${HC_STDERR_LOG})"
fi
if [[ ! -x ${_HTTPD_BIN} || -z "${_HTTPD_BIN}" ]]
then
warn "httpd (apache) is not installed here"
return 1
fi
# ---- process state ----
# 1) try using the init ways
linux_get_init
case "${LINUX_INIT}" in
'systemd')
_CHECK_SYSTEMD_SERVICE=$(linux_has_systemd_service "${_HTTPD_SYSTEMD_SERVICE}")
if (( _CHECK_SYSTEMD_SERVICE > 0 ))
then
systemctl --quiet is-active ${_HTTPD_SYSTEMD_SERVICE} 2>>${HC_STDERR_LOG} || _STC=1
else
warn "systemd unit file not found {${_HTTPD_SYSTEMD_SERVICE}}"
_RC=1
fi
;;
'upstart')
warn "code for upstart managed systems not implemented, NOOP"
_RC=1
;;
'sysv')
# check running SysV
if [[ -x ${_HTTPD_INIT_SCRIPT} ]]
then
if (( $(${_HTTPD_INIT_SCRIPT} status 2>>${HC_STDERR_LOG} | grep -c -i 'is running' 2>/dev/null) == 0 ))
if (( _DO_PGREP == 0 ))
then
[[ -n "${LINUX_INIT}" ]] || linux_get_init
case "${LINUX_INIT}" in
'systemd')
_CHECK_SYSTEMD_SERVICE=$(linux_has_systemd_service "${_HTTPD_SYSTEMD_SERVICE}")
if (( _CHECK_SYSTEMD_SERVICE > 0 ))
then
_STC=1
systemctl --quiet is-active ${_HTTPD_SYSTEMD_SERVICE} 2>>${HC_STDERR_LOG} || _STC=1
else
warn "systemd unit file not found {${_HTTPD_SYSTEMD_SERVICE}}"
_RC=1
fi
else
warn "sysv init script not found {${_HTTPD_INIT_SCRIPT}}"
;;
'upstart')
warn "code for upstart managed systems not implemented, NOOP"
_RC=1
fi
;;
*)
_RC=1
;;
esac
;;
'sysv')
# check running SysV
if [[ -x ${_HTTPD_INIT_SCRIPT} ]]
then
if (( $(${_HTTPD_INIT_SCRIPT} status 2>>${HC_STDERR_LOG} | grep -c -i 'is running' 2>/dev/null) == 0 ))
then
_STC=1
fi
else
warn "sysv init script not found {${_HTTPD_INIT_SCRIPT}}"
_RC=1
fi
;;
*)
_RC=1
;;
esac
fi
# 2) try the pgrep way (note: old pgreps do not support '-c')
if (( _RC > 0 ))
if (( _DO_PGREP > 0 || _RC > 0 ))
then
(( $(pgrep -u root httpd 2>>${HC_STDERR_LOG} | wc -l 2>/dev/null) == 0 )) && _STC=1
fi
@@ -140,23 +205,18 @@ then
fi
# ---- config state ----
_HTTPD_BIN="$(command -v httpd 2>>${HC_STDERR_LOG})"
if [[ -x ${_HTTPD_BIN} && -n "${_HTTPD_BIN}" ]]
${_HTTPD_BIN} -t >>${HC_STDOUT_LOG} 2>>${HC_STDERR_LOG}
if (( $? == 0 ))
then
# validate main configuration
${_HTTPD_BIN} -t >>${HC_STDOUT_LOG} 2>>${HC_STDERR_LOG}
if (( $? == 0 ))
then
_MSG="httpd configuration files are syntactically correct"
_STC=0
else
_MSG="httpd configuration files have syntax error(s) {httpd -s}"
_STC=1
fi
if (( _LOG_HEALTHY > 0 || _STC > 0 ))
then
log_hc "$0" ${_STC} "${_MSG}"
fi
_MSG="httpd configuration files are syntactically correct"
_STC=0
else
_MSG="httpd configuration files have syntax error(s) {httpd -s}"
_STC=1
fi
if (( _LOG_HEALTHY > 0 || _STC > 0 ))
then
log_hc "$0" ${_STC} "${_MSG}"
fi
return 0
@@ -168,6 +228,10 @@ function _show_usage
cat <<- EOT
NAME : $1
VERSION : $2
CONFIG : $3 with parameters:
log_healthy=<yes|no>
check_type=<auto|pgrep|sysv|systemd> [compt. >=2019-11-01]
httpd_bin=<path_to_httpd> [compt. >=2019-11-01]
PURPOSE : Checks whether httpd (apache service) is running and whether the
httpd configuration files are syntactically correct
LOG HEALTHY : Supported