Améliorations diverses UI, message de désactivation dans alerts_wrapper, couleurs...
All checks were successful
Ansible Lint |Total|New|Outstanding|Fixed|Trend |:-:|:-:|:-:|:-:|:-: |2695|0|2695|0|:zzz:
gitea/ansible-roles/pipeline/head This commit looks good

This commit is contained in:
William Hirigoyen 2024-03-26 18:00:59 +01:00
parent 2942ee1d4b
commit 2008c8caf4
5 changed files with 118 additions and 58 deletions

View file

@ -21,8 +21,8 @@ function show_help() {
cat <<END cat <<END
$PROGNAME disables or enables NRPE alerts wrapped by the script 'alerts_wrapper' in NRPE configuration. $PROGNAME disables or enables NRPE alerts wrapped by the script 'alerts_wrapper' in NRPE configuration.
Usage: $PROGNAME disable [-d|--during <DURATION>] <WRAPPER_NAME|all> Usage: $PROGNAME disable [-d|--during <DURATION>] [--message '<DISABLE_MESSAGE>'] <WRAPPER_NAME|all>
$PROGNAME enable <WRAPPER_NAME|all> $PROGNAME enable [--message '<ENABLE_MESSAGE>'] <WRAPPER_NAME|all>
$PROGNAME help $PROGNAME help
WRAPPER_NAME: The name given to '--name' option of 'alerts_wrapper'. WRAPPER_NAME: The name given to '--name' option of 'alerts_wrapper'.
@ -31,26 +31,32 @@ DURATION: Duration of alert disabling.
expressions like '1w2d10m42s' (if no time unit is provided, expressions like '1w2d10m42s' (if no time unit is provided,
hour is assumed) hour is assumed)
Default value: 1h Default value: 1h
DISABLE_MESSAGE: Message that will be logged and printed by alerts_wrapper
when alert is disabled.
ENABLE_MESSAGE: Message that will be logged when alert is enabled
END END
} }
function disable_alerts() { function disable_alerts() {
# $1: wrapper name, $2: duration_sec # $1: wrapper name, $2: duration_sec, $3: disable message
now_secs=$(date +"%s") now_secs=$(date +"%s")
disable_until_secs=$(( now_secs + ${2} )) disable_until_secs=$(( now_secs + ${2} ))
disable_file_path="$(get_disable_file_path "${1}")" disable_file_path="$(get_disable_file_path "${1}")"
echo "${disable_until_secs}" > "${disable_file_path}" echo "${disable_until_secs}" > "${disable_file_path}"
echo "$(logname || echo unknown): \"${3}\"" >> "${disable_file_path}"
chmod 0644 "${disable_file_path}" chmod 0644 "${disable_file_path}"
log "${1} alerts disabled by $(logname || echo unknown)" log "${1} alerts disabled by $(logname || echo unknown)"
log "Disable message: ${3}"
} }
function enable_alerts() { function enable_alerts() {
# $1: wrapper name # $1: wrapper name, $2: enable message
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
rm "${disable_file_path}" rm "${disable_file_path}"
fi fi
log "${1} alerts enabled by $(logname || echo unknown)" log "${1} alerts enabled by $(logname || echo unknown)"
log "Enable message: ${2}"
} }
function main() { function main() {
@ -62,7 +68,7 @@ function main() {
enable_alerts "${wrapper}" enable_alerts "${wrapper}"
done done
else else
enable_alerts "${wrapper_name}" enable_alerts "${wrapper_name}" "${message}"
fi fi
elif [ "${action}" == 'disable' ]; then elif [ "${action}" == 'disable' ]; then
duration_sec=$(time_to_seconds "${duration}") duration_sec=$(time_to_seconds "${duration}")
@ -71,7 +77,7 @@ function main() {
disable_alerts "${wrapper}" "${duration_sec}" disable_alerts "${wrapper}" "${duration_sec}"
done done
else else
disable_alerts "${wrapper_name}" "${duration_sec}" disable_alerts "${wrapper_name}" "${duration_sec}" "${message}"
fi fi
elif [ "${action}" == 'help' ]; then elif [ "${action}" == 'help' ]; then
show_help show_help
@ -95,6 +101,13 @@ while :; do
error "Missing --during argument." error "Missing --during argument."
fi fi
shift; shift;; shift; shift;;
-m|--message)
if [ "$#" -gt 1 ]; then
message="${2}"
else
error "Missing --message argument."
fi
shift; shift;;
*) *)
if [ -n "${1}" ]; then if [ -n "${1}" ]; then
if is_wrapper "${1}" || [ "${1}" == "all" ]; then if is_wrapper "${1}" || [ "${1}" == "all" ]; then

View file

@ -65,7 +65,11 @@ function main() {
enable_delay="$(enable_delay "${enable_time}")" enable_delay="$(enable_delay "${enable_time}")"
delay_str="$(delay_to_string "${enable_delay}")" delay_str="$(delay_to_string "${enable_delay}")"
enable_date="$(date --date "+${enable_delay} seconds" "+%d %h %Y at %H:%M:%S")" enable_date="$(date --date "+${enable_delay} seconds" "+%d %h %Y at %H:%M:%S")"
echo "ALERT DISABLED until ${enable_date} (${delay_str} left) - ${check_stdout}" disable_msg="$(get_disable_message "${wrapper_name}")"
if [ -n "${disable_msg}" ]; then
disable_msg="- ${disable_msg} "
fi
echo "ALERT DISABLED until ${enable_date} (${delay_str} left) ${disable_msg}- Check output: ${check_stdout}"
else else
echo "${check_stdout}" echo "${check_stdout}"
fi fi

View file

@ -42,7 +42,7 @@ ACTIONS:
status [CHECK_NAME|all] status [CHECK_NAME|all]
Print whether alerts are enabled or not (silenced). Print whether alerts are enabled or not (silenced).
If alerts are disabled (silenced), show comment and time left before automatic re-enabling. If alerts are disabled (silenced), show disable message and time left before automatic re-enabling.
check [--bypass-nrpe] CHECK_NAME check [--bypass-nrpe] CHECK_NAME
@ -51,12 +51,12 @@ ACTIONS:
-b, --bypass-nrpe Execute directly command from NRPE configuration, -b, --bypass-nrpe Execute directly command from NRPE configuration,
as user nagios, without passing the request to NRPE. as user nagios, without passing the request to NRPE.
disable CHECK_NAME|all [--during DURATION] --comment 'COMMENT' disable CHECK_NAME|all [--during DURATION] [--message 'DISABLE MESSAGE']
Disable (silence) CHECK_NAME or all alerts for DURATION and write COMMENT into the log. Disable (silence) CHECK_NAME or all alerts for DURATION and write DISABLE MESSAGE into the log.
Checks output is still printed, so alerts history won't be lost. Checks output is still printed, so alerts history won't be lost.
enable CHECK_NAME|all --comment 'COMMENT' enable CHECK_NAME|all [--message 'ENABLE MESSAGE']
Re-enable CHECK_NAME or all alerts Re-enable CHECK_NAME or all alerts
@ -64,9 +64,9 @@ ACTIONS:
Show NPRE command(s) configured for CHECK_NAME Show NPRE command(s) configured for CHECK_NAME
COMMENT: MESSAGE:
Comment string to be written in log (mandatory). Message to be written in log and disabled check output (mandatory, will be asked dynamically if not provided).
DURATION: DURATION:
@ -129,7 +129,7 @@ function check() {
if [ -z "${1}" ] || [ "${1}" = "all" ]; then if [ -z "${1}" ] || [ "${1}" = "all" ]; then
err_msg="Check command not found in NRPE configuration." err_msg="Check command not found in NRPE configuration."
else else
err_mgs="Warning: no command found in NRPE configuration for check '${check}'. Aborted." err_mgs="Error: no command found in NRPE configuration for check '${check}'. Aborted."
fi fi
fi fi
fi fi
@ -137,8 +137,12 @@ function check() {
check_output="$(${request_command})" check_output="$(${request_command})"
rc="$?" rc="$?"
check_output="$(echo "${check_output}" | tr '\n' ' ')" check_output="$(echo "${check_output}" | tr '\n' ' ')"
if [ -z "${1}" ] || [ "${1}" = "all" ]; then
term_cols="$(tput cols)"
if [ "${#check_output}" -gt 60 ]; then if [ "${#check_output}" -gt 60 ]; then
check_output="$(echo "${check_output}" | cut -c-60) [...]" check_output="$(echo "${check_output}" | cut -c-80) [...]"
fi
fi fi
else else
check_output="${err_msg}" check_output="${err_msg}"
@ -166,6 +170,7 @@ function check() {
rc_str="Unknown" rc_str="Unknown"
color="${purple}" color="${purple}"
esac esac
if [ -z "${1}" ] || [ "${1}" = "all" ]; then if [ -z "${1}" ] || [ "${1}" = "all" ]; then
str_out="${str_out}${color}${check}\t${rc_str}\t${check_output}${nocolor}\n" str_out="${str_out}${color}${check}\t${rc_str}\t${check_output}${nocolor}\n"
fi fi
@ -180,7 +185,7 @@ function check() {
} }
function disable_alerts() { function disable_alerts() {
# $1: check name, $2: comment # $1: check name, $2: disable message
if ! command -v alerts_switch &> /dev/null; then if ! command -v alerts_switch &> /dev/null; then
error "Error: script 'alerts_switch' is not installed. Aborted." error "Error: script 'alerts_switch' is not installed. Aborted."
fi fi
@ -191,11 +196,22 @@ function disable_alerts() {
checks="${1}" checks="${1}"
fi fi
if [ -z "${2}" ]; then
echo -n "> Please provide a disable message (for logging and check output): "
read -r message
echo ''
if [ -z "${message}" ]; then
error "Error: disable message is mandatory."
fi
else
message="${2}"
fi
# Verify that checks to disable are wrapped # Verify that checks to disable are wrapped
unwrappeds="$(not_wrapped_checks)" unwrappeds="$(not_wrapped_checks)"
unwrapped_checks="$(comm -12 <(echo "${checks}") <(echo "${unwrappeds}"))" unwrapped_checks="$(comm -12 <(echo "${checks}") <(echo "${unwrappeds}"))"
if [ -n "${unwrapped_checks}" ]; then if [ -n "${unwrapped_checks}" ]; then
>&2 printf "Warning: some checks have no alerts_wrapper, they will not be disabled:" >&2 printf "${orange}Warning:${nocolor} some checks are not configured, they will not be disabled:"
for unwrapped in ${unwrapped_checks}; do for unwrapped in ${unwrapped_checks}; do
>&2 printf " %s" "${unwrapped}" >&2 printf " %s" "${unwrapped}"
done done
@ -212,20 +228,27 @@ function disable_alerts() {
else else
check_txt="Check ${1}" check_txt="Check ${1}"
fi fi
main_msg="${check_txt} will be disabled for ${duration}${default_msg}."
main_msg_len="${#main_msg}"
line="$(printf '─%.0s' $(eval "echo {1.."${main_msg_len}"}"))"
cat <<EOF cat <<EOF
${check_txt} will be disabled for ${duration}${default_msg}. ┌${line}┐
Alerts history is kept in our monitoring system. │${main_msg}│
To re-enable alert(s) before ${duration}, execute as root or with sudo: 'monitoringctl enable ${1} --comment "YOUR REASON"' └${line}┘
Additional information:
* Alerts history is kept in our monitoring system.
* To re-enable alert(s) before ${duration}, execute as root or with sudo: 'monitoringctl enable ${1}'
EOF EOF
echo -n "Confirm (y/N)? " echo -n "> Confirm (y/N)? "
read -r answer read -r answer
if [ "${answer}" != "Y" ] && [ "${answer}" != "y" ]; then if [ "${answer}" != "Y" ] && [ "${answer}" != "y" ]; then
echo "Canceled." && exit 0 echo -e "${orange}Canceled.${nocolor}" && exit 0
fi fi
log "Action disable ${1} requested for ${duration} by user $(logname || echo unknown)." log "Action disable ${1} requested for ${duration} by user $(logname || echo unknown)."
log "Comment: '${2}'"
# Log a warning if a check has no wrapper # Log a warning if a check has no wrapper
if [ -n "${unwrapped_checks}" ]; then if [ -n "${unwrapped_checks}" ]; then
@ -235,14 +258,13 @@ EOF
done done
fi fi
log "Executing 'alerts_switch disable ${1} --during \"${duration}\"'" alerts_switch disable "${1}" --during "${duration}" --message "${message}"
alerts_switch disable "${1}" --during "${duration}"
echo "${1} alerts are now disabled for ${duration}" echo -e "${orange}Check ${1} alerts are now disabled for ${duration}${nocolor}"
} }
function enable_alerts() { function enable_alerts() {
# $1: wrapper name, $2: comment # $1: wrapper name, $2: enable message
if [ "${1}" != "all" ]; then if [ "${1}" != "all" ]; then
is_disabled="$(is_disabled "${1}")" is_disabled="$(is_disabled "${1}")"
if [ "${is_disabled}" = "False" ]; then if [ "${is_disabled}" = "False" ]; then
@ -251,12 +273,21 @@ function enable_alerts() {
fi fi
fi fi
log "Action enable ${1} requested by user $(logname || echo unknown)." if [ -z "${2}" ]; then
log "Comment: '${2}'" echo -n "> Please provide an enable message (for logging): "
log "Executing 'alerts_switch enable ${1}'" read -r message
alerts_switch enable "${1}" echo ''
if [ -z "${message}" ]; then
error "Error: disable message is mandatory."
fi
else
message="${2}"
fi
echo "${1} alerts are now enabled." 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}"
} }
# Show NRPE command(s) configured for a check # Show NRPE command(s) configured for a check
@ -286,15 +317,16 @@ function alerts_status() {
checks="${1}" checks="${1}"
fi fi
header="Check\tStatus\tRe-enable time" header="Check\tStatus\tRe-enable time\tDisable message"
underline="-----\t------\t--------------" underline="-----\t------\t--------------\t---------------"
str_out="${header}\n${underline}\n" str_out="${header}\n${underline}\n"
for check in $checks; do for check in $checks; do
enable_str="" enable_str=""
status_str="Enabled" status_str="Enabled"
disable_msg=""
if ! is_wrapped "${check}"; then if ! is_wrapped "${check}"; then
status_str="Not wrapped" status_str="Not configured"
else else
is_disabled="$(is_disabled "${check}")" is_disabled="$(is_disabled "${check}")"
if [ "${is_disabled}" = "True" ]; then if [ "${is_disabled}" = "True" ]; then
@ -304,6 +336,7 @@ function alerts_status() {
delay_str="$(delay_to_string "${enable_delay}")" delay_str="$(delay_to_string "${enable_delay}")"
enable_date="$(date --date "+${enable_delay} seconds" "+%d %h %Y at %H:%M:%S")" enable_date="$(date --date "+${enable_delay} seconds" "+%d %h %Y at %H:%M:%S")"
enable_str="${enable_date} (${delay_str} left)" enable_str="${enable_date} (${delay_str} left)"
disable_msg="$(get_disable_message "${check}")"
fi fi
fi fi
case "${status_str}" in case "${status_str}" in
@ -316,7 +349,7 @@ function alerts_status() {
*) *)
color="${red}" color="${red}"
esac esac
str_out="${str_out}${color}${check}\t${status_str}\t${enable_str}${nocolor}\n" str_out="${str_out}${color}${check}\t${status_str}\t${enable_str}${nocolor}\t${disable_msg}\n"
done done
echo -e "${str_out}" | column -t -s $'\t' echo -e "${str_out}" | column -t -s $'\t'
@ -339,7 +372,7 @@ fi
# Default arguments and options # Default arguments and options
action="" action=""
comment="" message=""
duration="${default_disabled_time}" duration="${default_disabled_time}"
bypass_nrpe="False" bypass_nrpe="False"
default_duration="True" default_duration="True"
@ -370,11 +403,11 @@ while :; do
fi fi
default_duration="False" default_duration="False"
shift; shift;; shift; shift;;
-c|--comment) -m|--message)
if [ "$#" -lt 2 ]; then if [ "$#" -lt 2 ]; then
usage_error "Option --comment: missing comment string." usage_error "Option --message: missing message string."
fi fi
comment="${2}" message="${2}"
shift; shift;; shift; shift;;
status|check|enable|disable|show) status|check|enable|disable|show)
action="${1}" action="${1}"
@ -404,7 +437,7 @@ while :; do
check_name="${1}" check_name="${1}"
else else
if is_check "${1}"; then if is_check "${1}"; then
error "Error: check '${1}' has no alerts_wrapper, it cannot be disabled. Aborted." error "Error: check '${1}' is not configured, it cannot be disabled. Aborted."
else else
# We use the word "check" for the end user, # We use the word "check" for the end user,
# but this is actually "unknown wrapper" # but this is actually "unknown wrapper"
@ -433,12 +466,6 @@ case "${action}" in
;; ;;
esac esac
if [ "${action}" = "enable" ] || [ "${action}" = "disable" ]; then
if [ -z "${comment}" ]; then
usage_error "Action ${action}: missing --comment argument."
fi
fi
if [ ! "${action}" = "disable" ]; then if [ ! "${action}" = "disable" ]; then
if [ "${default_duration}" = "False" ]; then if [ "${default_duration}" = "False" ]; then
usage_error "Action ${action}: there is no --during option." usage_error "Action ${action}: there is no --during option."
@ -456,10 +483,10 @@ case "${action}" in
show_check_commands "${check_name}" show_check_commands "${check_name}"
;; ;;
enable) enable)
enable_alerts "${check_name}" "${comment}" enable_alerts "${check_name}" "${message}"
;; ;;
disable) disable)
disable_alerts "${check_name}" "${comment}" disable_alerts "${check_name}" "${message}"
;; ;;
esac esac

View file

@ -75,7 +75,11 @@ function time_to_seconds() {
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}")"
enable_secs="$(grep -v -E "^\s*#" "${disable_file_path}" | grep -E "^[0-9]+$" | head -n1 | awk '{print $1}')" if [ ! -e "${disable_file_path}" ]; then
return
fi
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}")"
@ -85,6 +89,18 @@ function get_enable_time() {
echo "${enable_secs}" echo "${enable_secs}"
} }
# Print disable message
function get_disable_message() {
# $1: wrapper name
disable_file_path="$(get_disable_file_path "${1}")"
if [ ! -e "${disable_file_path}" ]; then
return
fi
disable_msg="$(sed '/^$/d' "${disable_file_path}" | tail -n+2 | tr '\n' ' ' | awk '{$1=$1;print}')"
echo "${disable_msg}"
}
function now_secs() { function now_secs() {
date +"%s" date +"%s"
} }

View file

@ -71,7 +71,7 @@
dest: /usr/local/sbin/monitoringctl dest: /usr/local/sbin/monitoringctl
owner: root owner: root
group: root group: root
mode: "0750" mode: "0755"
force: true force: true
- name: "copy monitoringctl_common lib" - name: "copy monitoringctl_common lib"