Version sant trop de bugs :) Gère si les noms des wrappers sont différents des checks, multiples, ou autres cas bizarres
All checks were successful
Ansible Lint |Total|New|Outstanding|Fixed|Trend |:-:|:-:|:-:|:-:|:-: |2696|0|2696|0|:zzz:
gitea/ansible-roles/pipeline/head This commit looks good

This commit is contained in:
William Hirigoyen 2024-03-29 10:45:01 +01:00
parent 2008c8caf4
commit f268b4411f
3 changed files with 131 additions and 44 deletions

View file

@ -65,7 +65,7 @@ function main() {
if [ "${action}" == 'enable' ]; then
if [ "${wrapper_name}" == "all" ]; then
for wrapper in $(get_wrappers_names); do
enable_alerts "${wrapper}"
enable_alerts "${wrapper}" "${message}"
done
else
enable_alerts "${wrapper_name}" "${message}"
@ -74,7 +74,7 @@ function main() {
duration_sec=$(time_to_seconds "${duration}")
if [ "${wrapper_name}" == "all" ]; then
for wrapper in $(get_wrappers_names); do
disable_alerts "${wrapper}" "${duration_sec}"
disable_alerts "${wrapper}" "${duration_sec}" "${message}"
done
else
disable_alerts "${wrapper_name}" "${duration_sec}" "${message}"

View file

@ -184,11 +184,16 @@ function check() {
fi
}
function disable_alerts() {
# $1: check name, $2: disable message
# Print error message and exit if not installed
function alerts_switch_is_installed() {
if ! command -v alerts_switch &> /dev/null; then
error "Error: script 'alerts_switch' is not installed. Aborted."
fi
}
function disable_alerts() {
# $1: check name, $2: disable message
alerts_switch_is_installed
if [ "${1}" = "all" ]; then
checks="$(get_checks_names)"
@ -196,6 +201,9 @@ function disable_alerts() {
checks="${1}"
fi
warn_not_wrapped "${checks}"
warn_wrapper_names "${checks}"
if [ -z "${2}" ]; then
echo -n "> Please provide a disable message (for logging and check output): "
read -r message
@ -207,17 +215,6 @@ function disable_alerts() {
message="${2}"
fi
# Verify that checks to disable are wrapped
unwrappeds="$(not_wrapped_checks)"
unwrapped_checks="$(comm -12 <(echo "${checks}") <(echo "${unwrappeds}"))"
if [ -n "${unwrapped_checks}" ]; then
>&2 printf "${orange}Warning:${nocolor} some checks are not configured, they will not be disabled:"
for unwrapped in ${unwrapped_checks}; do
>&2 printf " %s" "${unwrapped}"
done
>&2 printf "\n"
fi
default_msg=""
if [ "${default_duration}" = "True" ]; then
default_msg=" (use --during to change default time)"
@ -242,6 +239,19 @@ Additional information:
EOF
if [ "${1}" != "all" ]; then
wrapper="$(get_check_wrapper_name "${1}")"
checks="$(get_wrapper_checks "${wrapper}")"
n_checks="$(echo "${checks}" | wc -w)"
if [ "${n_checks}" -gt 1 ]; then
other_checks="$(echo "${checks}" | xargs -n1 | grep -v "${1}" | xargs)"
>&2 echo -e "${orange}Warning:${nocolor} because they have the same configuration, disabling ${1} will also disable: ${other_checks}.\n"
log "Warning: check ${1} will also disable ${other_checks} (which have the same wrapper name)."
fi
else
wrapper="all"
fi
echo -n "> Confirm (y/N)? "
read -r answer
if [ "${answer}" != "Y" ] && [ "${answer}" != "y" ]; then
@ -250,22 +260,25 @@ EOF
log "Action disable ${1} requested for ${duration} by user $(logname || echo unknown)."
# Log a warning if a check has no wrapper
if [ -n "${unwrapped_checks}" ]; then
log "Warning: some checks have no alerts_wrapper, they will not be disabled:"
for unwrapped in ${unwrapped_checks}; do
log " ${unwrapped}"
done
alerts_switch disable "${wrapper}" --during "${duration}" --message "${message}"
if [ "${1}" != "all" ]; then
if [ "${n_checks}" -eq 1 ]; then
echo -e "${orange}Check ${1} alerts are now disabled for ${duration}.${nocolor}"
else
echo -e "${orange}Alerts are now disabled for ${duration} for checks: ${checks}.${nocolor}"
fi
else
echo -e "${orange}All alerts are now disabled for ${duration}.${nocolor}"
fi
alerts_switch disable "${1}" --during "${duration}" --message "${message}"
echo -e "${orange}Check ${1} alerts are now disabled for ${duration}${nocolor}"
}
function enable_alerts() {
# $1: wrapper name, $2: enable message
# $1: check name, $2: enable message
alerts_switch_is_installed
if [ "${1}" != "all" ]; then
# Verify that check is not already enabled
is_disabled="$(is_disabled "${1}")"
if [ "${is_disabled}" = "False" ]; then
echo "${1} is already enabled, see 'monitoringctl status'"
@ -285,9 +298,31 @@ function enable_alerts() {
fi
log "Action enable ${1} requested by user $(logname || echo unknown)."
alerts_switch enable "${1}" --message "${message}"
echo -e "${green}Check ${1} alerts are now enabled.${nocolor}"
if [ "${1}" != "all" ]; then
wrapper="$(get_check_wrapper_name "${1}")"
checks="$(get_wrapper_checks "${wrapper}")"
n_checks="$(echo "${checks}" | wc -w)"
if [ "${n_checks}" -gt 1 ]; then
other_checks="$(echo "${checks}" | xargs -n1 | grep -v "${1}" | xargs)"
>&2 echo -e "${orange}Warning:${nocolor} because they have the same configuration, enabling ${1} will also enable: ${other_checks}.\n"
log "Warning: check ${1} will also enable ${other_checks} (which have the same wrapper name)."
fi
else
wrapper="all"
fi
alerts_switch enable "${wrapper}" --message "${message}"
if [ "${1}" != "all" ]; then
if [ "${n_checks}" -eq 1 ]; then
echo -e "${green}Check ${1} alerts are now enabled.${nocolor}"
else
echo -e "${green}Alerts are now enabled for checks: ${checks}.${nocolor}"
fi
else
echo -e "${green}All alerts are now enabled.${nocolor}"
fi
}
# Show NRPE command(s) configured for a check
@ -307,16 +342,55 @@ function show_check_commands() {
echo " ${check_command}"
}
# Echo which alerts are enabled or disabled and time left
# Print a warning if some wrappers have the same name
# or if a name is different from the check.
function warn_wrapper_names() {
#$1: checks to verify
warned="False"
for check in ${1}; do
wrapper_name="$(get_check_wrapper_name "${check}")"
if [ -n "${wrapper_name}" ] && [ "${wrapper_name}" != "${check}" ]; then
>&2 echo -e "${orange}Warning:${nocolor} ${check} check has wrapper name ${wrapper_name}."
warned="True"
fi
done
if [ "${warned}" = "True" ]; then
>&2 echo -e "${orange}It is recommanded to name the wrappers the same as the checks.${nocolor}\n"
fi
}
# Print a warning if some checks are not wrapped
function warn_not_wrapped() {
#$1: checks to verify
unwrappeds="$(not_wrapped_checks)"
unwrapped_checks="$(comm -12 <(echo "${1}") <(echo "${unwrappeds}"))"
if [ -n "${unwrapped_checks}" ]; then
n_unwrapped="$(echo "${unwrapped_checks}" | wc -w)"
if [ "${n_unwrapped}" -eq 1 ]; then
>&2 echo -e "${orange}Warning:${nocolor} ${unwrapped_checks} check is not wrapped, it will not be disabled."
else
>&2 echo -e -n "${orange}Warning:${nocolor} some checks are not configured, they will not be disabled:"
for unwrapped in ${unwrapped_checks}; do
>&2 echo -e -n " %s" "${unwrapped}"
done
>&2 echo -e -n "\n"
fi
log "Warning: some checks have no alerts_wrapper, they will not be disabled: $(echo "${unwrapped_checks}" | xargs)"
fi
}
# Echo which checks are enabled or disabled and time left
function alerts_status() {
# $1: check name, "all" or empty
if [ -z "${1}" ] || [ "${1}" = "all" ]; then
checks="$(get_checks_names)"
else
checks="${1}"
fi
warn_wrapper_names "${checks}"
header="Check\tStatus\tRe-enable time\tDisable message"
underline="-----\t------\t--------------\t---------------"
str_out="${header}\n${underline}\n"
@ -329,14 +403,15 @@ function alerts_status() {
status_str="Not configured"
else
is_disabled="$(is_disabled "${check}")"
wrapper_name="$(get_check_wrapper_name "${check}")"
if [ "${is_disabled}" = "True" ]; then
status_str="Disabled"
enable_time="$(get_enable_time "${check}")"
enable_time="$(get_enable_time "${wrapper_name}")"
enable_delay="$(enable_delay "${enable_time}")"
delay_str="$(delay_to_string "${enable_delay}")"
enable_date="$(date --date "+${enable_delay} seconds" "+%d %h %Y at %H:%M:%S")"
enable_str="${enable_date} (${delay_str} left)"
disable_msg="$(get_disable_message "${check}")"
disable_msg="$(get_disable_message "${wrapper_name}")"
fi
fi
case "${status_str}" in
@ -433,16 +508,12 @@ while :; do
fi
;;
enable|disable)
if is_wrapper "${1}" || [ "${1}" = "all" ]; then
if is_wrapper "${1}" || is_check "${1}" || [ "${1}" = "all" ]; then
check_name="${1}"
else
if is_check "${1}"; then
error "Error: check '${1}' is not configured, it cannot be disabled. Aborted."
else
# We use the word "check" for the end user,
# but this is actually "unknown wrapper"
usage_error "Action ${action}: unknown check '${1}'."
fi
# We use the word "check" for the end user,
# but this is actually "unknown wrapper"
usage_error "Action ${action}: unknown check '${1}'."
fi
;;
*)

View file

@ -139,10 +139,11 @@ function delay_to_string() {
}
function is_disabled() {
# $1: wrapper name
disable_file_path="$(get_disable_file_path "${1}")"
# $1: check name
wrapper="$(get_check_wrapper_name "${1}")"
disable_file_path="$(get_disable_file_path "${wrapper}")"
if [ -e "${disable_file_path}" ]; then
enable_time="$(get_enable_time "${1}")"
enable_time="$(get_enable_time "${wrapper}")"
enable_delay="$(enable_delay "${enable_time}")"
if [ "${enable_delay}" -le "0" ]; then
echo "False"
@ -234,7 +235,7 @@ function not_wrapped_checks() {
done
}
# Fail if check is mot wrapped
# Fail if check is not wrapped
function is_wrapped() {
# $1: check name
cmd=$(get_check_commands "${1}" | tail -n1)
@ -250,6 +251,15 @@ function get_wrappers_names() {
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
function get_check_wrapper_name() {
# $1: check name
cmd=$(get_check_commands "${1}" | tail -n1)
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]}'
fi
}
function is_check() {
# $1: check name
checks="$(get_checks_names)"
@ -268,3 +278,9 @@ function is_wrapper() {
return 1
}
# Print the checks that name this wrapper
function get_wrapper_checks() {
# $1: wrapper name
conf_lines="$(get_nrpe_conf "${nrpe_conf_path}")"
echo "${conf_lines}" | grep -E "command\[check_.*\]=" | grep -E "\-\-name\s*${1}" | awk -F"[\\\[\\\]=]" '{sub("check_", "", $2); print $2}' | sort | uniq | xargs
}