This commit is contained in:
patvdv
2017-09-02 23:27:01 +02:00
parent b23126799b
commit 2775da5f44
175 changed files with 17098 additions and 2 deletions
+100
View File
@@ -0,0 +1,100 @@
#!/usr/bin/env ksh
#******************************************************************************
# @(#) display_csv.sh
#******************************************************************************
# @(#) Copyright (C) 2017 by KUDOS BVBA (info@kudos.be). All rights reserved.
#
# This program is a free software; you can redistribute it and/or modify
# it under the same terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
#******************************************************************************
#
# DOCUMENTATION (MAIN)
# -----------------------------------------------------------------------------
# @(#) MAIN: display_csv
# DOES: display HC results in CSV format (semi-colon as separator)
# EXPECTS: 1=HC name [string], 2=HC FAIL_ID [string]
# RETURNS: 0
# REQUIRES: init_hc()
#
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#******************************************************************************
# -----------------------------------------------------------------------------
function display_csv
{
# ------------------------- CONFIGURATION starts here -------------------------
typeset _VERSION="2017-05-06" # YYYY-MM-DD
typeset _SUPPORTED_PLATFORMS="AIX,HP-UX,Linux" # uname -s match
typeset _SEP=";"
# ------------------------- CONFIGURATION ends here ---------------------------
# set defaults
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
init_hc "$0" "${_SUPPORTED_PLATFORMS}" "${_VERSION}"
typeset _DISPLAY_HC="$1"
typeset _DISPLAY_FAIL_ID="$2"
set -A _DISPLAY_MSG_STC
set -A _DISPLAY_MSG_TIME
set -A _DISPLAY_MSG_TEXT
set -A _DISPLAY_MSG_CUR_VAL
set -A _DISPLAY_MSG_EXP_VAL
typeset _I=0
typeset _MAX_I=0
typeset _ID_BIT=""
# read HC_MSG_FILE into an arrays
# note: this is less efficient but provides more flexibility for future extensions
# max array size: 1023 in ksh88f, plugins spawning more than >1K messages are crazy :-)
while read HC_MSG_ENTRY
do
_DISPLAY_MSG_STC[${_I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $1'})
_DISPLAY_MSG_TIME[${_I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $2'})
_DISPLAY_MSG_TEXT[${_I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $3'})
_DISPLAY_MSG_CUR_VAL[${_I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $4'})
_DISPLAY_MSG_EXP_VAL[${_I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $5'})
_I=$(( _I + 1 ))
done <${HC_MSG_FILE} 2>/dev/null
# display HC results
_MAX_I=${#_DISPLAY_MSG_STC[*]}
_I=0
if (( _MAX_I > 0 ))
then
printf "%s${_SEP}%s${_SEP}%s${_SEP}%s${_SEP}%s${_SEP}%s\n" "Health Check" "STC" "Message" "FAIL ID" \
"Current Value" "Expected Value"
while (( _I < _MAX_I ))
do
if (( _DISPLAY_MSG_STC[${_I}] != 0 ))
then
_ID_BIT="${_DISPLAY_FAIL_ID}"
else
_ID_BIT=""
fi
printf "%s${_SEP}%s${_SEP}%s${_SEP}%s${_SEP}%s${_SEP}%s\n" \
"${_DISPLAY_HC}" \
"${_DISPLAY_MSG_STC[${_I}]}" \
"${_ID_BIT}" \
"${_DISPLAY_MSG_TEXT[${_I}]}" \
"${_DISPLAY_MSG_CUR_VAL[${_I}]}" \
"${_DISPLAY_MSG_EXP_VAL[${_I}]}"
_I=$(( _I + 1 ))
done
else
ARG_LOG=0 ARG_VERBOSE=1 log "INFO: no HC results to display"
fi
return 0
}
#******************************************************************************
# END of script
#**************************************************************************
+151
View File
@@ -0,0 +1,151 @@
#!/usr/bin/env ksh
#******************************************************************************
# @(#) display_init.sh
#******************************************************************************
# @(#) Copyright (C) 2017 by KUDOS BVBA (info@kudos.be). All rights reserved.
#
# This program is a free software; you can redistribute it and/or modify
# it under the same terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
#******************************************************************************
#
# DOCUMENTATION (MAIN)
# -----------------------------------------------------------------------------
# @(#) MAIN: display_init
# DOES: display HC results as boot/init-style messages (coloured stati)
# EXPECTS: 1=HC name [string], 2=HC FAIL_ID [string],
# 3=display code [string] (optional)
# RETURNS: 0
# REQUIRES: init_hc()
#
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#******************************************************************************
# -----------------------------------------------------------------------------
function display_init
{
# ------------------------- CONFIGURATION starts here -------------------------
typeset _VERSION="2017-06-29" # YYYY-MM-DD
typeset _SUPPORTED_PLATFORMS="AIX,HP-UX,Linux" # uname -s match
# ------------------------- CONFIGURATION ends here ---------------------------
# set defaults
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
init_hc "$0" "${_SUPPORTED_PLATFORMS}" "${_VERSION}"
typeset _DISPLAY_HC="$1"
typeset _DISPLAY_FAIL_ID="$2"
typeset _DISPLAY_MSG_CODE="$3"
typeset _DISPLAY_MSG_STC=0
typeset _DISPLAY_HC_DESC=""
typeset _DISPLAY_CFG=""
typeset _DISPLAY_COLOR=""
typeset -R8 _DISPLAY_CODE=""
typeset _DISPLAY_ID=""
# check for terminal support (no ((...)) here)
if [[ $(tput colors 2>/dev/null) -gt 0 ]]
then
typeset _RED=$(tput setaf 1)
typeset _GREEN=$(tput setaf 2)
typeset _YELLOW=$(tput setaf 3)
typeset _BLUE=$(tput setaf 4)
typeset _MAGENTA=$(tput setaf 5)
typeset _CYAN=$(tput setaf 6)
typeset _WHITE=$(tput setaf 7)
typeset _BOLD=$(tput bold)
typeset _NORMAL=$(tput sgr0)
else
typeset _RED=""
typeset _GREEN=""
typeset _YELLOW=""
typeset _BLUE=""
typeset _MAGENTA=""
typeset _CYAN=""
typeset _WHITE=""
typeset _BOLD=""
typeset _NORMAL=""
fi
# read HC_MSG_FILE for STC
if [[ -n "${_DISPLAY_MSG_CODE}" ]]
then
case "${_DISPLAY_MSG_CODE}" in
ERROR|error)
_DISPLAY_COLOR="${_MAGENTA}"
;;
DISABLED|disabled)
_DISPLAY_COLOR="${_CYAN}"
;;
MISSING|missing)
_DISPLAY_COLOR="${_BLUE}"
;;
*)
_DISPLAY_COLOR=""
;;
esac
_DISPLAY_CODE="${_DISPLAY_MSG_CODE}"
else
if [[ -s ${HC_MSG_FILE} ]]
then
while read HC_MSG_ENTRY
do
_DISPLAY_MSG_STC=$(( $(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $1'}) + _DISPLAY_MSG_STC ))
done <${HC_MSG_FILE} 2>/dev/null
# display HC results
if (( _DISPLAY_MSG_STC == 0 ))
then
_DISPLAY_CODE="OK"
_DISPLAY_COLOR="${_GREEN}"
else
_DISPLAY_CODE="FAIL"
_DISPLAY_COLOR="${_RED}"
# check if we have a valid FAIL_ID
if (( ARG_LOG == 1 ))
then
_DISPLAY_ID=" (${_BOLD}${_DISPLAY_FAIL_ID}${_NORMAL})"
else
_DISPLAY_ID=" (${_BOLD}not logged${_NORMAL})"
fi
fi
else
_DISPLAY_CODE="UNKNOWN"
_DISPLAY_COLOR="${_YELLOW}"
fi
fi
# check for alternative description, mangle _DISPLAY_HC
_DISPLAY_HC_DESC=$(grep -i "^hc:${HC_RUN}:" ${HOST_CONFIG_FILE} 2>/dev/null | cut -f4 -d':')
[[ -n "${_DISPLAY_HC_DESC}" ]] && _DISPLAY_HC="${_DISPLAY_HC_DESC}"
# check for alternative configuration file
if [[ -n "${ARG_CONFIG_FILE}" ]]
then
# file name only
_DISPLAY_CFG="${ARG_CONFIG_FILE##*/}"
else
_DISPLAY_CFG="default config"
fi
# print status line (but also check for terminal support)
printf "%-30s %50s\t[ %8s ]%s\n" \
"${_DISPLAY_HC}" \
"(${_DISPLAY_CFG})" \
"${_DISPLAY_COLOR}${_DISPLAY_CODE}${_NORMAL}" \
"${_DISPLAY_ID}"
return 0
}
#******************************************************************************
# END of script
#**************************************************************************
+96
View File
@@ -0,0 +1,96 @@
#!/usr/bin/env ksh
#******************************************************************************
# @(#) display_terse.sh
#******************************************************************************
# @(#) Copyright (C) 2017 by KUDOS BVBA (info@kudos.be). All rights reserved.
#
# This program is a free software; you can redistribute it and/or modify
# it under the same terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
#******************************************************************************
#
# DOCUMENTATION (MAIN)
# -----------------------------------------------------------------------------
# @(#) MAIN: display_terse
# DOES: display HC results in a terse way
# EXPECTS: 1=HC name [string], 2=HC FAIL_ID [string]
# RETURNS: 0
# REQUIRES: init_hc()
#
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#******************************************************************************
# -----------------------------------------------------------------------------
function display_terse
{
# ------------------------- CONFIGURATION starts here -------------------------
typeset _VERSION="2017-05-06" # YYYY-MM-DD
typeset _SUPPORTED_PLATFORMS="AIX,HP-UX,Linux" # uname -s match
# ------------------------- CONFIGURATION ends here ---------------------------
# set defaults
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
init_hc "$0" "${_SUPPORTED_PLATFORMS}" "${_VERSION}"
typeset _DISPLAY_HC="$1"
typeset _DISPLAY_FAIL_ID="$2"
set -A _DISPLAY_MSG_STC
set -A _DISPLAY_MSG_TIME
set -A _DISPLAY_MSG_TEXT
typeset _I=0
typeset _MAX_I=0
typeset _ID_BIT=""
# read HC_MSG_FILE into an arrays
# note: this is less efficient but provides more flexibility for future extensions
# max array size: 1023 in ksh88f, plugins spawning more than >1K messages are crazy :-)
while read HC_MSG_ENTRY
do
_DISPLAY_MSG_STC[${_I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $1'})
_DISPLAY_MSG_TIME[${_I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $2'})
_DISPLAY_MSG_TEXT[${_I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $3'})
_I=$(( _I + 1 ))
done <${HC_MSG_FILE} 2>/dev/null
# display HC results
_MAX_I=${#_DISPLAY_MSG_STC[*]}
_I=0
if (( _MAX_I > 0 ))
then
printf "%-30s\t%s\t%-16s\t%s\n" "HC" "STC" "FAIL ID" "Message"
while (( _I < _MAX_I ))
do
if (( _DISPLAY_MSG_STC[${_I}] != 0 ))
then
_ID_BIT="${_DISPLAY_FAIL_ID}"
else
_ID_BIT=""
fi
printf "%-30s\t%s\t%-16s\t%s\n" \
"${_DISPLAY_HC}" \
"${_DISPLAY_MSG_STC[${_I}]}" \
"${_ID_BIT}" \
"${_DISPLAY_MSG_TEXT[${_I}]}"
_I=$(( _I + 1 ))
done
else
ARG_LOG=0 ARG_VERBOSE=1 log "INFO: no HC results to display"
fi
# notice of other messages
print
ARG_LOG=0 ARG_VERBOSE=1 log "all other messages have been suppressed (except fatal errors)"
return 0
}
#******************************************************************************
# END of script
#**************************************************************************
+910
View File
@@ -0,0 +1,910 @@
#!/usr/bin/env ksh
#******************************************************************************
# @(#) include_core.sh
#******************************************************************************
# @(#) Copyright (C) 2017 by KUDOS BVBA (info@kudos.be). All rights reserved.
#
# This program is a free software; you can redistribute it and/or modify
# it under the same terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
#******************************************************************************
#
# DOCUMENTATION (MAIN)
# -----------------------------------------------------------------------------
# @(#) MAIN: include_core
# DOES: helper functions
#
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#******************************************************************************
# -----------------------------------------------------------------------------
# @(#) FUNCTION: debug()
# DOES: handle debug messages
# EXPECTS: log message [string]
# RETURNS: 0
# REQUIRES: n/a
function debug
{
typeset LOG_LINE=""
print - "$*" | while read -r LOG_LINE
do
print -u2 "DEBUG: ${LOG_LINE}"
done
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: die()
# DOES: handle fatal errors and exit script
# EXPECTS: log message [string]
# RETURNS: 0
# REQUIRES: n/a
function die
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset NOW="$(date '+%d-%h-%Y %H:%M:%S')"
typeset LOG_LINE=""
if [[ -n "$1" ]]
then
if (( ARG_LOG != 0 ))
then
print - "$*" | while read -r LOG_LINE
do
print "${NOW}: ERROR: [$$]:" "${LOG_LINE}" >>${LOG_FILE}
done
fi
print - "$*" | while read -r LOG_LINE
do
print -u2 "ERROR:" "${LOG_LINE}"
done
fi
# finish up work
do_cleanup
exit 1
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: discover_core()
# DOES: discover core plugins
# EXPECTS: n/a
# RETURNS: 0
# REQUIRES: die()
function discover_core
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset NOTIFY_OPTS=""
# init global flags for core plugins (no typeset!)
DO_DISPLAY_CSV=0
DO_DISPLAY_INIT=0
DO_DISPLAY_TERSE=0
DO_NOTIFY_EIF=0
DO_NOTIFY_MAIL=0
DO_NOTIFY_SMS=0
HAS_DISPLAY_CSV=0
HAS_DISPLAY_INIT=0
HAS_DISPLAY_TERSE=0
HAS_NOTIFY_EIF=0
HAS_NOTIFY_MAIL=0
HAS_NOTIFY_SMS=0
# check which core display/notification plugins are installed
# do not use a while-do loop here because mksh/pdksh does not pass updated
# variables back from the sub shell (only works for true ksh88/ksh93)
for FFILE in $(ls -1 ${FPATH_PARENT}/core/*.sh 2>/dev/null | grep -v "include_" 2>/dev/null)
do
case "${FFILE}" in
*display_csv.sh)
HAS_DISPLAY_CSV=1
(( ARG_DEBUG != 0 )) && debug "display_csv plugin is available"
;;
*display_init.sh)
HAS_DISPLAY_INIT=1
(( ARG_DEBUG != 0 )) && debug "display_init plugin is available"
;;
*display_terse.sh)
HAS_DISPLAY_TERSE=1
(( ARG_DEBUG != 0 )) && debug "display_terse plugin is available"
;;
*notify_mail.sh)
HAS_NOTIFY_MAIL=1
(( ARG_DEBUG != 0 )) && debug "notify_mail plugin is available"
;;
*notify_sms.sh)
HAS_NOTIFY_SMS=1
(( ARG_DEBUG != 0 )) && debug "notify_sms plugin is available"
;;
*notify_eif.sh)
HAS_NOTIFY_EIF=1
(( ARG_DEBUG != 0 )) && debug "notify_eif plugin is available"
;;
esac
done
# check command-line parameters for core plugins
# --display
if [[ -n "${ARG_DISPLAY}" ]]
then
case "${ARG_DISPLAY}" in
csv) # csv format
if (( HAS_DISPLAY_CSV == 1 ))
then
DO_DISPLAY_CSV=1
ARG_VERBOSE=0
else
warn "csv plugin for '--display' not present"
fi
;;
init) # init/boot format
if (( HAS_DISPLAY_INIT == 1 ))
then
DO_DISPLAY_INIT=1
ARG_VERBOSE=0
else
warn "init plugin for '--display' not present"
fi
;;
terse) # terse format
if (( HAS_DISPLAY_TERSE == 1 ))
then
DO_DISPLAY_TERSE=1
ARG_VERBOSE=0
else
warn "terse plugin for '--display' not present"
fi
;;
*) # stdout default
;;
esac
fi
# --notify
if [[ -n "${ARG_NOTIFY}" ]]
then
# do not use a while-do loop here because mksh/pdksh does not pass updated
# variables back from the sub shell (only works for true ksh88/ksh93)
for NOTIFY_OPTS in $(print "${ARG_NOTIFY}" | tr ',' ' ' 2>/dev/null)
do
case "${NOTIFY_OPTS}" in
*eif*) # by ITM
DO_NOTIFY_EIF=1
;;
*mail*) # by mail
DO_NOTIFY_MAIL=1
;;
*sms*) # by sms
DO_NOTIFY_SMS=1
;;
*) # no valid option
die "you have specified an invalid option for '--notify'"
;;
esac
done
fi
# --mail-to/--notify
if [[ -n "${ARG_MAIL_TO}" ]] && (( DO_NOTIFY_MAIL == 0 ))
then
die "you cannot specify '--mail-to' without '--notify=mail'"
fi
if (( DO_NOTIFY_MAIL != 0 )) && [[ -z "${ARG_MAIL_TO}" ]]
then
die "you cannot specify '--notify=mail' without '--mail-to'"
fi
# --sms-to/--sms-provider/--notify
if [[ -n "${ARG_SMS_TO}" ]] && (( DO_NOTIFY_SMS == 0 ))
then
die "you cannot specify '--sms-to' without '--notify=sms'"
fi
if [[ -n "${ARG_SMS_PROVIDER}" ]] && (( DO_NOTIFY_SMS == 0 ))
then
die "you cannot specify '--sms-provider' without '--notify=sms'"
fi
if (( DO_NOTIFY_SMS != 0 )) && [[ -z "${ARG_SMS_TO}" ]]
then
die "you cannot specify '--notify=sms' without '--sms-to'"
fi
if (( DO_NOTIFY_SMS != 0 )) && [[ -z "${ARG_SMS_PROVIDER}" ]]
then
die "you cannot specify '--notify=sms' without '--sms-provider'"
fi
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: exists_hc()
# DOES: check if a HC (function) exists in $FPATH
# EXPECTS: health check name [string]
# RETURNS: 0=HC not found; 1=HC found
# REQUIRES: n/a
function exists_hc
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset EXISTS_HC="$1"
typeset FDIR=""
typeset EXISTS_RC=0
# do not use a while-do loop here because mksh/pdksh does not pass updated
# variables back from the sub shell (only works for true ksh88/ksh93)
for FDIR in $(print "${FPATH}" | tr ':' ' ' 2>/dev/null)
do
$(data_contains_string "${FDIR}" "core")
if (( $? == 0 ))
then
ls "${FDIR}/${EXISTS_HC}" >/dev/null 2>&1 && EXISTS_RC=1
fi
done
return ${EXISTS_RC}
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: find_hc()
# DOES: find location of a HC (function) in $FPATH
# EXPECTS: health check name [string]
# RETURNS: file location [string]
# REQUIRES: n/a
function find_hc
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset FIND_HC="$1"
typeset FIND_PATH=""
typeset FDIR=""
print "${FPATH}" | tr ':' '\n' | grep -v "core$" | while read -r FDIR
do
ls "${FDIR}/${FIND_HC}" >/dev/null 2>&1 && print "${FDIR}/${FIND_HC}"
done
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: handle_hc()
# DOES: handle HC results
# EXPECTS: 1=HC name [string], $HC_MSG_FILE temporary file
# RETURNS: 0
# REQUIRES: die(), display_csv(), display_terse(), notify_mail(), notify_sms(),
# notify_eif(), warn()
function handle_hc
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset HC_NAME="$1"
typeset HC_STC_COUNT=0
typeset I=0
typeset MAX_I=0
typeset HC_STDOUT_LOG_SHORT=""
typeset HC_STDERR_LOG_SHORT=""
set -A HC_MSG_STC
set -A HC_MSG_TIME
set -A HC_MSG_TEXT
set -A HC_MSG_CUR_VAL # optional
set -A HC_MSG_EXP_VAL # optional
if [[ -s ${HC_MSG_FILE} ]]
then
# DEBUG: dump TMP file
if (( ARG_DEBUG != 0 ))
then
debug "begin dumping plugin messages file (${HC_MSG_FILE})"
cat ${HC_MSG_FILE} 2>/dev/null
debug "end dumping plugin messages file (${HC_MSG_FILE})"
fi
# process message file into arrays
while read HC_MSG_ENTRY
do
HC_MSG_STC[${I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $1'})
HC_MSG_TIME[${I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $2'})
HC_MSG_TEXT[${I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $3'})
HC_MSG_CUR_VAL[${I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $4'})
HC_MSG_EXP_VAL[${I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $5'})
I=$(( I + 1 ))
done <${HC_MSG_FILE} 2>/dev/null
fi
# display routines
if (( ${#HC_MSG_STC[*]} > 0 ))
then
if (( DO_DISPLAY_CSV == 1 ))
then
if (( HAS_DISPLAY_CSV == 1 ))
then
# call plugin
display_csv "${HC_NAME}" "${HC_FAIL_ID}"
else
warn "display_csv plugin is not avaible, cannot display_results!"
fi
elif (( DO_DISPLAY_INIT == 1 ))
then
if (( HAS_DISPLAY_INIT == 1 ))
then
# call plugin
display_init "${HC_NAME}" "${HC_FAIL_ID}"
else
warn "display_init plugin is not avaible, cannot display_results!"
fi
elif (( DO_DISPLAY_TERSE == 1 ))
then
if (( HAS_DISPLAY_TERSE == 1 ))
then
# call plugin
display_terse "${HC_NAME}" "${HC_FAIL_ID}"
else
warn "display_terse plugin is not avaible, cannot display_results!"
fi
else
# default STDOUT
if (( ARG_VERBOSE != 0 ))
then
I=0
MAX_I=${#HC_MSG_STC[*]}
while (( I < MAX_I ))
do
printf "%s" "INFO: ${HC_NAME} [STC=${HC_MSG_STC[${I}]}]: ${HC_MSG_TEXT[${I}]}"
if (( HC_MSG_STC[${I}] != 0 ))
then
printf " %s\n" "[FAIL_ID=${HC_FAIL_ID}]"
else
printf "\n"
fi
I=$(( I + 1 ))
done
fi
fi
fi
# log & notify routines
if (( ARG_LOG != 0 )) && (( ${#HC_MSG_STC[*]} > 0 ))
then
# log routine (combined STC=0 or <>0)
I=0
MAX_I=${#HC_MSG_STC[*]}
while (( I < MAX_I ))
do
printf "%s${SEP}%s${SEP}%s${SEP}%s${SEP}" \
"${HC_MSG_TIME[${I}]}" \
"${HC_NAME}" \
${HC_MSG_STC[${I}]} \
"${HC_MSG_TEXT[${I}]}" >>${HC_LOG}
if (( HC_MSG_STC[${I}] != 0 ))
then
printf "%s${SEP}\n" "${HC_FAIL_ID}" >>${HC_LOG}
else
printf "\n" >>${HC_LOG}
fi
HC_STC_COUNT=$(( HC_STC_COUNT + HC_MSG_STC[${I}] ))
I=$(( I + 1 ))
done
# notify routine (combined STC > 0)
if (( HC_STC_COUNT > 0 ))
then
# save stdout/stderr to HC events location
if [[ -s ${HC_STDOUT_LOG} ]] || [[ -s ${HC_STDERR_LOG} ]]
then
# organize logs in sub-directories: YYYY/MM
mkdir -p "${EVENTS_DIR}/${DIR_PREFIX}/${HC_FAIL_ID}" >/dev/null 2>&1 || \
die "failed to create event directory at $1"
if [[ -f ${HC_STDOUT_LOG} ]]
then
# cut off the path and the .$$ part from the file location
HC_STDOUT_LOG_SHORT="${HC_STDOUT_LOG##*/}"
mv ${HC_STDOUT_LOG} "${EVENTS_DIR}/${DIR_PREFIX}/${HC_FAIL_ID}/${HC_STDOUT_LOG_SHORT%.*}" >/dev/null 2>&1 || \
die "failed to move ${HC_STDOUT_LOG} to event directory at $1"
fi
if [[ -f ${HC_STDERR_LOG} ]]
then
# cut off the path and the .$$ part from the file location
HC_STDERR_LOG_SHORT="${HC_STDERR_LOG##*/}"
mv ${HC_STDERR_LOG} "${EVENTS_DIR}/${DIR_PREFIX}/${HC_FAIL_ID}/${HC_STDERR_LOG_SHORT%.*}" >/dev/null 2>&1 || \
die "failed to move ${HC_STDERR_LOG} to event directory at $1"
fi
fi
# notify if needed (i.e. when we have HC failures)
# by mail?
if (( DO_NOTIFY_MAIL == 1 ))
then
if (( HAS_NOTIFY_MAIL == 1 ))
then
# call plugin (pick up HC failure/stdout/stderr files in notify_mail())
notify_mail "${HC_NAME}" "${HC_FAIL_ID}"
else
warn "notify_mail plugin is not avaible, cannot send alert via e-mail!"
fi
fi
# by sms?
if (( DO_NOTIFY_SMS == 1 ))
then
if (( HAS_NOTIFY_SMS == 1 ))
then
# call plugin
notify_sms "${HC_NAME}" "${HC_FAIL_ID}"
else
warn "notify_sms plugin is not avaible, cannot send alert via sms!"
fi
fi
# by EIF?
if (( DO_NOTIFY_EIF == 1 ))
then
if (( HAS_NOTIFY_EIF == 1 ))
then
# call plugin
notify_eif "${HC_NAME}" "${HC_FAIL_ID}"
else
warn "notify_sms plugin is not avaible, cannot send alert via sms!"
fi
fi
fi
fi
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: handle_timeout()
# DOES: kill long running background jobs
# EXPECTS: ${CHILD_PID} to be populated
# RETURNS: 0
# REQUIRES: warn()
function handle_timeout
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
[[ -n "${CHILD_PID}" ]] && kill -s TERM ${CHILD_PID}
warn "child process with PID ${CHILD_PID} has been forcefully stopped"
CHILD_ERROR=1
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: init_check_host
# DOES: init full host check
# EXPECTS: n/a
# RETURNS: 0
# REQUIRES: die()
function init_check_host
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset DUMMY=""
typeset HC_CONFIG=""
typeset HC_DESC=""
typeset HC_EXEC=""
typeset REPORT_STYLE=""
[[ -r ${HOST_CONFIG_FILE} ]] || die "unable to read configuration file at ${HOST_CONFIG_FILE}"
# read required config values
REPORT_STYLE="$(grep -i '^report_style=' ${HOST_CONFIG_FILE} | cut -f2 -d'=' | tr -d '\"')"
case "${REPORT_STYLE}" in
csv|CSV) # csv format
if (( HAS_DISPLAY_CSV == 1 ))
then
DO_DISPLAY_CSV=1
ARG_VERBOSE=0
fi
;;
terse|TERSE) # terse format
if (( HAS_DISPLAY_TERSE == 1 ))
then
DO_DISPLAY_TERSE=1
ARG_VERBOSE=0
fi
;;
*) # init/boot default, stdout fallback
if (( HAS_DISPLAY_INIT == 1 ))
then
DO_DISPLAY_INIT=1
ARG_VERBOSE=0
else
ARG_VERBOSE=1
warn "default boot/init display plugin not present"
fi
esac
# mangle $ARG_HC to build the full list of HCs to be executed
ARG_HC=""
grep -i '^hc:' ${HOST_CONFIG_FILE} 2>/dev/null |\
while IFS=':' read DUMMY HC_EXEC HC_CONFIG HC_DESC
do
ARG_HC="${ARG_HC},${HC_EXEC}"
done
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: init_hc
# DOES: init routines for HC/core plugins
# EXPECTS: 1=HC name [string], 2=list of platforms [string], 3=HC version [string]
# RETURNS: 0
# REQUIRES: die()
function init_hc
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset HC_PLATFORMS="$2"
typeset HC_VERSION="$3"
typeset HC_OK=0
# check platform (don't use a pattern comparison here (~! mksh/pdksh))
HC_OK=$(print "${HC_PLATFORMS}" | grep -c "${OS_NAME}" 2>/dev/null)
(( HC_OK != 0 )) || die "may only run on platform(s): ${HC_PLATFORMS}"
# check version of HC plugin
case "${HC_VERSION}" in
[0-2][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9])
# OK
(( ARG_DEBUG != 0 )) && debug "HC plugin $1 has version ${HC_VERSION}"
;;
*)
die "version of the HC plugin $1 is not in YYYY-MM-DD format (${HC_VERSION})"
;;
esac
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: is_scheduled()
# DOES: check if a HC is scheduled in a cron
# EXPECTS: health check name [string]
# RETURNS: 0=HC not scheduled; <>0=HC is scheduled
# REQUIRES: n/a
function is_scheduled
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset CRON_HC="$1"
typeset CRON_COUNT=0
typeset CRON_SYS_LOCATIONS='/etc/crontab /etc/cron.d/*'
typeset CRON_ANACRON_LOCATIONS='/etc/anacrontab /etc/cron.*'
# check for a scheduled job
case "${OS_NAME}" in
"Linux")
# check default root crontab
CRON_COUNT=$(crontab -l 2>/dev/null | grep -c -E -e "^[^#].*${CRON_HC}" 2>/dev/null)
# check system crontabs
if (( CRON_COUNT == 0 ))
then
CRON_COUNT=$(cat ${CRON_SYS_LOCATIONS} 2>/dev/null | grep -c -E -e "^[^#].*${CRON_HC}" 2>/dev/null)
fi
# check anacron
if (( CRON_COUNT == 0 ))
then
CRON_COUNT=$(cat ${CRON_ANACRON_LOCATIONS} 2>/dev/null | grep -c -E -e "^[^#].*${CRON_HC}" 2>/dev/null)
fi
;;
*)
# use default root crontab
CRON_COUNT=$(crontab -l 2>/dev/null | grep -c -E -e "^[^#].*${CRON_HC}" 2>/dev/null)
esac
return ${CRON_COUNT}
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: list_core()
# DOES: find HC core plugins
# EXPECTS: action identifier [string]
# RETURNS: 0
# REQUIRES: n/a
function list_core
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset FACTION="$1"
typeset FCONFIG=""
typeset FDIR=""
typeset FNAME=""
typeset FVERSION=""
typeset FCONFIG=""
typeset FSTATE="enabled" # default
typeset FFILE=""
typeset HAS_FCONFIG=0
typeset HC_VERSION=""
# print header
if [[ "${FACTION}" != "list" ]]
then
printf "%-30s\t%-8s\t%s\t\t%s\n" "Core plugin" "State" "Version" "Config?"
printf "%80s\n" | tr ' ' -
fi
print "${FPATH}" | tr ':' '\n' | grep "core$" | sort 2>/dev/null | while read -r FDIR
do
# exclude core helper librar(y|ies)
ls -1 ${FDIR}/*.sh 2>/dev/null | grep -v "include_" | sort 2>/dev/null | while read -r FFILE
do
# find function name but skip helper functions in the plug-in file (function _name)
FNAME=$(grep -E -e "^function[[:space:]]+[^_]" "${FFILE}" 2>&1)
# look for version string (cut off comments but don't use [:space:] in tr)
FVERSION=$(grep '^typeset _VERSION=' "${FFILE}" 2>&1 | tr -d '\"' | tr -d ' \t' | cut -f1 -d'#' | cut -f2 -d'=')
# look for configuration file string
HAS_FCONFIG=$(grep -c '^typeset _CONFIG_FILE=' "${FFILE}" 2>&1)
if (( HAS_FCONFIG != 0 ))
then
FCONFIG="Yes"
else
FCONFIG="No"
fi
# check state (only for unlinked)
[[ -h ${FFILE%%.*} ]] || FSTATE="unlinked"
# show results
if [[ "${FACTION}" != "list" ]]
then
printf "%-30s\t%-8s\t%s\t%s\n" \
"${FNAME#function *}" \
"${FSTATE}" \
"${FVERSION#typeset _VERSION=*}" \
"${FCONFIG}"
else
printf "%s\n" "${FNAME#function *}"
fi
done
done
# dead link detection
if [[ "${FACTION}" != "list" ]]
then
print
print -n "Dead links: "
print "${FPATH}" | tr ':' '\n' | grep "core$" | while read -r FDIR
do
# do not use 'find -type l' here!
ls ${FDIR} 2>/dev/null | grep -v "\." | while read -r FFILE
do
if [[ -h "${FDIR}/${FFILE}" ]] && [[ ! -f "${FDIR}/${FFILE}" ]]
then
printf "%s " ${FFILE##*/}
fi
done
done
print
# show FPATH
print
print "current FPATH: ${FPATH}"
fi
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: list_hc()
# DOES: find HC plugins/functions
# EXPECTS: action identifier [string]
# RETURNS: 0
# REQUIRES: is_scheduled()
function list_hc
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset FACTION="$1"
typeset FNEEDLE="$2"
typeset FCONFIG=""
typeset FDIR=""
typeset FNAME=""
typeset FVERSION=""
typeset FCONFIG=""
typeset FSTATE=""
typeset FFILE=""
typeset HAS_FCONFIG=0
typeset FSCHEDULED=0
typeset DISABLE_FFILE=""
typeset HC_VERSION=""
# build search needle
if [[ -z "${ARG_LIST}" ]]
then
FNEEDLE="*.sh"
else
FNEEDLE="${ARG_LIST}.sh"
fi
# print header
if [[ "${FACTION}" != "list" ]]
then
printf "%-30s\t%-8s\t%s\t\t%s\t%s\n" "Health Check" "State" "Version" "Config?" "Sched?"
printf "%80s\n" | tr ' ' -
fi
print "${FPATH}" | tr ':' '\n' | grep -v "core$" | sort 2>/dev/null | while read -r FDIR
do
ls -1 ${FDIR}/${FNEEDLE} 2>/dev/null | sort 2>/dev/null | while read -r FFILE
do
# find function name but skip helper functions in the plug-in file (function _name)
FNAME=$(grep -E -e "^function[[:space:]]+[^_]" "${FFILE}" 2>&1)
# look for version string (cut off comments but don't use [:space:] in tr)
FVERSION=$(grep '^typeset _VERSION=' "${FFILE}" 2>&1 | tr -d '\"' | tr -d ' \t' | cut -f1 -d'#' | cut -f2 -d'=')
# look for configuration file string
HAS_FCONFIG=$(grep -c '^typeset _CONFIG_FILE=' "${FFILE}" 2>&1)
if (( HAS_FCONFIG != 0 ))
then
FCONFIG="Yes"
else
FCONFIG="No"
fi
# check state
DISABLE_FFILE="$(print ${FFILE##*/} | sed 's/\.sh$//')"
if [[ -f "${STATE_PERM_DIR}/${DISABLE_FFILE}.disabled" ]]
then
FSTATE="disabled"
else
FSTATE="enabled"
fi
[[ -h ${FFILE%%.*} ]] || FSTATE="unlinked"
# check scheduling
is_scheduled "${FNAME#function *}"
if (( $? == 0 ))
then
FSCHEDULED="No"
else
FSCHEDULED="Yes"
fi
# show results
if [[ "${FACTION}" != "list" ]]
then
printf "%-30s\t%-8s\t%s\t%s\t%s\n" \
"${FNAME#function *}" \
"${FSTATE}" \
"${FVERSION#typeset _VERSION=*}" \
"${FCONFIG}" \
"${FSCHEDULED}"
else
printf "%s\n" "${FNAME#function *}"
fi
done
done
# dead link detection
if [[ "${FACTION}" != "list" ]]
then
print
print -n "Dead links: "
print "${FPATH}" | tr ':' '\n' | grep -v "core" | while read -r FDIR
do
# do not use 'find -type l' here!
ls ${FDIR} 2>/dev/null | grep -v "\." | while read -r FFILE
do
if [[ -h "${FDIR}/${FFILE}" ]] && [[ ! -f "${FDIR}/${FFILE}" ]]
then
printf "%s " ${FFILE##*/}
fi
done
done
print
# show FPATH
print
print "current FPATH: ${FPATH}"
fi
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: log()
# DOES: handle messages
# EXPECTS: log message [string]
# RETURNS: 0
# REQUIRES: n/a
function log
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset NOW="$(date '+%d-%h-%Y %H:%M:%S')"
typeset LOG_LINE=""
if [[ -n "$1" ]]
then
if (( ARG_LOG != 0 ))
then
print - "$*" | while read -r LOG_LINE
do
print "${NOW}: INFO: [$$]:" "${LOG_LINE}" >>${LOG_FILE}
done
fi
if (( ARG_VERBOSE != 0 ))
then
print - "$*" | while read -r LOG_LINE
do
print "INFO:" "${LOG_LINE}"
done
fi
fi
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: log_hc()
# DOES: log a HC plugin result
# EXPECTS: 1=HC name [string], 2=HC status code [integer], 3=HC message [string],
# 4=HC found value [string] (optional),
# 5=HC expected value [string] (optional)
# RETURNS: 0
# REQUIRES: n/a
function log_hc
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset HC_NAME="$1"
typeset HC_STC=$2
typeset HC_MSG="$3"
typeset HC_NOW="$(date '+%Y-%m-%d %H:%M:%S' 2>/dev/null)"
typeset HC_MSG_CUR_VAL=""
typeset HC_MSG_EXP_VAL=""
# assign optional parameters
[[ -n "$4" ]] && HC_MSG_CUR_VAL=$(data_newline2hash "$4")
[[ -n "$5" ]] && HC_MSG_EXP_VAL=$(data_newline2hash "$5")
# save the HC failure message for now
print "${HC_STC}%%${HC_NOW}%%${HC_MSG}%%${HC_MSG_CUR_VAL}%%${HC_MSG_EXP_VAL}" \
>>${HC_MSG_FILE}
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: stat_hc()
# DOES: retrieve status of a HC
# EXPECTS: HC name [string]
# RETURNS: 0=HC is disabled; 1=HC is enabled
# REQUIRES: n/a
function stat_hc
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset STAT_HC="$1"
typeset STAT_RC=1 # default: enabled
[[ -f "${STATE_PERM_DIR}/${STAT_HC}.disabled" ]] && STAT_RC=0
return ${STAT_RC}
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: warn()
# DOES: handle warnings
# EXPECTS: log message [string]
# RETURNS: 0
# REQUIRES: n/a
function warn
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset NOW="$(date '+%d-%h-%Y %H:%M:%S')"
typeset LOG_LINE=""
if [[ -n "$1" ]]
then
if (( ARG_LOG != 0 ))
then
print - "$*" | while read -r LOG_LINE
do
print "${NOW}: WARN: [$$]:" "${LOG_LINE}" >>${LOG_FILE}
done
fi
if (( ARG_VERBOSE != 0 ))
then
print - "$*" | while read -r LOG_LINE
do
print "WARN:" "${LOG_LINE}"
done
fi
fi
return 0
}
#******************************************************************************
# END of script
#******************************************************************************
+504
View File
@@ -0,0 +1,504 @@
#!/usr/bin/env ksh
#******************************************************************************
# @(#) include_data.sh
#******************************************************************************
# @(#) Copyright (C) 2017 by KUDOS BVBA (info@kudos.be). All rights reserved.
#
# This program is a free software; you can redistribute it and/or modify
# it under the same terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
#******************************************************************************
#
# DOCUMENTATION (MAIN)
# -----------------------------------------------------------------------------
# @(#) MAIN: include_data
# DOES: helper functions for data (manipulation)
#
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#******************************************************************************
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_get_lvalue_from_config()
# DOES: get an lvalue from the configuration file
# EXPECTS: parameter to look for [string]
# OUTPUTS: parameter value [string]
# RETURNS: 0=found; 1=not found
# REQUIRES: n/a
function data_get_lvalue_from_config
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset _PARAMETER="${1}"
typeset _LVALUE=""
typeset _RC=0
_LVALUE=$(grep -i "^${_PARAMETER}=" ${_CONFIG_FILE} | cut -f2 -d'=')
if [[ -n "${_LVALUE}" ]]
then
print $(data_dequote "${_LVALUE}")
else
_RC=1
fi
return ${_RC}
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_chop()
# DOES: cut last character of input
# EXPECTS: string
# OUTPUTS: string with last character omitted
# RETURNS: 0
# REQUIRES: n/a
function data_chop
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
print "${1%?}"
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_contains_string()
# DOES: checks if a string (haystack) contains a substring (needle).
# EXPECTS: $1=haystack [string]; $2=needle [string]
# OUTPUTS: n/a
# RETURNS: 0=not found; 1=found
# REQUIRES: n/a
function data_contains_string
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset _HAYSTACK="$1"
typeset _NEEDLE="$2"
typeset _RC=0
[[ "${_HAYSTACK#*${_NEEDLE}}" = "${_HAYSTACK}" ]] || _RC=1
return ${_RC}
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_decomma()
# DOES: remove comma's
# EXPECTS: [string] with comma's
# OUTPUTS: [string] without comma's
# RETURNS: 0
# REQUIRES: n/a
function data_decomma
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
print "${1}" | tr -d ',' 2>/dev/null
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_decomma_last()
# DOES: remove last comma
# EXPECTS: [string] with a last comma
# OUTPUTS: [string] without last comma
# RETURNS: 0
# REQUIRES: n/a
function data_decomma_last
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
print "${1%*,}"
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_decomma_first()
# DOES: remove last comma
# EXPECTS: [string] with a last comma
# OUTPUTS: [string] without last comma
# RETURNS: 0
# REQUIRES: n/a
function data_decomma_first
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
print "${1#,*}"
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_dequote()
# DOES: remove quotes
# EXPECTS: quoted [string]
# OUTPUTS: de-quoted (both double and single, but not escaped) [string]
# RETURNS: 0
# REQUIRES: n/a
function data_dequote
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
print "${1}" | tr -d '\"' 2>/dev/null | tr -d "'" 2>/dev/null
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_comma2space()
# DOES: replace commas with a space
# EXPECTS: [string] with commas
# OUTPUTS: [string] with spaces
# RETURNS: 0
# REQUIRES: n/a
function data_comma2space
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
print "${1}" | tr ',' ' ' 2>/dev/null
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_comma2newline()
# DOES: replace commas with a space
# EXPECTS: [string] with commas
# OUTPUTS: [string] with newlines
# RETURNS: 0
# REQUIRES: n/a
function data_comma2newline
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
print "${1}" | tr ',' '\n' 2>/dev/null
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_newline2comma()
# DOES: replace newlines with a comma
# EXPECTS: [string] with newlines
# OUTPUTS: [string] with commas
# RETURNS: 0
# REQUIRES: n/a
function data_newline2comma
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
print "${1}" | tr '\n' ',' 2>/dev/null
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_newline2hash()
# DOES: replace newlines with a hash
# EXPECTS: [string] with newlines
# OUTPUTS: [string] with hashes
# RETURNS: 0
# REQUIRES: n/a
function data_newline2hash
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
print "${1}" | tr '\n' '#' 2>/dev/null | tr '\r' '#' 2>/dev/null
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_space2comma()
# DOES: replace spaces with a comma
# EXPECTS: [string] with spaces
# OUTPUTS: [string] with commas
# RETURNS: 0
# REQUIRES: n/a
function data_space2comma
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
print "${1}" | tr -s ' ' 2>/dev/null | tr ' ' ',' 2>/dev/null
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_space2hash()
# DOES: replace spaces with a hash
# EXPECTS: [string] with spaces
# OUTPUTS: [string] with hashes
# RETURNS: 0
# REQUIRES: n/a
function data_space2hash
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
print "${1}" | tr -s ' ' 2>/dev/null | tr ' ' '#' 2>/dev/null
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_strip_newline()
# DOES: remove newlines
# EXPECTS: [string] with newlines
# OUTPUTS: [string] without newlines
# RETURNS: 0
# REQUIRES: n/a
function data_strip_newline
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
print "${1}" | tr -d '\n' 2>/dev/null | tr -d '\r' 2>/dev/null
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_lc()
# DOES: switch to lower case
# EXPECTS: [string]
# OUTPUTS: lower case [string]
# RETURNS: 0
# REQUIRES: n/a
function data_lc
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
print "${1}" | tr '[:upper:]' '[:lower:]' 2>/dev/null
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_uc()
# DOES: switch to upper case
# EXPECTS: [string]
# OUTPUTS: upper case [string]
# RETURNS: 0
# REQUIRES: n/a
function data_uc
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
print "${1}" | tr '[:lower:]' '[:upper:]' 2>/dev/null
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_encode_url
# DOES: encode URL data
# EXPECTS: text to be encoded [string]
# OUTPUTS: encoded text [string]
# RETURNS: 0
# REQUIRES:
# REFERENCE: added from http://www.shelldorado.com/scripts/cmds/urlencode
function data_encode_url
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset _EncodeEOL=0
LANG=C awk '
BEGIN {
# We assume an awk implementation that is just plain dumb.
# We will convert an character to its ASCII value with the
# table ord[], and produce two-digit hexadecimal output
# without the printf("%02X") feature.
EOL = "%0A" # "end of line" string (encoded)
split ("1 2 3 4 5 6 7 8 9 A B C D E F", hextab, " ")
hextab [0] = 0
for ( i=1; i<=255; ++i ) ord [ sprintf ("%c", i) "" ] = i + 0
if ("'"$_EncodeEOL"'" == "yes") _EncodeEOL = 1; else _EncodeEOL = 0
}
{
encoded = ""
for ( i=1; i<=length ($0); ++i ) {
c = substr ($0, i, 1)
if ( c ~ /[a-zA-Z0-9.-]/ ) {
encoded = encoded c # safe character
} else if ( c == " " ) {
encoded = encoded "+" # special handling
} else {
# unsafe character, encode it as a two-digit hex-number
lo = ord [c] % 16
hi = int (ord [c] / 16);
encoded = encoded "%" hextab [hi] hextab [lo]
}
}
if ( _EncodeEOL ) {
printf ("%s", encoded EOL)
} else {
print encoded
}
}
END {
#if ( _EncodeEOL ) print ""
}
' "$@"
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_dot2ip()
# DOES: converts a dotted-decimal IPv4 address representation to the 32 bit number.
# EXPECTS: dotted-decimal IPv4 address [string]
# OUTPUTS: 32 bit number [string]
# REQUIRES: n/a
# REFERENCE: https://raw.githubusercontent.com/dualbus/tutorial_nmap/master/iprange
function data_dot2ip
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset _DOT="$1"
typeset _IP=0
typeset _OLD_IFS="${IFS}"
IFS="."
set -A _COMPS ${_DOT}
IFS="${OLD_IFS}"
_IP=$((_IP | ((_COMPS[0] & 255) << 24) ))
_IP=$((_IP | ((_COMPS[1] & 255) << 16) ))
_IP=$((_IP | ((_COMPS[2] & 255) << 8) ))
_IP=$((_IP | ((_COMPS[3] & 255) << 0) ))
print "${_IP}"
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_ip2dot()
# DOES: converts a 32 bit unsigned integer to dotted-decimal notation.
# EXPECTS: 32 bit unsigned integer [string]
# OUTPUTS: dotted-decimal [string]
# RETURNS: 0
# REQUIRES: n/a
# REFERENCE: https://raw.githubusercontent.com/dualbus/tutorial_nmap/master/iprange
function data_ip2dot
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset _IP="$1"
typeset _W=""
typeset _X=""
typeset _Y=""
typeset _Z=""
_W=$(( (_IP >> 24) & 255 ))
_X=$(( (_IP >> 16) & 255 ))
_Y=$(( (_IP >> 8) & 255 ))
_Z=$(( (_IP >> 0) & 255 ))
print "${_W}.${_X}.${_Y}.${_Z}"
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_bits2mask()
# DOES: converts bits notation (prefix) to a dotted-decimal representation
# EXPECTS: netmask in prefix [string]
# OUTPUTS: netmask in dotted decimal notation [string]
# RETURNS: 0
# REQUIRES: n/a
# REFERENCE: https://raw.githubusercontent.com/dualbus/tutorial_nmap/master/iprange
function data_bits2mask
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset _BITS=$1
typeset _MAX=4294967296
typeset _OFFSET=0
case "${_BITS}" in
0) _OFFSET=${_MAX} ;;
1) _OFFSET=2147483648 ;;
2) _OFFSET=1073741824 ;;
3) _OFFSET=536870912 ;;
4) _OFFSET=268435456 ;;
5) _OFFSET=134217728 ;;
6) _OFFSET=67108864 ;;
7) _OFFSET=33554432 ;;
8) _OFFSET=16777216 ;;
9) _OFFSET=8388608 ;;
10) _OFFSET=4194304 ;;
11) _OFFSET=2097152 ;;
12) _OFFSET=1048576 ;;
13) _OFFSET=524288 ;;
14) _OFFSET=262144 ;;
15) _OFFSET=131072 ;;
16) _OFFSET=65536 ;;
17) _OFFSET=32768 ;;
18) _OFFSET=16384 ;;
19) _OFFSET=8192 ;;
20) _OFFSET=4096 ;;
21) _OFFSET=2048 ;;
22) _OFFSET=1024 ;;
23) _OFFSET=512 ;;
24) _OFFSET=256 ;;
25) _OFFSET=128 ;;
26) _OFFSET=64 ;;
27) _OFFSET=32 ;;
28) _OFFSET=16 ;;
29) _OFFSET=8 ;;
30) _OFFSET=4 ;;
31) _OFFSET=2 ;;
32) _OFFSET=1 ;;
esac
data_ip2dot "$(( (_MAX - 1) & ~(_OFFSET - 1) ))"
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_mask2bits()
# DOES: converts decimal netmask to bits notation (prefix)
# EXPECTS: netmask in dotted decimal notation [string]
# OUTPUTS: netmask in prefix [string]
# RETURNS: 0
# REQUIRES: n/a
# REFERENCE: https://raw.githubusercontent.com/dualbus/tutorial_nmap/master/iprange
function data_mask2bits
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset _MASK="$1"
typeset -i _I=32
while (( _I > 0 ))
do
[[ "${_MASK}" = $(data_bits2mask "${_I}") ]] && print "${_I}"
_I=$(( _I - 1 ))
done
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: data_is_IPv4()
# DOES: checks if an IP address is in IPv4 dotted notation
# EXPECTS: IPv4 address in decimal notation [string]
# RETURNS: 0=not IPv4; <>0=IPv4
# REQUIRES: n/a
function data_is_ipv4
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset _IP="$1"
typeset _RC=0
_RC=$(print "${_IP}" | grep -c -E -e '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' 2>/dev/null)
return ${_RC}
}
#******************************************************************************
# END of script
#******************************************************************************
+211
View File
@@ -0,0 +1,211 @@
#!/usr/bin/env ksh
#******************************************************************************
# @(#) include_os.sh
#******************************************************************************
# @(#) Copyright (C) 2017 by KUDOS BVBA (info@kudos.be). All rights reserved.
#
# This program is a free software; you can redistribute it and/or modify
# it under the same terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
#******************************************************************************
#
# DOCUMENTATION (MAIN)
# -----------------------------------------------------------------------------
# @(#) MAIN: include_OS
# DOES: helper functions for OS related functions
#
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#******************************************************************************
# -----------------------------------------------------------------------------
# @(#) FUNCTION: linux_get_distro()
# DOES: get Linux distribution name & version, sets $LINUX_DISTRO & $LINUX_RELEASE
# EXPECTS: n/a
# RETURNS: 0
# REQUIRES: n/a
function linux_get_distro
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
# linux only
check_platform 'Linux' || {
(( ARG_DEBUG != 0 )) && debug "may only run on platform(s): Linux"
return 1
}
# try LSB first (good for Ubuntu & derivatives)
if [[ -f /etc/lsb-release ]]
then
. /etc/lsb-release
LINUX_DISTRO="${DISTRIB_ID}"
LINUX_RELEASE="${DISTRIB_RELEASE}"
fi
# LSB turned up zippo, start digging
if [[ -z "${LINUX_DISTRO}" || -z "${LINUX_RELEASE}" ]]
then
if [[ -f /etc/debian_version ]]
then
LINUX_DISTRO="Debian"
LINUX_RELEASE=$(</etc/debian_version 2>/dev/null)
elif [[ -f /etc/SuSE-release ]]
then
LINUX_DISTRO="SuSE"
LINUX_RELEASE=$(grep 'VERSION' /etc/SuSE-release 2>/dev/null | cut -f2 -d'=' | tr -d ' ')
[[ -n "${LINUX_RELEASE}" ]] || LINUX_RELEASE=$(grep 'CPE_NAME' /etc/os-release 2>/dev/null | cut -f2 -d'=' | cut -f5 -d':')
elif [[ -f /etc/redhat-release ]]
then
LINUX_DISTRO="Redhat"
# system-release-cpe is present since Fedora 9 [cpe:/o:centos:linux:6:GA]
LINUX_RELEASE=$(cut -f5 -d':' </etc/system-release-cpe 2>/dev/null)
else
LINUX_DISTRO="${OS_NAME}"
LINUX_RELEASE="unknown"
fi
fi
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: linux_get_init()
# DOES: get Linux init mechanism, sets $LINUX_INIT
# EXPECTS: n/a
# RETURNS: 0
# REQUIRES: n/a
function linux_get_init
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
# linux only
check_platform 'Linux' || {
(( ARG_DEBUG != 0 )) && debug "may only run on platform(s): Linux"
return 1
}
# default is sysv
LINUX_INIT="sysv"
if [[ -r /usr/lib/systemd ]]
then
LINUX_INIT="systemd"
elif [[ -r /usr/share/upstart ]]
then
LINUX_INIT="upstart"
fi
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: linux_has_crm()
# DOES: check if Corosync (CRM version) is running
# EXPECTS: n/a
# OUTPUTS: 0=not active/installed; 1=active/installed
# RETURNS: 0=success; 1=error
# REQUIRES: n/a
function linux_has_crm
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset _CRM_BIN=""
typeset _HAS_CRM=0
# linux only
check_platform 'Linux' || {
warn "may only run on platform(s): Linux"
return 1
}
_CRM_BIN="$(which crm 2>/dev/null)"
if [[ -x ${_CRM_BIN} && -n "${_CRM_BIN}" ]]
then
# check for active
crm status >/dev/null 2>/dev/null
(( $? == 0 )) && _HAS_CRM=1
else
(( ARG_DEBUG != 0 )) && debug "corosync (crm) is not active here"
return 1
fi
print ${_HAS_CRM}
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: linux_has_docker()
# DOES: check if docker is running
# EXPECTS: n/a
# OUTPUTS: 0=not active/installed; 1=active/installed
# RETURNS: 0=success; 1=error
# REQUIRES: n/a
function linux_has_docker
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset _DOCKER_BIN=""
typeset _HAS_DOCKER=0
# linux only
check_platform 'Linux' || {
warn "may only run on platform(s): Linux"
return 1
}
_DOCKER_BIN="$(which docker 2>/dev/null)"
if [[ -x ${_DOCKER_BIN} && -n "${_DOCKER_BIN}" ]]
then
# check for active
docker ps >/dev/null 2>/dev/null
(( $? == 0 )) && _HAS_DOCKER=1
else
(( ARG_DEBUG != 0 )) && debug "docker is not active here"
return 1
fi
print ${_HAS_DOCKER}
return 0
}
# -----------------------------------------------------------------------------
# @(#) FUNCTION: linux_has_nm()
# DOES: check if NetworkManager is running
# EXPECTS: n/a
# OUTPUTS: 0=not active/installed; 1=active/installed
# RETURNS: 0=success; 1=error
# REQUIRES: n/a
function linux_has_nm
{
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
typeset _NMCLI_BIN=""
typeset _HAS_NM=0
# linux only
check_platform 'Linux' || {
warn "may only run on platform(s): Linux"
return 1
}
_NMCLI_BIN="$(which nmcli 2>/dev/null)"
if [[ -x ${_NMCLI_BIN} && -n "${_NMCLI_BIN}" ]]
then
# check for active
_HAS_NM=$(nmcli networking 2>/dev/null | grep -c -i "enabled")
else
(( ARG_DEBUG != 0 )) && debug "NetworkManager is not active here"
return 1
fi
print ${_HAS_NM}
return 0
}
#******************************************************************************
# END of script
#******************************************************************************
+126
View File
@@ -0,0 +1,126 @@
#!/usr/bin/env ksh
#******************************************************************************
# @(#) notify_eif.sh
#******************************************************************************
# @(#) Copyright (C) 2017 by KUDOS BVBA (info@kudos.be). All rights reserved.
#
# This program is a free software; you can redistribute it and/or modify
# it under the same terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
#******************************************************************************
#
# DOCUMENTATION (MAIN)
# -----------------------------------------------------------------------------
# @(#) MAIN: notify_eif
# DOES: send alert via posteifmsg
# EXPECTS: HC name [string]
# RETURNS: 0
# REQUIRES: handle_timeout(), init_hc(), log(), warn()
# INFO: https://www-01.ibm.com/support/knowledgecenter/SSSHTQ_8.1.0/com.ibm.netcool_OMNIbus.doc_8.1.0/omnibus/wip/eifsdk/reference/omn_eif_posteifmsg.html
#
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#******************************************************************************
# -----------------------------------------------------------------------------
function notify_eif
{
# ------------------------- CONFIGURATION starts here -------------------------
typeset _CONFIG_FILE="${CONFIG_DIR}/core/providers/$0.conf"
typeset _VERSION="2016-03-04" # YYYY-MM-DD
typeset _SUPPORTED_PLATFORMS="AIX,HP-UX,Linux" # uname -s match
# ------------------------- CONFIGURATION ends here ---------------------------
# set defaults
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
init_hc "$0" "${_SUPPORTED_PLATFORMS}" "${_VERSION}"
typeset _EIF_MESSAGE="$1 alert with ID ${HC_FAIL_ID}"
typeset _EIF_CLASS="${SCRIPT_NAME}"
typeset _EIF_BIN=""
typeset _EIF_ETC=""
typeset _EIF_SEVERITY=""
typeset _TIME_OUT=10
typeset _CHILD_ERROR=0
typeset _OWNER_PID=0
typeset _SLEEP_PID=0
typeset _CHILD_RC=0
# handle config file
if [[ ! -r ${_CONFIG_FILE} ]]
then
warn "unable to read configuration file at ${_CONFIG_FILE}"
return 1
fi
# read required config values
_EIF_BIN="$(grep -i '^EIF_BIN=' ${_CONFIG_FILE} | cut -f2 -d'=' | tr -d '\"')"
if [[ -z "${_EIF_BIN}" ]]
then
warn "no value set for 'EIF_BIN' in ${_CONFIG_FILE}"
return 1
fi
_EIF_ETC="$(grep -i '^EIF_ETC=' ${_CONFIG_FILE} | cut -f2 -d'=' | tr -d '\"')"
if [[ -z "${_EIF_ETC}" ]]
then
warn "no value set for 'EIF_ETC' in ${_CONFIG_FILE}"
return 1
fi
_EIF_SEVERITY="$(grep -i '^EIF_SEVERITY=' ${_CONFIG_FILE} | cut -f2 -d'=' | tr -d '\"')"
if [[ -z "${_EIF_SEVERITY}" ]]
then
warn "no value set for 'EIF_SEVERITY' in ${_CONFIG_FILE}"
return 1
fi
# send EIF
if [[ -x ${EIF_BIN} ]]
then
# set trap on SIGUSR1
trap "handle_timeout" USR1
# $PID is PID of the owner shell
_OWNER_PID=$$
(
# sleep for $_TIME_OUT seconds. If the sleep subshell is then still alive, send a SIGUSR1 to the owner
sleep ${_TIME_OUT}
kill -s USR1 ${_OWNER_PID} >/dev/null 2>&1
) &
# $_SLEEP_PID is the PID of the sleep subshell itself
_SLEEP_PID=$!
# do POSTEIFMSG in the background
${EIF_BIN} -f ${EIF_ETC} -r ${EIF_SEVERITY} -m "${_EIF_MESSAGE}" \
hostname=${HOST_NAME} sub_origin=part1 ${_EIF_CLASS} POST &
CHILD_PID=$!
log "spawning child process with time-out of ${_TIME_OUT} secs for EIF notify [PID=${CHILD_PID}]"
# wait for the command to complete
wait ${CHILD_PID}
# when the child completes, we can get rid of the sleep trigger
_CHILD_RC=$?
kill -s TERM ${_SLEEP_PID} >/dev/null 2>&1
# process return codes
if (( _CHILD_RC != 0 ))
then
warn "problem in sending alert via EIF [RC=${_CHILD_RC}]"
else
if (( _CHILD_ERROR == 0 ))
then
log "child process with PID ${CHILD_PID} ended correctly"
else
log "child process with PID ${CHILD_PID} did end correctly"
fi
fi
else
warn "could not sent alert via EIF (posteifmsg tool not found)"
fi
return 0
}
#******************************************************************************
# END of script
#******************************************************************************
+225
View File
@@ -0,0 +1,225 @@
#!/usr/bin/env ksh
#******************************************************************************
# @(#) notify_mail.sh
#******************************************************************************
# @(#) Copyright (C) 2017 by KUDOS BVBA (info@kudos.be). All rights reserved.
#
# This program is a free software; you can redistribute it and/or modify
# it under the same terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
#******************************************************************************
#
# DOCUMENTATION (MAIN)
# -----------------------------------------------------------------------------
# @(#) MAIN: notify_mail
# DOES: send e-mail alert
# EXPECTS: 1=HC name [string], 2=HC FAIL_ID [string]
# RETURNS: 0
# REQUIRES: curl, die(), init_hc(), log()
#
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#******************************************************************************
# -----------------------------------------------------------------------------
function notify_mail
{
# ------------------------- CONFIGURATION starts here -------------------------
typeset _VERSION="2017-05-17" # YYYY-MM-DD
typeset _SUPPORTED_PLATFORMS="AIX,HP-UX,Linux" # uname -s match
# ------------------------- CONFIGURATION ends here ---------------------------
# set defaults
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
init_hc "$0" "${_SUPPORTED_PLATFORMS}" "${_VERSION}"
typeset _MAIL_HC="$1"
typeset _MAIL_FAIL_ID="$2"
typeset _HC_BODY=""
set -A _MAIL_MSG_STC
set -A _MAIL_MSG_TIME
set -A _MAIL_MSG_TEXT
typeset _I=0
typeset _MAX_I=0
typeset _HC_STDOUT_LOG_SHORT=""
typeset _HC_STDERR_LOG_SHORT=""
typeset _MAIL_INFO_TPL="${CONFIG_DIR}/core/templates/mail_info.tpl"
typeset _MAIL_HEADER_TPL="${CONFIG_DIR}/core/templates/mail_header.tpl"
typeset _MAIL_BODY_TPL="${CONFIG_DIR}/core/templates/mail_body.tpl"
typeset _MAIL_FOOTER_TPL="${CONFIG_DIR}/core/templates/mail_footer.tpl"
typeset _MAIL_STDOUT_LOG=""
typeset _MAIL_STDERR_LOG=""
typeset _MAIL_STDOUT_MSG=""
typeset _MAIL_STDERR_MSG=""
typeset _MAIL_ATTACH_BIT=""
typeset _MAIL_METHOD=""
typeset _MAIL_RC=0
typeset _MAILX_BIN=""
typeset _MUTT_BIN=""
typeset _SENDMAIL_BIN=""
typeset _UUENCODE_BIN=""
typeset _TMP1_MAIL_FILE="${TMP_DIR}/.${SCRIPT_NAME}.mail.tmp1.$$"
typeset _TMP2_MAIL_FILE="${TMP_DIR}/.${SCRIPT_NAME}.mail.tmp2.$$"
typeset _NOW="$(date '+%d-%h-%Y %H:%M:%S')"
typeset _SUBJ_MSG="[${HOST_NAME}] HC ${_MAIL_HC} failed (${_NOW})"
typeset _FROM_MSG="${EXEC_USER}@${HOST_NAME}"
# set local trap for cleanup
trap "[[ -f ${_TMP1_MAIL_FILE} ]] && rm -f ${_TMP1_MAIL_FILE} >/dev/null 2>&1; [[ -f ${_TMP2_MAIL_FILE} ]] && rm -f ${_TMP2_MAIL_FILE} >/dev/null 2>&1; return 1" 1 2 3 15
# set short paths for STDOUT/STDERR logs
_HC_STDOUT_LOG_SHORT="${HC_STDOUT_LOG##*/}"
_HC_STDERR_LOG_SHORT="${HC_STDERR_LOG##*/}"
# check & determine mailer tools
case "${OS_NAME}" in
"Linux")
# prefer mutt :-)
_MUTT_BIN="$(which mutt 2>/dev/null)"
if [[ -x ${_MUTT_BIN} ]] && [[ -n "${_MUTT_BIN}" ]]
then
_MAIL_METHOD="mutt"
else
# prefer mailx next
_MAILX_BIN="$(which mailx 2>/dev/null)"
if [[ -x ${_MAILX_BIN} ]] && [[ -n "${_MAILX_BIN}" ]]
then
_MAIL_METHOD="mailx"
else
_MAIL_METHOD="sendmail"
fi
fi
;;
*)
_MAIL_METHOD="sendmail"
;;
esac
# check if fall-back & last resort exists
if [[ "${_MAIL_METHOD}" = "sendmail" ]]
then
# find 'sendmail'
_SENDMAIL_BIN="$(which sendmail 2>/dev/null)"
if [[ ! -x ${_SENDMAIL_BIN} ]] || [[ -z "${_SENDMAIL_BIN}" ]]
then
die "unable to send e-mail - sendmail is not installed here"
fi
# find 'uuencode'
_UUENCODE_BIN="$(which uuencode 2>/dev/null)"
if [[ ! -x ${_UUENCODE_BIN} ]] || [[ -z "${_UUENCODE_BIN}" ]]
then
die "unable to send e-mail - uuencode is not installed here"
fi
fi
# create info part (not for mailx or mutt)
if [[ "${_MAIL_METHOD}" = "sendmail" ]]
then
[[ -r "${_MAIL_INFO_TPL}" ]] || die "cannot read mail info template at ${_MAIL_INFO_TPL}"
eval "cat << __EOT
$(sed 's/[\$`]/\\&/g;s/<## @\([^ ]*\) ##>/${\1}/g' <${_MAIL_INFO_TPL})
__EOT" >${_TMP1_MAIL_FILE}
fi
# create header part
[[ -r "${_MAIL_HEADER_TPL}" ]] || die "cannot read mail header template at ${_MAIL_HEADER_TPL}"
eval "cat << __EOT
$(sed 's/[\$`]/\\&/g;s/<## @\([^ ]*\) ##>/${\1}/g' <${_MAIL_HEADER_TPL})
__EOT" >>${_TMP1_MAIL_FILE}
# create body part (max array size: 1023 in ksh88f, plugins spawning more than >1K messages are crazy :-))
while read HC_MSG_ENTRY
do
_MAIL_MSG_STC[${_I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $1'})
_MAIL_MSG_TIME[${_I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $2'})
_MAIL_MSG_TEXT[${_I}]=$(print "${HC_MSG_ENTRY}" | awk -F "%%" '{ print $3'})
_I=$(( _I + 1 ))
done <${HC_MSG_FILE} 2>/dev/null
_MAX_I=${#_MAIL_MSG_STC[*]}
_I=0
while (( _I < _MAX_I ))
do
(( _MAIL_MSG_STC[${_I}] > 0 )) && _HC_BODY=$(printf "%s\n%s\n" "${_HC_BODY}" "${_MAIL_MSG_TEXT[${_I}]}")
_I=$(( _I + 1 ))
done
# check for custom template
[[ -r "${_MAIL_BODY_TPL}-${_MAIL_HC}" ]] && _MAIL_BODY_TPL="${_MAIL_BODY_TPL}-${_MAIL_HC}"
[[ -r "${_MAIL_BODY_TPL}" ]] || die "cannot read mail body template at ${_MAIL_BODY_TPL}"
eval "cat << __EOT
$(sed 's/[\$`]/\\&/g;s/<## @\([^ ]*\) ##>/${\1}/g' <${_MAIL_BODY_TPL})
__EOT" >>${_TMP1_MAIL_FILE}
# HC STDOUT log? (drop the .$$ bit)
_MAIL_STDOUT_LOG="${EVENTS_DIR}/${DIR_PREFIX}/${_MAIL_FAIL_ID}/${_HC_STDOUT_LOG_SHORT%.*}"
if [[ -s "${_MAIL_STDOUT_LOG}" ]]
then
_MAIL_STDOUT_MSG="${_MAIL_STDOUT_LOG}"
_MAIL_ATTACH_BIT="-a ${_MAIL_STDOUT_LOG}"
else
_MAIL_STDOUT_MSG="no log file available"
fi
# HC STDERR? (drop the .$$ bit)
_MAIL_STDERR_LOG="${EVENTS_DIR}/${DIR_PREFIX}/${_MAIL_FAIL_ID}/${_HC_STDERR_LOG_SHORT%.*}"
if [[ -s "${_MAIL_STDERR_LOG}" ]]
then
_MAIL_STDERR_MSG="${_MAIL_STDERR_LOG}"
_MAIL_ATTACH_BIT="${_MAIL_ATTACH_BIT} -a ${_MAIL_STDERR_LOG}"
else
_MAIL_STDERR_MSG="no log file available"
fi
# create footer part
[[ -r ${_MAIL_FOOTER_TPL} ]] || die "cannot read mail body template at ${_MAIL_FOOTER_TPL}"
eval "cat << __EOT
$(sed 's/[\$`]/\\&/g;s/<## @\([^ ]*\) ##>/${\1}/g' <${_MAIL_FOOTER_TPL})
__EOT" >>${_TMP1_MAIL_FILE}
# combine and send message components
case "${_MAIL_METHOD}" in
"mailx")
# remove non-ASCII characters to avoid Exchange ATT00001.bin
cat ${_TMP1_MAIL_FILE} | tr -cd '[:print:]\n' 2>/dev/null |\
${_MAILX_BIN} ${_MAIL_ATTACH_BIT} -s "${_SUBJ_MSG}" "${ARG_MAIL_TO}"
_MAIL_RC=$?
;;
"mutt")
# attach bit goes at the end
cat ${_TMP1_MAIL_FILE} 2>/dev/null |\
${_MUTT_BIN} -s "${_SUBJ_MSG}" "${ARG_MAIL_TO}" ${_MAIL_ATTACH_BIT}
_MAIL_RC=$?
;;
"sendmail")
[[ -s "${_MAIL_STDOUT_LOG}" ]] && \
uuencode ${_MAIL_STDOUT_LOG} stdout.log >>${_TMP2_MAIL_FILE} 2>/dev/null
[[ -s "${_MAIL_STDERR_LOG}" ]] && \
uuencode ${_MAIL_STDERR_LOG} stderr.log >>${_TMP2_MAIL_FILE} 2>/dev/null
cat ${_TMP1_MAIL_FILE} ${_TMP2_MAIL_FILE} 2>/dev/null | ${_SENDMAIL_BIN} -t
_MAIL_RC=$?
;;
*)
:
;;
esac
if (( _MAIL_RC == 0 ))
then
log "e-mail alert sent/queued to ${ARG_MAIL_TO}: ${_MAIL_HC} failed, FAIL_ID=${_MAIL_FAIL_ID}"
else
log "failed to send e-mail to ${ARG_MAIL_TO}: ${_MAIL_HC} failed, FAIL_ID=${_MAIL_FAIL_ID}"
fi
# clean up temporary files
[[ -f ${_TMP1_MAIL_FILE} ]] && rm -f ${_TMP1_MAIL_FILE} >/dev/null 2>&1
[[ -f ${_TMP2_MAIL_FILE} ]] && rm -f ${_TMP2_MAIL_FILE} >/dev/null 2>&1
return 0
}
#******************************************************************************
# END of script
#******************************************************************************
+129
View File
@@ -0,0 +1,129 @@
#!/usr/bin/env ksh
#******************************************************************************
# @(#) notify_sms.sh
#******************************************************************************
# @(#) Copyright (C) 2017 by KUDOS BVBA (info@kudos.be). All rights reserved.
#
# This program is a free software; you can redistribute it and/or modify
# it under the same terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
#******************************************************************************
#
# DOCUMENTATION (MAIN)
# -----------------------------------------------------------------------------
# @(#) MAIN: notify_sms
# DOES: send sms alert
# EXPECTS: 1=HC name [string], 2=HC FAIL_ID [string]
# RETURNS: 0
# REQUIRES: init_hc(), log(), warn()
#
# -----------------------------------------------------------------------------
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
#******************************************************************************
# -----------------------------------------------------------------------------
function notify_sms
{
# ------------------------- CONFIGURATION starts here -------------------------
typeset _CONFIG_FILE="${CONFIG_DIR}/core/providers/$0.conf"
typeset _VERSION="2017-04-27" # YYYY-MM-DD
typeset _SUPPORTED_PLATFORMS="AIX,HP-UX,Linux" # uname -s match
# ------------------------- CONFIGURATION ends here ---------------------------
# set defaults
(( ARG_DEBUG != 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
init_hc "$0" "${_SUPPORTED_PLATFORMS}" "${_VERSION}"
typeset _SMS_HC="$1"
typeset _SMS_FAIL_ID="$2"
typeset _SMS_TEXT=""
typeset _FROM_MSG="${EXEC_USER}@${HOST_NAME}"
typeset _CURL_BIN=""
typeset _SMS_PROVIDERS=""
typeset _SMS_KAPOW_SEND_URL=""
typeset _SMS_KAPOW_USER=""
typeset _SMS_KAPOW_PASSWORD=""
# handle config file
if [[ ! -r ${_CONFIG_FILE} ]]
then
warn "unable to read configuration file at ${_CONFIG_FILE}"
return 1
fi
# read required config values
_SMS_PROVIDERS="$(grep -i '^SMS_PROVIDERS=' ${_CONFIG_FILE} | cut -f2 -d'=' | tr -d '\"')"
if [[ -z "${_SMS_PROVIDERS}" ]]
then
warn "no value set for 'SMS_PROVIDERS' in ${_CONFIG_FILE}"
return 1
fi
# SMS_PROVIDERS & SMS settings
if [[ -n "${_SMS_PROVIDERS}" ]]
then
# SMS specific values are sourced in read_config()
print "${_SMS_PROVIDERS}" | tr ',' '\n' | while read -r _PROVIDER_OPTS
do
case "${_PROVIDER_OPTS}" in
*kapow*|*KAPOW*|*Kapow*)
# read required config values
_SMS_KAPOW_SEND_URL="$(grep -i '^SMS_KAPOW_SEND_URL=' ${_CONFIG_FILE} | cut -f2 -d'=' | tr -d '\"')"
if [[ -z "${_SMS_KAPOW_SEND_URL}" ]]
then
warn "no value set for 'SMS_KAPOW_SEND_URL' in ${_CONFIG_FILE}"
return 1
fi
_SMS_KAPOW_USER="$(grep -i '^SMS_KAPOW_USER=' ${_CONFIG_FILE} | cut -f2 -d'=' | tr -d '\"')"
if [[ -z "${_SMS_KAPOW_USER}" ]]
then
warn "no value set for 'SMS_KAPOW_USER' in ${_CONFIG_FILE}"
return 1
fi
_SMS_KAPOW_PASS="$(grep -i '^SMS_KAPOW_PASS=' ${_CONFIG_FILE} | cut -f2 -d'=' | tr -d '\"')"
if [[ -z "${_SMS_KAPOW_PASS}" ]]
then
warn "no value set for 'SMS_KAPOW_PASS' in ${_CONFIG_FILE}"
return 1
fi
esac
done
fi
# send SMS
case "${ARG_SMS_PROVIDER}" in
*kapow*|*KAPOW*|*Kapow*)
# KAPOW (https://www.kapow.co.uk/)
# find 'curl'
_CURL_BIN="$(which curl 2>/dev/null)"
if [[ -x ${_CURL_BIN} ]] && [[ -n "${_CURL_BIN}" ]]
then
_SMS_TEXT=$(print "${_FROM_MSG}: HC ${_SMS_HC} failed, FAIL_ID=${_SMS_FAIL_ID}" | data_encode_url)
if (( ARG_DEBUG == 0 ))
then
${_CURL_BIN} -s --url "${_SMS_KAPOW_SEND_URL}?username=${_SMS_KAPOW_USER}&password=${_SMS_KAPOW_PASS}&mobile=${ARG_SMS_TO}&sms=${_SMS_TEXT}" >/dev/null 2>&1
else
${_CURL_BIN} --url "${_SMS_KAPOW_SEND_URL}?username=${_SMS_KAPOW_USER}&password=${_SMS_KAPOW_PASS}&mobile=${ARG_SMS_TO}&sms=${_SMS_TEXT}"
fi
else
die "unable to send SMS - curl is not installed here"
fi
;;
*)
# nothing here
die "unable to send SMS - no method defined for SMS provider ${ARG_SMS_PROVIDER}"
;;
esac
log "SMS alert sent/queued to ${ARG_SMS_TO}: ${_SMS_HC} failed, FAIL_ID=${_SMS_FAIL_ID}"
return 0
}
#******************************************************************************
# END of script
#******************************************************************************