Variables privées dans common + optim lecture conf NRPE
This commit is contained in:
parent
5b460bf65b
commit
b3751c3f70
|
@ -13,6 +13,7 @@ readonly debian_major_version
|
||||||
# If no time limit is provided in CLI or found in file, this value is used
|
# If no time limit is provided in CLI or found in file, this value is used
|
||||||
readonly default_disabled_time="1h"
|
readonly default_disabled_time="1h"
|
||||||
|
|
||||||
|
_nrpe_conf_lines='' # populated at the end of the file
|
||||||
|
|
||||||
|
|
||||||
function error() {
|
function error() {
|
||||||
|
@ -50,8 +51,8 @@ END
|
||||||
# Fail if argument does not respect format: XwXdXhXmXs, XhX, XmX
|
# Fail if argument does not respect format: XwXdXhXmXs, XhX, XmX
|
||||||
function filter_duration() {
|
function filter_duration() {
|
||||||
# $1: duration in format specified above
|
# $1: duration in format specified above
|
||||||
time_regex="^([0-9]+d)?(([0-9]+h(([0-9]+m?)|([0-9]+m([0-9]+s?)?))?)|(([0-9]+m([0-9]+s?)?)?))?$"
|
_time_regex="^([0-9]+d)?(([0-9]+h(([0-9]+m?)|([0-9]+m([0-9]+s?)?))?)|(([0-9]+m([0-9]+s?)?)?))?$"
|
||||||
if [[ "${1}" =~ ${time_regex} ]]; then
|
if [[ "${1}" =~ ${_time_regex} ]]; then
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
return 1
|
return 1
|
||||||
|
@ -74,31 +75,31 @@ function time_to_seconds() {
|
||||||
# Print re-enable time in secs
|
# Print re-enable time in secs
|
||||||
function get_enable_time() {
|
function get_enable_time() {
|
||||||
# $1: wrapper name
|
# $1: wrapper name
|
||||||
disable_file_path="$(get_disable_file_path "${1}")"
|
_disable_file_path="$(get_disable_file_path "${1}")"
|
||||||
if [ ! -e "${disable_file_path}" ]; then
|
if [ ! -e "${_disable_file_path}" ]; then
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
enable_secs="$(grep -v -E "^\s*#" "${disable_file_path}" | sed '/^$/d' | head -n1 | awk '/^[0-9]+$/ {print $1}')"
|
_enable_secs="$(grep -v -E "^\s*#" "${_disable_file_path}" | sed '/^$/d' | head -n1 | awk '/^[0-9]+$/ {print $1}')"
|
||||||
# If file is empty, use file last change date plus default disabled time
|
# If file is empty, use file last change date plus default disabled time
|
||||||
if [ -z "${enable_secs}" ]; then
|
if [ -z "${_enable_secs}" ]; then
|
||||||
file_last_change_secs="$(stat -c %Z "${disable_file_path}")"
|
_file_last_change_secs="$(stat -c %Z "${_disable_file_path}")"
|
||||||
default_disabled_time_secs="$(time_to_seconds "${default_disabled_time}")"
|
_default_disabled_time_secs="$(time_to_seconds "${default_disabled_time}")"
|
||||||
enable_secs="$(( file_last_change_secs + default_disabled_time_secs ))"
|
_enable_secs="$(( _file_last_change_secs + _default_disabled_time_secs ))"
|
||||||
fi
|
fi
|
||||||
echo "${enable_secs}"
|
echo "${_enable_secs}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Print disable message
|
# Print disable message
|
||||||
function get_disable_message() {
|
function get_disable_message() {
|
||||||
# $1: wrapper name
|
# $1: wrapper name
|
||||||
disable_file_path="$(get_disable_file_path "${1}")"
|
_disable_file_path="$(get_disable_file_path "${1}")"
|
||||||
if [ ! -e "${disable_file_path}" ]; then
|
if [ ! -e "${_disable_file_path}" ]; then
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
disable_msg="$(sed '/^$/d' "${disable_file_path}" | tail -n+2 | tr '\n' ' ' | awk '{$1=$1;print}')"
|
_disable_msg="$(sed '/^$/d' "${_disable_file_path}" | tail -n+2 | tr '\n' ' ' | awk '{$1=$1;print}')"
|
||||||
echo "${disable_msg}"
|
echo "${_disable_msg}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function now_secs() {
|
function now_secs() {
|
||||||
|
@ -112,40 +113,39 @@ function now_iso() {
|
||||||
# Print delay before re-enable in secs
|
# Print delay before re-enable in secs
|
||||||
function enable_delay() {
|
function enable_delay() {
|
||||||
# $1: re-enable time in secs
|
# $1: re-enable time in secs
|
||||||
#now_secs=$(date +"%s")
|
|
||||||
echo $(( ${1} - $(now_secs) ))
|
echo $(( ${1} - $(now_secs) ))
|
||||||
}
|
}
|
||||||
|
|
||||||
# Converts delay (in seconds) into human readable duration
|
# Converts delay (in seconds) into human readable duration
|
||||||
function delay_to_string() {
|
function delay_to_string() {
|
||||||
# $1: delay in secs
|
# $1: delay in secs
|
||||||
delay_days="$(( ${1} /86400 ))"
|
_delay_days="$(( ${1} /86400 ))"
|
||||||
if [ "${delay_days}" -eq 0 ]; then delay_days=""
|
if [ "${_delay_days}" -eq 0 ]; then _delay_days=""
|
||||||
else delay_days="${delay_days}d"; fi
|
else _delay_days="${_delay_days}d"; fi
|
||||||
|
|
||||||
delay_hours="$(( (${1} %86400) /3600 ))"
|
_delay_hours="$(( (${1} %86400) /3600 ))"
|
||||||
if [ "${delay_hours}" -eq 0 ]; then delay_hours=""
|
if [ "${_delay_hours}" -eq 0 ]; then _delay_hours=""
|
||||||
else delay_hours="${delay_hours}h"; fi
|
else _delay_hours="${_delay_hours}h"; fi
|
||||||
|
|
||||||
delay_minutes="$(( ((${1} %86400) %3600) /60 ))"
|
_delay_minutes="$(( ((${1} %86400) %3600) /60 ))"
|
||||||
if [ "${delay_minutes}" -eq 0 ]; then delay_minutes=""
|
if [ "${_delay_minutes}" -eq 0 ]; then _delay_minutes=""
|
||||||
else delay_minutes="${delay_minutes}m"; fi
|
else _delay_minutes="${_delay_minutes}m"; fi
|
||||||
|
|
||||||
delay_seconds="$(( ((${1} %86400) %3600) %60 ))"
|
_delay_seconds="$(( ((${1} %86400) %3600) %60 ))"
|
||||||
if [ "${delay_seconds}" -eq 0 ]; then delay_seconds=""
|
if [ "${_delay_seconds}" -eq 0 ]; then _delay_seconds=""
|
||||||
else delay_seconds="${delay_seconds}s"; fi
|
else _delay_seconds="${_delay_seconds}s"; fi
|
||||||
|
|
||||||
echo "${delay_days}${delay_hours}${delay_minutes}${delay_seconds}"
|
echo "${_delay_days}${_delay_hours}${_delay_minutes}${_delay_seconds}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_disabled() {
|
function is_disabled() {
|
||||||
# $1: check name
|
# $1: check name
|
||||||
wrapper="$(get_check_wrapper_name "${1}")"
|
_wrapper="$(get_check_wrapper_name "${1}")"
|
||||||
disable_file_path="$(get_disable_file_path "${wrapper}")"
|
_disable_file_path="$(get_disable_file_path "${_wrapper}")"
|
||||||
if [ -e "${disable_file_path}" ]; then
|
if [ -e "${_disable_file_path}" ]; then
|
||||||
enable_time="$(get_enable_time "${wrapper}")"
|
_enable_time="$(get_enable_time "${_wrapper}")"
|
||||||
enable_delay="$(enable_delay "${enable_time}")"
|
_enable_delay="$(enable_delay "${_enable_time}")"
|
||||||
if [ "${enable_delay}" -le "0" ]; then
|
if [ "${_enable_delay}" -le "0" ]; then
|
||||||
echo "False"
|
echo "False"
|
||||||
else
|
else
|
||||||
echo "True"
|
echo "True"
|
||||||
|
@ -168,7 +168,7 @@ function get_disable_file_path() {
|
||||||
# and in the same order than NRPE does (taking account that
|
# and in the same order than NRPE does (taking account that
|
||||||
# order changes from Deb10)
|
# order changes from Deb10)
|
||||||
function get_nrpe_conf() {
|
function get_nrpe_conf() {
|
||||||
_get_conf_from_file "${nrpe_conf_path}"
|
echo "${_nrpe_conf_lines}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Private function to recursively get NRPE conf from file
|
# Private function to recursively get NRPE conf from file
|
||||||
|
@ -176,18 +176,18 @@ function _get_conf_from_file() {
|
||||||
# $1: NRPE conf file (.cfg)
|
# $1: NRPE conf file (.cfg)
|
||||||
if [ ! -f "${1}" ]; then return; fi
|
if [ ! -f "${1}" ]; then return; fi
|
||||||
|
|
||||||
conf_lines=$(grep -E -R -v --no-filename "^\s*(#.*|)$" "${1}")
|
_conf_lines=$(grep -E -R -v --no-filename "^\s*(#.*|)$" "${1}")
|
||||||
while read -r line; do
|
while read -r _line; do
|
||||||
if [[ "${line}" =~ .*'include='.* ]]; then
|
if [[ "${_line}" =~ .*'include='.* ]]; then
|
||||||
conf_file=$(echo "${line}" | cut -d= -f2)
|
_conf_file=$(echo "${_line}" | cut -d= -f2)
|
||||||
_get_conf_from_file "${conf_file}"
|
_get_conf_from_file "${_conf_file}"
|
||||||
elif [[ "${line}" =~ .*'include_dir='.* ]]; then
|
elif [[ "${_line}" =~ .*'include_dir='.* ]]; then
|
||||||
conf_dir=$(echo "${line}" | cut -d= -f2)
|
_conf_dir=$(echo "${_line}" | cut -d= -f2)
|
||||||
_get_conf_from_dir "${conf_dir}"
|
_get_conf_from_dir "${_conf_dir}"
|
||||||
else
|
else
|
||||||
echo "${line}"
|
echo "${_line}"
|
||||||
fi
|
fi
|
||||||
done <<< "${conf_lines}"
|
done <<< "${_conf_lines}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Private function to recursively get NRPE conf from directory
|
# Private function to recursively get NRPE conf from directory
|
||||||
|
@ -197,40 +197,38 @@ function _get_conf_from_dir() {
|
||||||
|
|
||||||
if [ "${debian_major_version}" -ge 10 ]; then
|
if [ "${debian_major_version}" -ge 10 ]; then
|
||||||
# From Deb10, NRPE use scandir() with alphasort() function
|
# From Deb10, NRPE use scandir() with alphasort() function
|
||||||
sort_command="sort"
|
_sort_command="sort"
|
||||||
else
|
else
|
||||||
# Before Deb10, NRPE use loaddir(), like find utility
|
# Before Deb10, NRPE use loaddir(), like find utility
|
||||||
sort_command="cat -"
|
_sort_command="cat -"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Add conf files in dir to be processed recursively
|
# Add conf files in dir to be processed recursively
|
||||||
for file in $(find "${1}" -maxdepth 1 -name "*.cfg" | ${sort_command}); do
|
for _file in $(find "${1}" -maxdepth 1 -name "*.cfg" | ${_sort_command}); do
|
||||||
if [ -f "${file}" ]; then
|
if [ -f "${_file}" ]; then
|
||||||
_get_conf_from_file "${file}"
|
_get_conf_from_file "${_file}"
|
||||||
elif [ -d "${file}" ]; then
|
elif [ -d "${_file}" ]; then
|
||||||
_get_conf_from_dir "${file}"
|
_get_conf_from_dir "${_file}"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# Print the checks that are configured in NRPE
|
# Print the checks that are configured in NRPE
|
||||||
function get_checks_names() {
|
function get_checks_names() {
|
||||||
conf_lines="$(get_nrpe_conf "${nrpe_conf_path}")"
|
echo "${_nrpe_conf_lines}" | grep -E "command\[check_.*\]=" | awk -F"[\\\[\\\]=]" '{sub("check_", "", $2); print $2}' | sort | uniq
|
||||||
echo "${conf_lines}" | grep -E "command\[check_.*\]=" | awk -F"[\\\[\\\]=]" '{sub("check_", "", $2); print $2}' | sort | uniq
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Print the commands defined for check $1 in NRPE configuration
|
# Print the commands defined for check $1 in NRPE configuration
|
||||||
function get_check_commands() {
|
function get_check_commands() {
|
||||||
# $1: check name
|
# $1: check name
|
||||||
conf_lines="$(get_nrpe_conf "${nrpe_conf_path}")"
|
echo "${_nrpe_conf_lines}" | grep -E "command\[check_${1}\]" | cut -d'=' -f2-
|
||||||
echo "${conf_lines}" | grep -E "command\[check_${1}\]" | cut -d'=' -f2-
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Print the checks that have no alerts_wrapper in NRPE configuration
|
# Print the checks that have no alerts_wrapper in NRPE configuration
|
||||||
function not_wrapped_checks() {
|
function not_wrapped_checks() {
|
||||||
for check in $(get_checks_names); do
|
for _check in $(get_checks_names); do
|
||||||
if ! is_wrapped "${check}"; then
|
if ! is_wrapped "${_check}"; then
|
||||||
echo "${check}"
|
echo "${_check}"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
@ -238,8 +236,8 @@ function not_wrapped_checks() {
|
||||||
# Fail if check is not wrapped
|
# Fail if check is not wrapped
|
||||||
function is_wrapped() {
|
function is_wrapped() {
|
||||||
# $1: check name
|
# $1: check name
|
||||||
cmd=$(get_check_commands "${1}" | tail -n1)
|
_cmd=$(get_check_commands "${1}" | tail -n1)
|
||||||
if echo "${cmd}" | grep --quiet --no-messages alerts_wrapper; then
|
if echo "${_cmd}" | grep --quiet --no-messages alerts_wrapper; then
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
return 1
|
return 1
|
||||||
|
@ -247,23 +245,22 @@ function is_wrapped() {
|
||||||
|
|
||||||
# Print the names that are defined in the wrappers of the checks
|
# Print the names that are defined in the wrappers of the checks
|
||||||
function get_wrappers_names() {
|
function get_wrappers_names() {
|
||||||
conf_lines="$(get_nrpe_conf "${nrpe_conf_path}")"
|
echo "${_nrpe_conf_lines}" | grep -s "alerts_wrapper" | awk '{ for (i=1 ; i<=NF; i++) { if ($i ~ /^(-n|--name)$/) { print $(i+1); break } } }' | tr ',' '\n' | sort | uniq
|
||||||
echo "${conf_lines}" | grep -s "alerts_wrapper" | awk '{ for (i=1 ; i<=NF; i++) { if ($i ~ /^(-n|--name)$/) { print $(i+1); break } } }' | tr ',' '\n' | sort | uniq
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Print the wrapper name of the check
|
# Print the wrapper name of the check
|
||||||
function get_check_wrapper_name() {
|
function get_check_wrapper_name() {
|
||||||
# $1: check name
|
# $1: check name
|
||||||
cmd=$(get_check_commands "${1}" | tail -n1)
|
_cmd=$(get_check_commands "${1}" | tail -n1)
|
||||||
if echo "${cmd}" | grep --quiet --no-messages alerts_wrapper; then
|
if echo "${_cmd}" | grep --quiet --no-messages alerts_wrapper; then
|
||||||
echo "${cmd}" | awk '/--name/ {match($0, /--name\s*([a-zA-Z0-9_\-]*)\s*/, m); print m[1]}'
|
echo "${_cmd}" | awk '/--name/ {match($0, /--name\s*([a-zA-Z0-9_\-]*)\s*/, m); print m[1]}'
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_check() {
|
function is_check() {
|
||||||
# $1: check name
|
# $1: check name
|
||||||
checks="$(get_checks_names)"
|
_checks="$(get_checks_names)"
|
||||||
if echo "${checks}" | grep --quiet -E "^${1}$"; then
|
if echo "${_checks}" | grep --quiet -E "^${1}$"; then
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
return 1
|
return 1
|
||||||
|
@ -271,8 +268,8 @@ function is_check() {
|
||||||
|
|
||||||
function is_wrapper() {
|
function is_wrapper() {
|
||||||
# $1: wrapper name
|
# $1: wrapper name
|
||||||
wrappers="$(get_wrappers_names)"
|
_wrappers="$(get_wrappers_names)"
|
||||||
if echo "${wrappers}" | grep --quiet -E "^${1}$"; then
|
if echo "${_wrappers}" | grep --quiet -E "^${1}$"; then
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
return 1
|
return 1
|
||||||
|
@ -281,6 +278,9 @@ function is_wrapper() {
|
||||||
# Print the checks that name this wrapper
|
# Print the checks that name this wrapper
|
||||||
function get_wrapper_checks() {
|
function get_wrapper_checks() {
|
||||||
# $1: wrapper name
|
# $1: wrapper name
|
||||||
conf_lines="$(get_nrpe_conf "${nrpe_conf_path}")"
|
echo "${_nrpe_conf_lines}" | grep -E "command\[check_.*\]=" | grep -E "\-\-name\s*${1}" | awk -F"[\\\[\\\]=]" '{sub("check_", "", $2); print $2}' | sort | uniq | xargs
|
||||||
echo "${conf_lines}" | grep -E "command\[check_.*\]=" | grep -E "\-\-name\s*${1}" | awk -F"[\\\[\\\]=]" '{sub("check_", "", $2); print $2}' | sort | uniq | xargs
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Load NRPE configuration
|
||||||
|
_nrpe_conf_lines="$(_get_conf_from_file "${nrpe_conf_path}")"
|
||||||
|
|
Loading…
Reference in a new issue