Re-org of tree
This commit is contained in:
Executable
+119
@@ -0,0 +1,119 @@
|
||||
#!/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="2019-03-16" # YYYY-MM-DD
|
||||
typeset _SUPPORTED_PLATFORMS="AIX,HP-UX,Linux" # uname -s match
|
||||
typeset _DISPLAY_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"
|
||||
|
||||
typeset _DISPLAY_MSG_STC=""
|
||||
typeset _DISPLAY_MSG_TIME=""
|
||||
typeset _DISPLAY_MSG_TEXT=""
|
||||
typeset _DISPLAY_MSG_CUR_VAL=""
|
||||
typeset _DISPLAY_MSG_EXP_VAL=""
|
||||
typeset _ID_BIT=""
|
||||
|
||||
# parse $HC_MSG_VAR
|
||||
if [[ -n "${HC_MSG_VAR}" ]]
|
||||
then
|
||||
# shellcheck disable=SC1117
|
||||
printf "%s${_DISPLAY_SEP}%s${_DISPLAY_SEP}%s${_DISPLAY_SEP}%s${_DISPLAY_SEP}%s${_DISPLAY_SEP}%s\n" "Health Check" "STC" "Message" "FAIL ID" \
|
||||
"Current Value" "Expected Value"
|
||||
|
||||
# shellcheck disable=SC2034
|
||||
print "${HC_MSG_VAR}" | while IFS=${MSG_SEP} read -r _DISPLAY_MSG_STC _DISPLAY_MSG_TIME _DISPLAY_MSG_TEXT _DISPLAY_MSG_CUR_VAL _DISPLAY_MSG_EXP_VAL
|
||||
do
|
||||
# magically unquote if needed
|
||||
if [[ -n "${_DISPLAY_MSG_TEXT}" ]]
|
||||
then
|
||||
data_contains_string "${_DISPLAY_MSG_TEXT}" "${MAGIC_QUOTE}"
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
_DISPLAY_MSG_TEXT=$(data_magic_unquote "${_DISPLAY_MSG_TEXT}")
|
||||
fi
|
||||
fi
|
||||
if [[ -n "${_DISPLAY_MSG_CUR_VAL}" ]]
|
||||
then
|
||||
data_contains_string "${_DISPLAY_MSG_CUR_VAL}" "${MAGIC_QUOTE}"
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
_DISPLAY_MSG_CUR_VAL=$(data_magic_unquote "${_DISPLAY_MSG_CUR_VAL}")
|
||||
fi
|
||||
fi
|
||||
if [[ -n "${_DISPLAY_MSG_EXP_VAL}" ]]
|
||||
then
|
||||
data_contains_string "${_DISPLAY_MSG_EXP_VAL}" "${MAGIC_QUOTE}"
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
_DISPLAY_MSG_EXP_VAL=$(data_magic_unquote "${_DISPLAY_MSG_EXP_VAL}")
|
||||
fi
|
||||
fi
|
||||
if (( _DISPLAY_MSG_STC > 0 ))
|
||||
then
|
||||
_ID_BIT="${_DISPLAY_FAIL_ID}"
|
||||
else
|
||||
_ID_BIT=""
|
||||
fi
|
||||
# escape stuff
|
||||
_DISPLAY_MSG_TEXT=$(data_escape_csv "${_DISPLAY_MSG_TEXT}")
|
||||
_DISPLAY_MSG_CUR_VAL=$(data_escape_csv "${_DISPLAY_MSG_CUR_VAL}")
|
||||
_DISPLAY_MSG_EXP_VAL=$(data_escape_csv "${_DISPLAY_MSG_EXP_VAL}")
|
||||
|
||||
# shellcheck disable=SC1117
|
||||
printf "%s${_DISPLAY_SEP}%s${_DISPLAY_SEP}%s${_DISPLAY_SEP}%s${_DISPLAY_SEP}%s${_DISPLAY_SEP}%s\n" \
|
||||
"${_DISPLAY_HC}" \
|
||||
"${_DISPLAY_MSG_STC}" \
|
||||
"${_ID_BIT}" \
|
||||
"${_DISPLAY_MSG_TEXT}" \
|
||||
"${_DISPLAY_MSG_CUR_VAL}" \
|
||||
"${_DISPLAY_MSG_EXP_VAL}"
|
||||
done
|
||||
else
|
||||
ARG_LOG=0 ARG_VERBOSE=1 log "INFO: no HC results to display"
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
#******************************************************************************
|
||||
# END of script
|
||||
#******************************************************************************
|
||||
Executable
+153
@@ -0,0 +1,153 @@
|
||||
#!/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="2019-03-22" # 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) > 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 _BOLD=$(tput bold)
|
||||
typeset _NORMAL=$(tput sgr0)
|
||||
else
|
||||
typeset _RED=""
|
||||
typeset _GREEN=""
|
||||
typeset _YELLOW=""
|
||||
typeset _BLUE=""
|
||||
typeset _MAGENTA=""
|
||||
typeset _CYAN=""
|
||||
typeset _BOLD=""
|
||||
typeset _NORMAL=""
|
||||
fi
|
||||
|
||||
# parse $HC_MSG_VAR
|
||||
if [[ -n "${_DISPLAY_MSG_CODE}" ]]
|
||||
then
|
||||
case "${_DISPLAY_MSG_CODE}" in
|
||||
OK|ok)
|
||||
_DISPLAY_COLOR="${_GREEN}"
|
||||
;;
|
||||
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 [[ -n "${HC_MSG_VAR}" ]]
|
||||
then
|
||||
print "${HC_MSG_VAR}" | while read -r _HC_MSG_ENTRY
|
||||
do
|
||||
# determine _DISPLAY_MSG_STC (sum of all STCs)
|
||||
_DISPLAY_MSG_STC=$(print "${_HC_MSG_ENTRY}" | awk -F"${MSG_SEP}" 'BEGIN { stc = 0 } { for (i=1;i<=NF;i++) { stc = stc + $1 } } END { print stc }' 2>/dev/null)
|
||||
done
|
||||
|
||||
# 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)
|
||||
# shellcheck disable=SC1117
|
||||
printf "%-30s %50s\t[ %8s ]%s\n" \
|
||||
"${_DISPLAY_HC}" \
|
||||
"(${_DISPLAY_CFG})" \
|
||||
"${_DISPLAY_COLOR}${_DISPLAY_CODE}${_NORMAL}" \
|
||||
"${_DISPLAY_ID}"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
#******************************************************************************
|
||||
# END of script
|
||||
#**************************************************************************
|
||||
Executable
+114
@@ -0,0 +1,114 @@
|
||||
#!/usr/bin/env ksh
|
||||
#******************************************************************************
|
||||
# @(#) display_json.sh
|
||||
#******************************************************************************
|
||||
# @(#) Copyright (C) 2018 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_json
|
||||
# DOES: display HC results in JSON 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_json
|
||||
{
|
||||
# ------------------------- CONFIGURATION starts here -------------------------
|
||||
typeset _VERSION="2019-03-16" # 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_STC=""
|
||||
typeset _DISPLAY_MSG_TIME=""
|
||||
typeset _DISPLAY_MSG_TEXT=""
|
||||
typeset _DISPLAY_MSG_CUR_VAL=""
|
||||
typeset _DISPLAY_MSG_EXP_VAL=""
|
||||
typeset _ID_BIT=""
|
||||
|
||||
# parse $HC_MSG_VAR
|
||||
if [[ -n "${HC_MSG_VAR}" ]]
|
||||
then
|
||||
print "${HC_MSG_VAR}" | while IFS=${MSG_SEP} read -r _DISPLAY_MSG_STC _DISPLAY_MSG_TIME _DISPLAY_MSG_TEXT _DISPLAY_MSG_CUR_VAL _DISPLAY_MSG_EXP_VAL
|
||||
do
|
||||
# magically unquote if needed
|
||||
if [[ -n "${_DISPLAY_MSG_TEXT}" ]]
|
||||
then
|
||||
data_contains_string "${_DISPLAY_MSG_TEXT}" "${MAGIC_QUOTE}"
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
_DISPLAY_MSG_TEXT=$(data_magic_unquote "${_DISPLAY_MSG_TEXT}")
|
||||
fi
|
||||
fi
|
||||
if [[ -n "${_DISPLAY_MSG_CUR_VAL}" ]]
|
||||
then
|
||||
data_contains_string "${_DISPLAY_MSG_CUR_VAL}" "${MAGIC_QUOTE}"
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
_DISPLAY_MSG_CUR_VAL=$(data_magic_unquote "${_DISPLAY_MSG_CUR_VAL}")
|
||||
fi
|
||||
fi
|
||||
if [[ -n "${_DISPLAY_MSG_EXP_VAL}" ]]
|
||||
then
|
||||
data_contains_string "${_DISPLAY_MSG_EXP_VAL}" "${MAGIC_QUOTE}"
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
_DISPLAY_MSG_EXP_VAL=$(data_magic_unquote "${_DISPLAY_MSG_EXP_VAL}")
|
||||
fi
|
||||
fi
|
||||
if (( _DISPLAY_MSG_STC > 0 ))
|
||||
then
|
||||
_ID_BIT="${_DISPLAY_FAIL_ID}"
|
||||
else
|
||||
_ID_BIT=""
|
||||
fi
|
||||
# escape stuff
|
||||
_DISPLAY_MSG_TEXT=$(data_escape_json "${_DISPLAY_MSG_TEXT}")
|
||||
_DISPLAY_MSG_CUR_VAL=$(data_escape_json "${_DISPLAY_MSG_CUR_VAL}")
|
||||
_DISPLAY_MSG_EXP_VAL=$(data_escape_json "${_DISPLAY_MSG_EXP_VAL}")
|
||||
|
||||
|
||||
printf '{"hc":"%s","stc":"%s","time":"%s","fail_id":"%s","text":"%s","current_value":"%s","expected_value":"%s"}\n' \
|
||||
"${_DISPLAY_HC}" \
|
||||
"${_DISPLAY_MSG_STC}" \
|
||||
"${_DISPLAY_MSG_TIME}" \
|
||||
"${_ID_BIT}" \
|
||||
"${_DISPLAY_MSG_TEXT}" \
|
||||
"${_DISPLAY_MSG_CUR_VAL}" \
|
||||
"${_DISPLAY_MSG_EXP_VAL}"
|
||||
done
|
||||
else
|
||||
ARG_LOG=0 ARG_VERBOSE=1 log "INFO: no HC results to display"
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
#******************************************************************************
|
||||
# END of script
|
||||
#******************************************************************************
|
||||
Executable
+114
@@ -0,0 +1,114 @@
|
||||
#!/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="2019-03-16" # 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_STC=""
|
||||
typeset _DISPLAY_MSG_TIME=""
|
||||
typeset _DISPLAY_MSG_TEXT=""
|
||||
typeset _DISPLAY_MSG_CUR_VAL=""
|
||||
typeset _DISPLAY_MSG_EXP_VAL=""
|
||||
typeset _ID_BIT=""
|
||||
|
||||
# parse $HC_MSG_VAR
|
||||
if [[ -n "${HC_MSG_VAR}" ]]
|
||||
then
|
||||
# shellcheck disable=SC1117
|
||||
printf "%-30s\t%s\t%-16s\t%s\n" "HC" "STC" "FAIL ID" "Message"
|
||||
|
||||
# shellcheck disable=SC2034
|
||||
print "${HC_MSG_VAR}" | while IFS=${MSG_SEP} read -r _DISPLAY_MSG_STC _DISPLAY_MSG_TIME _DISPLAY_MSG_TEXT _DISPLAY_MSG_CUR_VAL _DISPLAY_MSG_EXP_VAL
|
||||
do
|
||||
# magically unquote if needed
|
||||
if [[ -n "${_DISPLAY_MSG_TEXT}" ]]
|
||||
then
|
||||
data_contains_string "${_DISPLAY_MSG_TEXT}" "${MAGIC_QUOTE}"
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
_DISPLAY_MSG_TEXT=$(data_magic_unquote "${_DISPLAY_MSG_TEXT}")
|
||||
fi
|
||||
fi
|
||||
if [[ -n "${_DISPLAY_MSG_CUR_VAL}" ]]
|
||||
then
|
||||
data_contains_string "${_DISPLAY_MSG_CUR_VAL}" "${MAGIC_QUOTE}"
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
_DISPLAY_MSG_CUR_VAL=$(data_magic_unquote "${_DISPLAY_MSG_CUR_VAL}")
|
||||
fi
|
||||
fi
|
||||
if [[ -n "${_DISPLAY_MSG_EXP_VAL}" ]]
|
||||
then
|
||||
data_contains_string "${_DISPLAY_MSG_EXP_VAL}" "${MAGIC_QUOTE}"
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
_DISPLAY_MSG_EXP_VAL=$(data_magic_unquote "${_DISPLAY_MSG_EXP_VAL}")
|
||||
fi
|
||||
fi
|
||||
if (( _DISPLAY_MSG_STC > 0 ))
|
||||
then
|
||||
_ID_BIT="${_DISPLAY_FAIL_ID}"
|
||||
else
|
||||
_ID_BIT=""
|
||||
fi
|
||||
# shellcheck disable=SC1117
|
||||
printf "%-30s\t%s\t%-16s\t%s\n" \
|
||||
"${_DISPLAY_HC}" \
|
||||
"${_DISPLAY_MSG_STC}" \
|
||||
"${_ID_BIT}" \
|
||||
"${_DISPLAY_MSG_TEXT}"
|
||||
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
|
||||
#**************************************************************************
|
||||
Executable
+114
@@ -0,0 +1,114 @@
|
||||
#!/usr/bin/env ksh
|
||||
#******************************************************************************
|
||||
# @(#) display_zenoss.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_zenoss
|
||||
# DOES: display HC results in a Zenoss like format. The format contains a status
|
||||
# field and 6 data fields:
|
||||
# OK/NOK|data1=<HC name> data2=<STC> data3=<FAIL ID> data4="<MSG>" data5=<current value> data6=<expected value>
|
||||
# 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_zenoss
|
||||
{
|
||||
# ------------------------- CONFIGURATION starts here -------------------------
|
||||
typeset _VERSION="2019-03-16" # 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_STC=""
|
||||
typeset _DISPLAY_MSG_TIME=""
|
||||
typeset _DISPLAY_MSG_TEXT=""
|
||||
typeset _DISPLAY_MSG_CUR_VAL=""
|
||||
typeset _DISPLAY_MSG_EXP_VAL=""
|
||||
|
||||
# parse $HC_MSG_VAR
|
||||
if [[ -n "${HC_MSG_VAR}" ]]
|
||||
then
|
||||
# shellcheck disable=SC2034
|
||||
print "${HC_MSG_VAR}" | while IFS=${MSG_SEP} read -r _DISPLAY_MSG_STC _DISPLAY_MSG_TIME _DISPLAY_MSG_TEXT _DISPLAY_MSG_CUR_VAL _DISPLAY_MSG_EXP_VAL
|
||||
do
|
||||
# magically unquote if needed
|
||||
if [[ -n "${_DISPLAY_MSG_TEXT}" ]]
|
||||
then
|
||||
data_contains_string "${_DISPLAY_MSG_TEXT}" "${MAGIC_QUOTE}"
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
_DISPLAY_MSG_TEXT=$(data_magic_unquote "${_DISPLAY_MSG_TEXT}")
|
||||
fi
|
||||
fi
|
||||
if [[ -n "${_DISPLAY_MSG_CUR_VAL}" ]]
|
||||
then
|
||||
data_contains_string "${_DISPLAY_MSG_CUR_VAL}" "${MAGIC_QUOTE}"
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
_DISPLAY_MSG_CUR_VAL=$(data_magic_unquote "${_DISPLAY_MSG_CUR_VAL}")
|
||||
fi
|
||||
fi
|
||||
if [[ -n "${_DISPLAY_MSG_EXP_VAL}" ]]
|
||||
then
|
||||
data_contains_string "${_DISPLAY_MSG_EXP_VAL}" "${MAGIC_QUOTE}"
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
_DISPLAY_MSG_EXP_VAL=$(data_magic_unquote "${_DISPLAY_MSG_EXP_VAL}")
|
||||
fi
|
||||
fi
|
||||
if (( _DISPLAY_MSG_STC > 0 ))
|
||||
then
|
||||
# shellcheck disable=SC1117
|
||||
printf "NOK|data1=%s data2=%s data3=%s data4=\"%s\" data5=%s data6=%s\n" \
|
||||
"${_DISPLAY_HC}" \
|
||||
"${_DISPLAY_MSG_STC}" \
|
||||
"${_DISPLAY_FAIL_ID}" \
|
||||
"${_DISPLAY_MSG_TEXT}" \
|
||||
"${_DISPLAY_MSG_CUR_VAL}" \
|
||||
"${_DISPLAY_MSG_EXP_VAL}"
|
||||
else
|
||||
# shellcheck disable=SC1117
|
||||
printf "OK|data1=%s data2=%s data3=%s data4=\"%s\" data5=%s data6=%s\n" \
|
||||
"${_DISPLAY_HC}" \
|
||||
"${_DISPLAY_MSG_STC}" \
|
||||
"0" \
|
||||
"${_DISPLAY_MSG_TEXT}" \
|
||||
"${_DISPLAY_MSG_CUR_VAL}" \
|
||||
"${_DISPLAY_MSG_EXP_VAL}"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
#******************************************************************************
|
||||
# END of script
|
||||
#******************************************************************************
|
||||
Executable
+2027
File diff suppressed because it is too large
Load Diff
Executable
+853
@@ -0,0 +1,853 @@
|
||||
#!/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: version_include_core()
|
||||
# DOES: dummy function for version placeholder
|
||||
# EXPECTS: n/a
|
||||
# RETURNS: 0
|
||||
function version_include_data
|
||||
{
|
||||
typeset _VERSION="2019-03-16" # YYYY-MM-DD
|
||||
|
||||
print "INFO: $0: ${_VERSION#version_*}"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) 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
|
||||
# do not escape inside quotes
|
||||
print -R "$(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 -R "${1%?}" 2>/dev/null
|
||||
|
||||
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_is_string()
|
||||
# DOES: checks if a string (haystack) matches a string (needle).
|
||||
# EXPECTS: $1=haystack [string]; $2=needle [string]
|
||||
# OUTPUTS: n/a
|
||||
# RETURNS: 0=not found; 1=found
|
||||
# REQUIRES: n/a
|
||||
function data_is_string
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
|
||||
typeset _HAYSTACK="${1}"
|
||||
typeset _NEEDLE="${2}"
|
||||
|
||||
[[ "${_STRING}" = "${_NEEDLE}" ]] && return 1
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_list_contains_string()
|
||||
# DOES: checks if a comma-separated list of strings (haystack) contains a substring (needle).
|
||||
# EXPECTS: $1=haystack [string]; $2=needle [string]
|
||||
# OUTPUTS: n/a
|
||||
# RETURNS: 0=not found; 1=found
|
||||
# REQUIRES: data_contains_string()
|
||||
function data_list_contains_string
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
|
||||
typeset _HAYSTACK="${1}"
|
||||
typeset _NEEDLE="${2}"
|
||||
typeset _STRING=""
|
||||
|
||||
print "${_HAYSTACK}" | tr ',' '\n' 2>/dev/null | while read -r _STRING
|
||||
do
|
||||
data_contains_string "${_STRING}" "${_NEEDLE}" || return 1
|
||||
done
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_list_is_string()
|
||||
# DOES: checks if a comma-separated list of strings (haystack) matches a string (needle).
|
||||
# EXPECTS: $1=haystack [string]; $2=needle [string]
|
||||
# OUTPUTS: n/a
|
||||
# RETURNS: 0=not found; 1=found
|
||||
# REQUIRES: n/a
|
||||
function data_list_is_string
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
|
||||
typeset _HAYSTACK="${1}"
|
||||
typeset _NEEDLE="${2}"
|
||||
|
||||
print "${_HAYSTACK}" | tr ',' '\n' 2>/dev/null | while read -r _STRING
|
||||
do
|
||||
[[ "${_STRING}" = "${_NEEDLE}" ]] && return 1
|
||||
done
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_magic_quote()
|
||||
# DOES: magically quotes a needle in a string (default needle is: %)
|
||||
# EXPECTS: to be magically quoted [string]; $2=needle [string]
|
||||
# OUTPUTS: magically quoted [string]
|
||||
# RETURNS: n/a
|
||||
# REQUIRES: n/a
|
||||
function data_magic_quote
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
typeset _SEP="${2:-%}"
|
||||
typeset _MAGIC="${MAGIC:-!_!}"
|
||||
|
||||
print -R "${1}" 2>/dev/null | sed "s/${_SEP}/${_MAGIC}/g" 2>/dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_magic_unquote()
|
||||
# DOES: magically unquotes a needle in a string (default needle is: %)
|
||||
# EXPECTS: to be magically unquoted [string]; $2=needle [string]
|
||||
# OUTPUTS: magically unquoted [string]
|
||||
# RETURNS: n/a
|
||||
# REQUIRES: n/a
|
||||
function data_magic_unquote
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
typeset _SEP="${2:-%}"
|
||||
typeset _MAGIC="${MAGIC:-!_!}"
|
||||
|
||||
print -R "${1}" 2>/dev/null | sed "s/${_MAGIC}/${_SEP}/g" 2>/dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_escape_csv()
|
||||
# DOES: escapes semi-colons
|
||||
# EXPECTS: to be escaped [string]
|
||||
# OUTPUTS: escaped [string]
|
||||
# RETURNS: n/a
|
||||
# REQUIRES: n/a
|
||||
function data_escape_csv
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
|
||||
print -R "${1}" 2>/dev/null | sed 's#\([;]\)#\\\1#g' 2>/dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_escape_json()
|
||||
# DOES: escapes double quotes and backslashes
|
||||
# EXPECTS: to be escaped [string]
|
||||
# OUTPUTS: escaped [string]
|
||||
# RETURNS: n/a
|
||||
# REQUIRES: n/a
|
||||
function data_escape_json
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
typeset _NEEDLE='[\"]'
|
||||
|
||||
print -R "${1}" 2>/dev/null | sed 's#\(["\]\)#\\\1#g' 2>/dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_decomma()
|
||||
# DOES: remove commas
|
||||
# EXPECTS: [string] with commas
|
||||
# OUTPUTS: [string] without commas
|
||||
# RETURNS: 0
|
||||
# REQUIRES: n/a
|
||||
function data_decomma
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
|
||||
print -R "${1}" 2>/dev/null | 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 -R "${1%*,}" 2>/dev/null
|
||||
|
||||
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 -R "${1#,*}" 2>/dev/null
|
||||
|
||||
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 -R "${1}" 2>/dev/null | 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 -R "${1}" 2>/dev/null | tr ',' ' ' 2>/dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_comma2pipe()
|
||||
# DOES: replace commas with a pipe
|
||||
# EXPECTS: [string] with commas
|
||||
# OUTPUTS: [string] with pipes
|
||||
# RETURNS: 0
|
||||
# REQUIRES: n/a
|
||||
function data_comma2pipe
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
|
||||
print -R "${1}" 2>/dev/null | 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 -R "${1}" 2>/dev/null | 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 -R "${1}" 2>/dev/null | tr '\n' ',' 2>/dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_newline2hash()
|
||||
# DOES: replace newlines with a hash
|
||||
# EXPECTS: [string] with newlines (UNIX)
|
||||
# OUTPUTS: [string] with hashes (UNIX)
|
||||
# RETURNS: 0
|
||||
# REQUIRES: n/a
|
||||
function data_newline2hash
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
|
||||
print -R "${1}" 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 -R "${1}" 2>/dev/null | tr -s ' ' 2>/dev/null | tr ' ' ',' 2>/dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_pipe2comma()
|
||||
# DOES: replace pipes with a comma
|
||||
# EXPECTS: [string] with pipes
|
||||
# OUTPUTS: [string] with commas
|
||||
# RETURNS: 0
|
||||
# REQUIRES: n/a
|
||||
function data_pipe2comma
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
|
||||
print -R "${1}" 2>/dev/null | 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 -R "${1}" 2>/dev/null | 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 -R "${1}" 2>/dev/null | tr -d '\n' 2>/dev/null | tr -d '\r' 2>/dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_strip_space()
|
||||
# DOES: remove spaces
|
||||
# EXPECTS: [string] with spaces (all whitespace)
|
||||
# OUTPUTS: [string] without spaces (all whitespace)
|
||||
# RETURNS: 0
|
||||
# REQUIRES: n/a
|
||||
function data_strip_space
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
|
||||
print -R "${1}" 2>/dev/null | tr -d '[:space:]' 2>/dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_strip_leading_space()
|
||||
# DOES: remove leading spaces
|
||||
# EXPECTS: [string] with leading spaces (all whitespace)
|
||||
# OUTPUTS: [string] without leading spaces (all whitespace)
|
||||
# RETURNS: 0
|
||||
# REQUIRES: n/a
|
||||
function data_strip_leading_space
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
|
||||
print -R "${1}" | sed 's/^[[:blank:]]*//' 2>/dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_strip_trailing_space()
|
||||
# DOES: remove trailing spaces
|
||||
# EXPECTS: [string] with trailing spaces (all whitespace)
|
||||
# OUTPUTS: [string] without trailing spaces (all whitespace)
|
||||
# RETURNS: 0
|
||||
# REQUIRES: n/a
|
||||
function data_strip_trailing_space
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
|
||||
print -R "${1}" | sed 's/[[:blank:]]*$//' 2>/dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_strip_outer_space()
|
||||
# DOES: remove leading + trailing spaces
|
||||
# EXPECTS: [string] with leading + trailing spaces (all whitespace)
|
||||
# OUTPUTS: [string] without leading + trailing spaces (all whitespace)
|
||||
# RETURNS: 0
|
||||
# REQUIRES: n/a
|
||||
function data_strip_outer_space
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
|
||||
print -R "${1}" | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//' 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 -R "${1}" 2>/dev/null | 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 -R "${1}" 2>/dev/null | tr '[:lower:]' '[:upper:]' 2>/dev/null
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_is_numeric()
|
||||
# DOES: check if input is numeric
|
||||
# EXPECTS: [string]
|
||||
# OUTPUTS: n/a
|
||||
# RETURNS: 0=numeric; <>0=not numeric
|
||||
# REQUIRES: n/a
|
||||
function data_is_numeric
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
|
||||
case "${1}" in
|
||||
+([0-9])*(.)*([0-9]))
|
||||
# numeric, OK
|
||||
;;
|
||||
*)
|
||||
# not numeric
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
|
||||
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}" 2>/dev/null
|
||||
|
||||
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}" 2>/dev/null
|
||||
|
||||
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}" 2>/dev/null
|
||||
_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}
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_date2epoch()
|
||||
# DOES: converts a given date into UNIX epoch seconds
|
||||
# EXPECTS: date formatted as individual parameters (in UTC time):
|
||||
# $1 : YYYY
|
||||
# $2 : MM
|
||||
# $3 : DD
|
||||
# $4 : HH
|
||||
# $5 : MM
|
||||
# $6 : SS
|
||||
# OUTPUTS: UNIX epoch seconds [number]
|
||||
# RETURNS: 0
|
||||
# REQUIRES: n/a
|
||||
# REFERENCE: https://groups.google.com/forum/#!topic/comp.unix.shell/aPoPzFWP2Og
|
||||
function data_date2epoch
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
typeset _YEAR="${1}"
|
||||
typeset _MONTH="${2}"
|
||||
typeset _DAY="${3}"
|
||||
typeset _HOUR="${4}"
|
||||
typeset _MINUTE="${5}"
|
||||
typeset _SECOND="${6}"
|
||||
typeset _DAYS_ACC
|
||||
typeset _YEAR_DAY
|
||||
typeset _EPOCH
|
||||
typeset _LEAP_YEARS
|
||||
set -A _DAYS_ACC 0 0 31 59 90 120 151 181 212 243 273 304 334 365
|
||||
|
||||
# calculate day of year (counting from 0)
|
||||
_YEAR_DAY=$(( (_DAY - 1) + _DAYS_ACC[_MONTH] ))
|
||||
|
||||
# calculate number of leap years
|
||||
_LEAP_YEARS=$(( (_YEAR - 1968) / 4 ))
|
||||
_LEAP_YEARS=$(( _LEAP_YEARS - _YEAR / 100 + _YEAR / 400 + 15 ))
|
||||
|
||||
# adjust if we are still in Jan/Feb of leap year
|
||||
[[ $((_YEAR % 4)) = 0 && ${_MONTH} -lt 3 ]] && _LEAP_YEARS=$(( _LEAP_YEARS - 1 ))
|
||||
|
||||
# calculate the time since epoch
|
||||
_EPOCH=$(( ((_YEAR - 1970) * 365 + _YEAR_DAY + _LEAP_YEARS) * 86400
|
||||
+ _HOUR * 3600 + _MINUTE * 60 + _SECOND ))
|
||||
|
||||
print ${_EPOCH}
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: data_epoch2date()
|
||||
# DOES: converts an UNIX epoch to a human readable format (trying GNU date and
|
||||
# and perl). If neither works, then return the UNIX epoch.
|
||||
# EXPECTS: UNIX epoch [string]
|
||||
# OUTPUTS: date in human readable format OR UNIX epoch [string]
|
||||
# RETURNS: 0=conversion OK; 1=conversion failed
|
||||
# REQUIRES: n/a
|
||||
function data_epoch2date
|
||||
{
|
||||
typeset _UNIX_EPOCH="${1}"
|
||||
typeset _CONVERT_DATE=""
|
||||
|
||||
# try the GNU version of 'date -d'
|
||||
_CONVERT_DATE=$(date -d @"${_UNIX_EPOCH}" 2>/dev/null)
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
# try the perl way
|
||||
_CONVERT_DATE=$(perl -e "print scalar(localtime(${_UNIX_EPOCH}))" 2>/dev/null)
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
# no luck, we just return the UNIX epoch again
|
||||
print "${_UNIX_EPOCH}"
|
||||
return 1
|
||||
else
|
||||
print "${_CONVERT_DATE}"
|
||||
fi
|
||||
else
|
||||
print "${_CONVERT_DATE}"
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
#******************************************************************************
|
||||
# END of script
|
||||
#******************************************************************************
|
||||
Executable
+278
@@ -0,0 +1,278 @@
|
||||
#!/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: version_include_core()
|
||||
# DOES: dummy function for version placeholder
|
||||
# EXPECTS: n/a
|
||||
# RETURNS: 0
|
||||
function version_include_os
|
||||
{
|
||||
typeset _VERSION="2019-03-16" # YYYY-MM-DD
|
||||
|
||||
print "INFO: $0: ${_VERSION#version_*}"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) 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
|
||||
# shellcheck disable=SC1091
|
||||
. /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'=' 2>/dev/null | tr -d ' ' 2>/dev/null)
|
||||
[[ -n "${LINUX_RELEASE}" ]] || LINUX_RELEASE=$(grep 'CPE_NAME' /etc/os-release 2>/dev/null | cut -f2 -d'=' 2>/dev/null | cut -f5 -d':' 2>/dev/null)
|
||||
elif [[ -f /etc/redhat-release ]]
|
||||
then
|
||||
LINUX_DISTRO="Redhat"
|
||||
if [[ -f /etc/system-release-cpe ]]
|
||||
then
|
||||
# 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_RELEASE=$(print "${LINUX_RELEASE##*release }")
|
||||
fi
|
||||
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 && -n "$(command -v systemctl 2>/dev/null)" ]]
|
||||
then
|
||||
LINUX_INIT="systemd"
|
||||
elif [[ -r /usr/share/upstart ]]
|
||||
then
|
||||
# shellcheck disable=SC2034
|
||||
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: Corosync
|
||||
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="$(command -v crm 2>/dev/null)"
|
||||
if [[ -x ${_CRM_BIN} && -n "${_CRM_BIN}" ]]
|
||||
then
|
||||
# check for active
|
||||
crm status >/dev/null 2>/dev/null
|
||||
# shellcheck disable=SC2181
|
||||
(( $? == 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: Docker
|
||||
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="$(command -v docker 2>/dev/null)"
|
||||
if [[ -x ${_DOCKER_BIN} && -n "${_DOCKER_BIN}" ]]
|
||||
then
|
||||
# check for active
|
||||
docker ps >/dev/null 2>/dev/null
|
||||
# shellcheck disable=SC2181
|
||||
(( $? == 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: NetworkManager
|
||||
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="$(command -v 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" 2>/dev/null)
|
||||
else
|
||||
(( ARG_DEBUG > 0 )) && debug "NetworkManager is not active here"
|
||||
return 1
|
||||
fi
|
||||
|
||||
print ${_HAS_NM}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: linux_has_systemd_service()
|
||||
# DOES: check if a systemd service is present (unit file)
|
||||
# EXPECTS: name of service [string]
|
||||
# OUTPUTS: 0=not installed; 1=installed
|
||||
# RETURNS: 0=success; 1=error
|
||||
# REQUIRES: systemd
|
||||
function linux_has_systemd_service
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set "${DEBUG_OPTS}"
|
||||
typeset _RC=0
|
||||
|
||||
systemctl list-unit-files 2>/dev/null | grep -c "^${1}" 2>/dev/null
|
||||
_RC=$?
|
||||
|
||||
return ${_RC}
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# @(#) FUNCTION: linux_exec_ssh()
|
||||
# DOES: execute a shell command remotely via SSH
|
||||
# EXPECTS: 1=options [string], 2=user [string], 3=host [string], 4=command [string]
|
||||
# RETURNS: exit code of remote command
|
||||
# OUTPUTS: STDOUT from SSH call
|
||||
# REQUIRES: ssh command-line utility
|
||||
function linux_exec_ssh
|
||||
{
|
||||
(( ARG_DEBUG > 0 && ARG_DEBUG_LEVEL > 0 )) && set ${DEBUG_OPTS}
|
||||
typeset _SSH_OPTS="${1}"
|
||||
typeset _SSH_USER="${2}"
|
||||
typeset _SSH_HOST="${3}"
|
||||
typeset _SSH_COMMAND="${4}"
|
||||
|
||||
if [[ -z "${_SSH_USER}" || -z "${_SSH_HOST}" || -z "${_SSH_COMMAND}" ]]
|
||||
then
|
||||
return 255
|
||||
fi
|
||||
# shellcheck disable=SC2086
|
||||
ssh ${_SSH_OPTS} -l ${_SSH_USER} ${_SSH_HOST} ${_SSH_COMMAND} 2>>${HC_STDERR_LOG} </dev/null
|
||||
|
||||
return $?
|
||||
}
|
||||
|
||||
#******************************************************************************
|
||||
# END of script
|
||||
#******************************************************************************
|
||||
Executable
+128
@@ -0,0 +1,128 @@
|
||||
#!/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: data_get_lvalue_from_config(), 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="2018-10-28" # 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=$(_CONFIG_FILE="${_CONFIG_FILE}" data_get_lvalue_from_config '_EIF_BIN')
|
||||
if [[ -z "${_EIF_BIN}" ]]
|
||||
then
|
||||
warn "no value set for 'EIF_BIN' in ${_CONFIG_FILE}"
|
||||
return 1
|
||||
fi
|
||||
_EIF_ETC=$(_CONFIG_FILE="${_CONFIG_FILE}" data_get_lvalue_from_config '_EIF_ETC')
|
||||
if [[ -z "${_EIF_ETC}" ]]
|
||||
then
|
||||
warn "no value set for 'EIF_ETC' in ${_CONFIG_FILE}"
|
||||
return 1
|
||||
fi
|
||||
_EIF_SEVERITY=$(_CONFIG_FILE="${_CONFIG_FILE}" data_get_lvalue_from_config '_EIF_SEVERITY')
|
||||
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 sub-shell 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
|
||||
#******************************************************************************
|
||||
Executable
+251
@@ -0,0 +1,251 @@
|
||||
#!/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="2019-03-16" # 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=""
|
||||
typeset _HC_STDOUT_LOG_SHORT=""
|
||||
typeset _HC_STDERR_LOG_SHORT=""
|
||||
typeset _MAIL_MSG_STC=""
|
||||
typeset _MAIL_MSG_TEXT=""
|
||||
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})"
|
||||
# shellcheck disable=SC2034
|
||||
typeset _FROM_MSG="${EXEC_USER}@${HOST_NAME}"
|
||||
|
||||
# set local trap for cleanup
|
||||
# shellcheck disable=SC2064
|
||||
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="$(command -v mutt 2>/dev/null)"
|
||||
if [[ -x ${_MUTT_BIN} ]] && [[ -n "${_MUTT_BIN}" ]]
|
||||
then
|
||||
_MAIL_METHOD="mutt"
|
||||
else
|
||||
# prefer mailx next
|
||||
_MAILX_BIN="$(command -v 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="$(command -v 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="$(command -v 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 (from $HC_MSG_VAR)
|
||||
print "${HC_MSG_VAR}" | while IFS=${MSG_SEP} read _MAIL_MSG_STC _ _MAIL_MSG_TEXT _MAIL_MSG_CUR_VAL _MAIL_MSG_EXP_VAL
|
||||
do
|
||||
# magically unquote if needed
|
||||
if [[ -n "${_MAIL_MSG_TEXT}" ]]
|
||||
then
|
||||
data_contains_string "${_MAIL_MSG_TEXT}" "${MAGIC_QUOTE}"
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
_MAIL_MSG_TEXT=$(data_magic_unquote "${_MAIL_MSG_TEXT}")
|
||||
fi
|
||||
fi
|
||||
if [[ -n "${_MAIL_MSG_CUR_VAL}" ]]
|
||||
then
|
||||
data_contains_string "${_MAIL_MSG_CUR_VAL}" "${MAGIC_QUOTE}"
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
_MAIL_MSG_CUR_VAL=$(data_magic_unquote "${_MAIL_MSG_CUR_VAL}")
|
||||
fi
|
||||
fi
|
||||
if [[ -n "${_MAIL_MSG_EXP_VAL}" ]]
|
||||
then
|
||||
data_contains_string "${_MAIL_MSG_EXP_VAL}" "${MAGIC_QUOTE}"
|
||||
# shellcheck disable=SC2181
|
||||
if (( $? > 0 ))
|
||||
then
|
||||
_MAIL_MSG_EXP_VAL=$(data_magic_unquote "${_MAIL_MSG_EXP_VAL}")
|
||||
fi
|
||||
fi
|
||||
if (( _MAIL_MSG_STC > 0 ))
|
||||
then
|
||||
# shellcheck disable=SC1117
|
||||
_HC_BODY=$(printf "%s\n%s\n" "${_HC_BODY}" "${_MAIL_MSG_TEXT}")
|
||||
fi
|
||||
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
|
||||
# shellcheck disable=SC2034
|
||||
_MAIL_STDOUT_MSG="${_MAIL_STDOUT_LOG}"
|
||||
_MAIL_ATTACH_BIT="-a ${_MAIL_STDOUT_LOG}"
|
||||
else
|
||||
# shellcheck disable=SC2034
|
||||
_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
|
||||
# shellcheck disable=SC2034
|
||||
_MAIL_STDERR_MSG="${_MAIL_STDERR_LOG}"
|
||||
_MAIL_ATTACH_BIT="${_MAIL_ATTACH_BIT} -a ${_MAIL_STDERR_LOG}"
|
||||
else
|
||||
# shellcheck disable=SC2034
|
||||
_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
|
||||
#******************************************************************************
|
||||
Executable
+131
@@ -0,0 +1,131 @@
|
||||
#!/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: data_get_lvalue_from_config(), 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="2018-10-28" # 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_PASS=""
|
||||
|
||||
# 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=$(_CONFIG_FILE="${_CONFIG_FILE}" data_get_lvalue_from_config 'SMS_PROVIDERS')
|
||||
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=$(_CONFIG_FILE="${_CONFIG_FILE}" data_get_lvalue_from_config 'SMS_KAPOW_SEND_URL')
|
||||
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=$(_CONFIG_FILE="${_CONFIG_FILE}" data_get_lvalue_from_config 'SMS_KAPOW_USER')
|
||||
if [[ -z "${_SMS_KAPOW_USER}" ]]
|
||||
then
|
||||
warn "no value set for 'SMS_KAPOW_USER' in ${_CONFIG_FILE}"
|
||||
return 1
|
||||
fi
|
||||
_SMS_KAPOW_PASS=$(_CONFIG_FILE="${_CONFIG_FILE}" data_get_lvalue_from_config 'SMS_KAPOW_PASS')
|
||||
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="$(command -v 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
|
||||
#******************************************************************************
|
||||
Executable
+348
@@ -0,0 +1,348 @@
|
||||
#!/usr/bin/env ksh
|
||||
#******************************************************************************
|
||||
# @(#) report_std.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: report_std
|
||||
# DOES: report HC events on STDOUT
|
||||
# EXPECTS: n/a
|
||||
# RETURNS: 0
|
||||
# REQUIRES: count_log_errors(), die(), init_hc(), list_hc(), $EVENTS_DIR, $HC_LOG
|
||||
#
|
||||
# -----------------------------------------------------------------------------
|
||||
# DO NOT CHANGE THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
|
||||
#******************************************************************************
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
function report_std
|
||||
{
|
||||
# ------------------------- CONFIGURATION starts here -------------------------
|
||||
typeset _VERSION="2019-03-16" # 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 _DIR_PREFIX=""
|
||||
typeset _ERROR_COUNT=0
|
||||
typeset _ERROR_TOTAL_COUNT=0
|
||||
typeset _HC_LAST=""
|
||||
typeset _HC_LAST_TIME=""
|
||||
typeset _HC_LAST_STC=0
|
||||
typeset _HC_LAST_FAIL_ID="-"
|
||||
typeset _ID_NEEDLE=""
|
||||
typeset _IS_VALID_DATE=""
|
||||
typeset _IS_VALID_ID=""
|
||||
typeset _CHECK_FILE=""
|
||||
typeset _LOG_FILE=""
|
||||
typeset _LOG_FILES=""
|
||||
typeset _LOG_STASH=""
|
||||
typeset _SORT_CMD=""
|
||||
typeset _LOG_MONTH=""
|
||||
typeset _LOG_YEAR=""
|
||||
typeset _OLDER_MONTH=""
|
||||
typeset _OLDER_YEAR=""
|
||||
typeset _NEWER_MONTH=""
|
||||
typeset _NEWER_YEAR=""
|
||||
|
||||
# set archive log stash
|
||||
if (( ARG_HISTORY > 0 )) || [[ -n "${ARG_OLDER}" ]] || [[ -n "${ARG_NEWER}" ]]
|
||||
then
|
||||
set +f # file globbing must be on
|
||||
_LOG_STASH="${ARCHIVE_DIR}/hc.*.log"
|
||||
fi
|
||||
# apply --newer or --older to log stash by intelligently selecting archive log files
|
||||
if [[ -n "${_LOG_STASH}" ]]
|
||||
then
|
||||
if [[ -n "${ARG_OLDER}" ]] || [[ -n "${ARG_NEWER}" ]]
|
||||
then
|
||||
(( ARG_DEBUG > 0 )) && debug "mangling archive log stash because we used --older/--newer"
|
||||
if [[ -n "${ARG_OLDER}" ]]
|
||||
then
|
||||
# check datestamp (should be YYYYMMDD)
|
||||
_IS_VALID_DATE=$(print "${ARG_OLDER}" | grep -c -E -e "^[0-9]{4}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])$" 2>/dev/null)
|
||||
(( _IS_VALID_DATE > 0 )) || die "invalid date for '--older' specified"
|
||||
# shellcheck disable=SC2003
|
||||
_OLDER_YEAR=$(expr substr "${ARG_OLDER}" 1 4 2>/dev/null)
|
||||
# shellcheck disable=SC2003
|
||||
_OLDER_MONTH=$(expr substr "${ARG_OLDER}" 5 2 2>/dev/null)
|
||||
(( ARG_DEBUG > 0 )) && debug "END date: ${_OLDER_YEAR}${_OLDER_MONTH}"
|
||||
# expand curent log stash (use for/do ~f*cking mskh)
|
||||
# shellcheck disable=SC2086
|
||||
_LOG_FILES=$(find ${_LOG_STASH} -type f 2>/dev/null | tr '\n' ' ' 2>/dev/null)
|
||||
_LOG_STASH=""
|
||||
for _LOG_FILE in ${_LOG_FILES}
|
||||
do
|
||||
# shellcheck disable=SC2003
|
||||
_LOG_YEAR=$(expr substr "$(basename ${_LOG_FILE} 2/dev/null)" 4 4 2>/dev/null)
|
||||
# shellcheck disable=SC2003
|
||||
_LOG_MONTH=$(expr substr "$(basename ${_LOG_FILE} 2/dev/null)" 9 2 2>/dev/null)
|
||||
(( ARG_DEBUG > 0 )) && debug "LOG date for ${_LOG_FILE}: ${_LOG_YEAR}${_LOG_MONTH}"
|
||||
# add log file to stash if file date <= older date; force arithemetic on strings
|
||||
if (( ${_LOG_YEAR}${_LOG_MONTH} <= ${_OLDER_YEAR}${_OLDER_MONTH} ))
|
||||
then
|
||||
(( ARG_DEBUG > 0 )) && debug "push ${_LOG_FILE} to archive log stash"
|
||||
_LOG_STASH="${_LOG_STASH} ${_LOG_FILE}"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
if [[ -n "${ARG_NEWER}" ]]
|
||||
then
|
||||
# check datestamp (should be YYYYMMDD)
|
||||
_IS_VALID_DATE=$(print "${ARG_NEWER}" | grep -c -E -e "^[0-9]{4}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])$" 2>/dev/null)
|
||||
(( _IS_VALID_DATE > 0 )) || die "invalid date for '--newer' specified"
|
||||
# shellcheck disable=SC2003
|
||||
_NEWER_YEAR=$(expr substr "${ARG_NEWER}" 1 4)
|
||||
# shellcheck disable=SC2003
|
||||
_NEWER_MONTH=$(expr substr "${ARG_NEWER}" 5 2)
|
||||
(( ARG_DEBUG > 0 )) && debug "START date: ${_NEWER_YEAR}${_NEWER_MONTH}"
|
||||
# expand curent log stash (use for/do ~f*cking mskh)
|
||||
# shellcheck disable=SC2086
|
||||
_LOG_FILES=$(find ${_LOG_STASH} -type f 2>/dev/null | tr '\n' ' ' 2>/dev/null)
|
||||
_LOG_STASH=""
|
||||
for _LOG_FILE in ${_LOG_FILES}
|
||||
do
|
||||
# shellcheck disable=SC2003
|
||||
_LOG_YEAR=$(expr substr "$(basename ${_LOG_FILE} 2/dev/null)" 4 4)
|
||||
# shellcheck disable=SC2003
|
||||
_LOG_MONTH=$(expr substr "$(basename ${_LOG_FILE} 2/dev/null)" 9 2)
|
||||
(( ARG_DEBUG > 0 )) && debug "LOG date for ${_LOG_FILE}: ${_LOG_YEAR}${_LOG_MONTH}"
|
||||
# add log file to stash if file date <= older date; force arithemetic on strings
|
||||
if (( ${_LOG_YEAR}${_LOG_MONTH} >= ${_NEWER_YEAR}${_NEWER_MONTH} ))
|
||||
then
|
||||
(( ARG_DEBUG > 0 )) && debug "push ${_LOG_FILE} to archive log stash"
|
||||
_LOG_STASH="${_LOG_STASH} ${_LOG_FILE}"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
# add current log file to log stash
|
||||
_LOG_STASH="${HC_LOG} ${_LOG_STASH}"
|
||||
|
||||
# --last report
|
||||
if (( ARG_LAST > 0 ))
|
||||
then
|
||||
# shellcheck disable=SC1117
|
||||
printf "\n| %-40s | %-20s | %-14s | %-4s\n" "HC" "Timestamp" "FAIL ID" "STC (combined value)"
|
||||
# shellcheck disable=SC2183,SC1117
|
||||
printf "%120s\n" | tr ' ' -
|
||||
# loop over all HCs
|
||||
list_hc "list" | while read -r _HC_LAST
|
||||
do
|
||||
_HC_LAST_TIME=""
|
||||
_HC_LAST_STC=0
|
||||
_HC_LAST_FAIL_ID="-"
|
||||
# find last event or block of events (same timestamp)
|
||||
# (but unfortunately this is only accurate to events within the SAME second!)
|
||||
# shellcheck disable=SC2086
|
||||
_HC_LAST_TIME="$(grep -h ${_HC_LAST} ${_LOG_STASH} 2>/dev/null | sort -n 2>/dev/null | cut -f1 -d${LOG_SEP} 2>/dev/null | uniq 2>/dev/null | tail -1 2>/dev/null)"
|
||||
if [[ -z "${_HC_LAST_TIME}" ]]
|
||||
then
|
||||
_HC_LAST_TIME="-"
|
||||
_HC_LAST_STC="-"
|
||||
else
|
||||
# use of cat is not useless here, makes sure END {} gets executed even
|
||||
# if $_LOG STASH contains non-existing files (because of * wildcard)
|
||||
# shellcheck disable=SC2002,SC2086
|
||||
cat ${_LOG_STASH} 2>/dev/null | awk -F "${LOG_SEP}" -v needle_time="${_HC_LAST_TIME}" -v needle_hc="${_HC_LAST}" \
|
||||
'
|
||||
BEGIN {
|
||||
last_stc = 0
|
||||
last_fail_id = "-"
|
||||
}
|
||||
{
|
||||
if (($1 ~ needle_time && $2 ~ needle_hc) && NF <= '"${NUM_LOG_FIELDS}"') {
|
||||
last_event_stc = $3
|
||||
last_stc = last_stc + last_event_stc
|
||||
last_event_fail_id = $5
|
||||
if (last_event_fail_id != "") { last_fail_id = last_event_fail_id }
|
||||
}
|
||||
}
|
||||
END {
|
||||
print last_fail_id, last_stc
|
||||
}
|
||||
' 2>/dev/null | read -r _HC_LAST_FAIL_ID _HC_LAST_STC
|
||||
fi
|
||||
# report on findings
|
||||
# shellcheck disable=SC1117
|
||||
printf "| %-40s | %-20s | %-14s | %-4s\n" \
|
||||
"${_HC_LAST}" "${_HC_LAST_TIME}" "${_HC_LAST_FAIL_ID}" "${_HC_LAST_STC}"
|
||||
done
|
||||
# disclaimer
|
||||
print "NOTE: this report only shows the overall combined status of all events of each HC within exactly"
|
||||
print " the *same* time stamp (seconds precise). It may therefore fail to report certain FAIL IDs."
|
||||
print " Use '--report' to get the exact list of failure events."
|
||||
# other reports
|
||||
else
|
||||
_ID_NEEDLE="[0-9][0-9]*"
|
||||
if [[ -n "${ARG_FAIL_ID}" ]]
|
||||
then
|
||||
# check FAIL_ID first (must be YYYYMMDDHHMMSS)
|
||||
_IS_VALID_ID=$(print "${ARG_FAIL_ID}" | grep -c -E -e "^[0-9]{4}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])([0-1][0-9]|2[0-3])([0-5][0-9])([0-5][0-9])$" 2>/dev/null)
|
||||
(( _IS_VALID_ID > 0 )) || die "invalid ID specified"
|
||||
_ID_NEEDLE="${ARG_FAIL_ID}"
|
||||
fi
|
||||
(( ARG_TODAY > 0 )) && _ID_NEEDLE="$(date '+%Y%m%d')" # refers to timestamp of HC FAIL_ID
|
||||
|
||||
# reverse?
|
||||
if (( ARG_REVERSE == 0 ))
|
||||
then
|
||||
_SORT_CMD="sort -n"
|
||||
else
|
||||
_SORT_CMD="sort -rn"
|
||||
fi
|
||||
# global or detailed?
|
||||
if (( ARG_DETAIL == 0 ))
|
||||
then
|
||||
# print failed events
|
||||
# not a useless use of cat here
|
||||
# (sort baulks if $_LOG STASH contains non-existing files (because of * wildcard))
|
||||
# shellcheck disable=SC2002,SC2086
|
||||
cat ${_LOG_STASH} 2>/dev/null | ${_SORT_CMD} 2>/dev/null | awk -F"${LOG_SEP}" \
|
||||
-v id_needle="${_ID_NEEDLE}" \
|
||||
-v older="${ARG_OLDER}" \
|
||||
-v newer="${ARG_NEWER}" \
|
||||
'
|
||||
BEGIN {
|
||||
event_count = 0
|
||||
if (older != "") { use_filter = 1; use_older = 1 }
|
||||
if (newer != "") { use_filter = 1; use_newer = 1 }
|
||||
}
|
||||
|
||||
{
|
||||
# apply --older/--newer filter?
|
||||
if (use_filter > 0) {
|
||||
# find log entries that are older than --older=<YYYYMMDD>
|
||||
if (use_older > 0) {
|
||||
log_date = substr($5, 1, 8);
|
||||
if (log_date < older && $5 ~ id_needle && NF <= '"${NUM_LOG_FIELDS}"') {
|
||||
events[event_count]=$0;
|
||||
event_count++;
|
||||
}
|
||||
}
|
||||
# find log entries that are newer than --older=<YYYYMMDD>
|
||||
if (use_newer > 0) {
|
||||
log_date = substr($5, 1, 8);
|
||||
if (log_date > newer && $5 ~ id_needle && NF <= '"${NUM_LOG_FIELDS}"') {
|
||||
events[event_count]=$0;
|
||||
event_count++;
|
||||
}
|
||||
}
|
||||
# no --older/--newer filter
|
||||
} else {
|
||||
if ($5 ~ id_needle && NF <= '"${NUM_LOG_FIELDS}"') {
|
||||
events[event_count]=$0;
|
||||
event_count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
END {
|
||||
if (event_count > 0) {
|
||||
printf ("\n| %-20s | %-14s | %-40s | %-s\n", "Timestamp", "FAIL ID", "HC", "Message");
|
||||
for (i=0; i<120; i++) { printf ("-"); }
|
||||
# loop over array (sorted)
|
||||
for (i=0; i<event_count; i++) {
|
||||
split (events[i], event, "|");
|
||||
printf ("\n| %-20s | %-14s | %-40s | %-s", event[1], event[5], event[2], event[4]);
|
||||
}
|
||||
printf ("\n\nSUMMARY: %s failed HC event(s) found.\n", event_count);
|
||||
} else {
|
||||
printf ("\nSUMMARY: 0 failed HC events found.\n");
|
||||
}
|
||||
}
|
||||
' 2>/dev/null
|
||||
else
|
||||
# print failed events (we may have multiple events for 1 FAIL ID)
|
||||
# not a useless use of cat here
|
||||
# (sort baulks if $_LOG STASH contains non-existing files (because of * wildcard))
|
||||
# shellcheck disable=SC2002,SC2086
|
||||
cat ${_LOG_STASH} 2>/dev/null | ${_SORT_CMD} 2>/dev/null | awk -F"${LOG_SEP}" -v id_needle="${_ID_NEEDLE}" \
|
||||
' BEGIN {
|
||||
event_count = 1
|
||||
dashes = sprintf("%36s",""); gsub (/ /, "-", dashes);
|
||||
}
|
||||
{
|
||||
if ($5 ~ id_needle && NF <= '"${NUM_LOG_FIELDS}"') {
|
||||
printf ("%36sMSG #%03d%36s", dashes, event_count, dashes)
|
||||
printf ("\nTime : %-s\nHC : %-s\nDetail : %-s\n", $1, $2, $4)
|
||||
event_count++
|
||||
}
|
||||
}
|
||||
' 2>/dev/null
|
||||
|
||||
# shellcheck disable=SC2003,SC2086
|
||||
_DIR_PREFIX="$(expr substr ${ARG_FAIL_ID} 1 4 2>/dev/null)-$(expr substr ${ARG_FAIL_ID} 5 2 2>/dev/null)"
|
||||
# shellcheck disable=SC2183,SC1117
|
||||
printf "%37sSTDOUT%37s\n" | tr ' ' -;
|
||||
# display non-empty STDOUT file(s)
|
||||
# shellcheck disable=SC2086
|
||||
if [[ -n "$(du -a ${EVENTS_DIR}/${_DIR_PREFIX}/${ARG_FAIL_ID}/*.stdout.log 2>/dev/null | awk '$1*512 > 0 {print $2}')" ]]
|
||||
then
|
||||
cat ${EVENTS_DIR}/${_DIR_PREFIX}/${ARG_FAIL_ID}/*.stdout.log
|
||||
else
|
||||
# shellcheck disable=SC1117
|
||||
printf "%-s\n" "No STDOUT found"
|
||||
fi
|
||||
|
||||
# shellcheck disable=SC2183,SC1117
|
||||
printf "%37sSTDERR%37s\n" | tr ' ' -;
|
||||
# display non-empty STDERR file(s)
|
||||
# shellcheck disable=SC2086
|
||||
if [[ -n "$(du -a ${EVENTS_DIR}/${_DIR_PREFIX}/${ARG_FAIL_ID}/*.stderr.log 2>/dev/null | awk '$1*512 > 0 {print $2}')" ]]
|
||||
then
|
||||
cat ${EVENTS_DIR}/${_DIR_PREFIX}/${ARG_FAIL_ID}/*.stderr.log
|
||||
else
|
||||
# shellcheck disable=SC1117
|
||||
printf "%-s\n" "No STDERR found"
|
||||
fi
|
||||
|
||||
# shellcheck disable=SC2183,SC1117
|
||||
printf "%80s\n" | tr ' ' -
|
||||
fi
|
||||
fi
|
||||
|
||||
# general note: history or not?
|
||||
if (( ARG_HISTORY > 0 ))
|
||||
then
|
||||
print "NOTE: showing results with all history (archive) included (--with-history)"
|
||||
else
|
||||
print "NOTE: showing results only of current log entries (use --with-history to view all entries)"
|
||||
fi
|
||||
|
||||
# check consistency of log(s)
|
||||
# shellcheck disable=SC2086
|
||||
find ${_LOG_STASH} -type f -print 2>/dev/null | while read -r _CHECK_FILE
|
||||
do
|
||||
_ERROR_COUNT=$(count_log_errors ${_CHECK_FILE})
|
||||
if (( _ERROR_COUNT > 0 ))
|
||||
then
|
||||
print "NOTE: found ${_ERROR_COUNT} rogue entr(y|ies) in log file ${_CHECK_FILE}"
|
||||
_ERROR_TOTAL_COUNT=$(( _ERROR_TOTAL_COUNT + _ERROR_COUNT ))
|
||||
fi
|
||||
_ERROR_COUNT=0
|
||||
done
|
||||
(( _ERROR_TOTAL_COUNT > 0 )) && print "NOTE: fix log errors with ${SCRIPT_NAME} --fix-logs [--with-history]"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
#******************************************************************************
|
||||
# END of script
|
||||
#******************************************************************************
|
||||
Reference in New Issue
Block a user