From 7edd8ac5b8ef53f73c3d7bb72cf809b51e460f98 Mon Sep 17 00:00:00 2001 From: William Hirigoyen Date: Fri, 16 Feb 2024 11:58:19 +0100 Subject: [PATCH] =?UTF-8?q?Fin=20de=20cr=C3=A9neau=20(code=20non=20utilisa?= =?UTF-8?q?ble)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nagios-nrpe/files/alerts_switch | 99 +++++---- nagios-nrpe/files/alerts_wrapper | 204 ++++++++---------- nagios-nrpe/files/monitoringctl.sh | 75 ++++--- nagios-nrpe/files/monitoringctl_completion | 25 +++ .../files/monitoringctl_completion_dev | 95 ++++++++ 5 files changed, 312 insertions(+), 186 deletions(-) create mode 100755 nagios-nrpe/files/monitoringctl_completion create mode 100644 nagios-nrpe/files/monitoringctl_completion_dev diff --git a/nagios-nrpe/files/alerts_switch b/nagios-nrpe/files/alerts_switch index c9f0e1d7..36f48c79 100755 --- a/nagios-nrpe/files/alerts_switch +++ b/nagios-nrpe/files/alerts_switch @@ -14,44 +14,59 @@ readonly log_file="/var/log/monitoringctl.log" duration="1h" usage() { - echo "$PROGNAME disables or enables NRPE alerts wrapped by the script 'alerts_wrapper' in NRPE configuration." - echo "Usage: $PROGNAME disable [-d|--duration ] " - echo " $PROGNAME enable " - echo " $PROGNAME help" - echo "NAME: one of the names given to '--names' option of 'alerts_wrapper'." - echo "DURATION: Duration of alert disabling." - echo " Can be '1d' for 1 day, '5m' for 5 minutes or more complex expressions like '1w2d10m42s' (if no time unit is provided, hour is assumed)" - echo " Default value: 1h" + cat <] + $PROGNAME enable + $PROGNAME help + +CHECK_NAME: the name given to '--name' option of 'alerts_wrapper'. +DURATION: Duration of alert disabling. + Can be '1d' for 1 day, '5m' for 5 minutes or more complex + expressions like '1w2d10m42s' (if no time unit is provided, + hour is assumed) + Default value: 1h +END } -time_in_seconds() { +error() { + # $1: error message + >&2 echo "$1" + usage + exit 1 +} + +time_to_seconds() { + # $1: time in string format if echo "${1}" | grep -E -q '^([0-9]+[wdhms])+$'; then echo "${1}" | sed 's/w/ * 604800 + /g; s/d/ * 86400 + /g; s/h/ * 3600 + /g; s/m/ * 60 + /g; s/s/ + /g; s/+ $//' | xargs expr elif echo "${1}" | grep -E -q '^([0-9]+$)'; then echo "${1} * 3600" | xargs expr else - >&2 echo "Invalid duration: '${1}'." - usage - exit 1 + error "Invalid duration: '${1}'." fi } get_disable_names() { - echo "all,$(grep "alerts_wrapper" -Rs /etc/nagios/ | grep -vE "^\s*#" | awk '{ for (i=1; i<=NF; i++) { if ($i ~ /(-n|--name[s]?)/) print $(i+1) } }')" | tr ',' '\n' | sort | uniq + grep "alerts_wrapper" -Rs /etc/nagios/ | grep -vE "^\s*#" | awk '{ for (i=1; i<=NF; i++) { if ($i ~ /(-n|--name)/) print $(i+1) } }' | tr ',' '\n' | sort | uniq } - disable_alerts () { + # $1: check name + disable_file_path="${base_dir}/${check_name}_alerts_disabled" echo "${duration_sec}" > "${disable_file_path}" chmod 0644 "${disable_file_path}" - log_disable + log_disable "$1" } enable_alerts () { + # $1: check name + disable_file_path="${base_dir}/${check_name}_alerts_disabled" if [ -e "${disable_file_path}" ]; then rm "${disable_file_path}" fi - log_enable + log_enable "$1" } now () { @@ -59,24 +74,29 @@ now () { } log_disable () { - echo "$(now) - alerts_switch: ${disable_name} alerts disabled by $(logname || echo unknown)" >> $log_file + # $1: check name + echo "$(now) - alerts_switch: $1 alerts disabled by $(logname || echo unknown)" >> $log_file } log_enable () { - echo "$(now) - alerts_switch: ${disable_name} alerts enabled by $(logname || echo unknown)" >> $log_file + # $1: check name + echo "$(now) - alerts_switch: $1 alerts enabled by $(logname || echo unknown)" >> $log_file } main () { - disable_file_path="${base_dir}/${disable_name}_alerts_disabled" - mkdir -p "${base_dir}" + #TODO + if all + for check in $(get_disable_names); do + done + if [ "${action}" == 'enable' ]; then - enable_alerts + enable_alerts "${check_name}" elif [ "${action}" == 'disable' ]; then - duration_sec=$(time_in_seconds "${duration}") - disable_alerts + duration_sec=$(time_to_seconds "${duration}") + disable_alerts "${check_name}" elif [ "${action}" == 'help' ]; then usage fi @@ -88,42 +108,39 @@ while :; do enable|disable|help) action="$1" shift;; - -d|--duration) + -d|--during) if [ "$#" -gt 1 ]; then duration="$2" else - >&2 echo "Missing --duration argument." - usage - exit 1 + error "Missing --during argument." fi shift; shift;; *) if [ -z "${action}" ]; then - >&2 echo "Missing action argument." - usage - exit 1 - fi - if [ -z "$1" ]; then + error "Missing action argument." + elif [ -n "${check_name}" ]; then + error "Unknown argument '$1'." + elif [ -z "$1" ]; then break fi get_disable_names | grep --quiet -E "^$1$" arg_is_in_disable_names_rc=$? - if [ "${arg_is_in_disable_names_rc}" -eq 0 ] && [ -z "${disable_name}" ]; then - disable_name="$1" + if [ "${arg_is_in_disable_names_rc}" -eq 0 ] || [ "$1" == "all" ]; then + check_name="$1" else - >&2 echo "Unknown argument '$1', or NAME not defined in NRPE configuration." - usage - exit 1 + error "Unknown argument '$1', or NAME not defined in NRPE configuration." fi shift;; esac done -if [ -z "${disable_name}" ] && [ "${action}" != 'help' ] ; then - >&2 echo "Missing NAME argument." - usage - exit 1 +if [ -z "${check_name}" ] && [ "${action}" != 'help' ] ; then + error "Missing CHECK_NAME argument." fi +readonly check_name +readonly duration +readonly action + main diff --git a/nagios-nrpe/files/alerts_wrapper b/nagios-nrpe/files/alerts_wrapper index e2f0533f..da68c24e 100755 --- a/nagios-nrpe/files/alerts_wrapper +++ b/nagios-nrpe/files/alerts_wrapper @@ -4,15 +4,14 @@ # https://gitea.evolix.org/evolix/ansible-roles/src/branch/stable/nagios-nrpe # -VERSION="21.04" -readonly VERSION +readonly VERSION="21.04" + +# Location of disable files readonly base_dir="/var/lib/monitoringctl" -# Default maximum allowed disable time -disable_max_time_default="1d" -readonly disable_max_time_default +# If no datetime is found in file, this value is used +readonly default_disabled_time="1d" -# base functions show_version() { cat < -Usage: alerts_wrapper disable_name +Usage: alerts_wrapper --name +Usage: alerts_wrapper (deprecated) Options - -m|--max|--maximum Max age of the disable file. - Can be "1d" for 1 day, "5m" for 5 minutes… - or more complex expressions like "1w2d10m42s" - If no time unit is provided, hour is assumed. - --limit (deprecated) Same as above (kept for backward compatibility). - -n|--names Disable name (recommanded: set at least the check name). - Special name: 'all' is already defined (and cannot be removed). - --name (deprecated) Disable name (kept for backward compatibility). - -h, --help Print this message and exit. - -V, --version Print version and exit. + --name Check name (like load, disk1…) + Special name: 'all' is already hard-coded. + -h, --help Print this message and exit. + -V, --version Print version and exit. END } -time_in_seconds() { +time_to_seconds() { + # $1: formated time string if echo "${1}" | grep -E -q '^([0-9]+[wdhms])+$'; then echo "${1}" | sed 's/w/ * 604800 + /g; s/d/ * 86400 + /g; s/h/ * 3600 + /g; s/m/ * 60 + /g; s/s/ + /g; s/+ $//' | xargs expr elif echo "${1}" | grep -E -q '^([0-9]+$)'; then @@ -59,23 +54,57 @@ time_in_seconds() { } delay_from_disable_file() { - # $1: disabled file - last_change=$(stat -c %Z "$1") - - disable_secs=$(grep -v -E "^\s*#" "${1}" | grep -E "[0-9]+" | head -n1 | awk '{print$1}') - disable_max_secs=$(time_in_seconds "${disable_max_time}" || time_in_seconds "${disable_max_time_default}") - - if [ "${disable_secs}" -gt "${disable_max_secs}" ]; then - disable_secs="${disable_max_secs}" + # $1: disabled file containing the re-enable time in sec + enable_secs=$(grep -v -E "^\s*#" "${1}" | grep -E "[0-9]+" | head -n1 | awk '{print $1}') + # If file is empty, use file last change date plus default disabled time + if [ -z "${enable_secs}" ]; then + file_last_change_secs=$(stat -c %Z "$1") + default_disabled_time_secs="$(time_to_seconds "${default_disabled_time}")" + enable_secs="$(( file_last_change_secs + default_disabled_time_secs))" fi - disable_date=$(date --date "${disable_secs} seconds ago" +"%s") - - echo $(( last_change - disable_date )) + now_secs=$(date +"%s") + echo $(( enable_secs - now_secs )) } -enable_checks() { - # $1: disable name +delay_to_string() { + #$1 delay in secs + delay_days="$(( delay /86400 ))" + if [ "${delay_days}" -eq 0 ]; then delay_days="" + else delay_days="${delay_days}d "; fi + + delay_hours="$(( (delay %86400) /3600 ))" + if [ "${delay_hours}" -eq 0 ]; then delay_hours="" + else delay_hours="${delay_hours}h "; fi + + delay_minutes="$(( ((delay %86400) %3600) /60 ))" + if [ "${delay_minutes}" -eq 0 ]; then delay_minutes="" + else delay_minutes="${delay_minutes}m "; fi + + delay_seconds="$(( ((delay %86400) %3600) %60 ))" + if [ "${delay_seconds}" -eq 0 ]; then delay_seconds="" + else delay_days="${delay_days}s "; fi + + echo "${delay_days}${delay_hours}${delay_minutes}${delay_seconds}" +} + +is_disabled() { + # $1: disabled file containing the re-enable time in sec + if [ -e "$1" ]; then + delay=$(delay_from_disable_file "$1") + if [ "${delay}" -le "0" ]; then + echo "False" + enable_check "${check_name}" + else + echo "True" + fi + else + echo False + fi +} + +enable_check() { + # $1: check name if [ "$(id -u)" -eq "0" ] ; then /usr/local/bin/alerts_switch enable "$1" else @@ -84,58 +113,33 @@ enable_checks() { } main() { - check_stdout=$(timeout 9 ${check_command}) + is_disabled=$(is_disabled "${disable_file}") + + timeout_command="" + if [ "${is_disabled}" == "True" ]; then + timeout_command="timeout 9" + fi + + check_stdout=$(${timeout_command} "${check_command}") check_rc=$? - if [ "${check_rc}" -eq 124 ] && [ -z "${check_stdout}" ]; then + if [ "${is_disabled}" == "True" ] && [ "${check_rc}" -eq 124 ] && [ -z "${check_stdout}" ]; then check_stdout="Check timeout (9 sec)" fi - delay=0 - disabled="False" - for disable_name in ${disable_names}; do - disable_file="${base_dir}/${disable_name}_alerts_disabled" - if [ -e "${disable_file}" ]; then - delay=$(delay_from_disable_file "${disable_file}") - - if [ "${delay}" -le "0" ]; then - enable_checks "${disable_name}" - fi - fi - - if [ -e "${disable_file}" ]; then - disabled="True" - formatted_last_change=$(date --date "@$(stat -c %Z "${disable_file}")" +'%c') - - delay_days="$(( delay /86400 ))" - if [ "${delay_days}" -eq 0 ]; then delay_days="" - else delay_days="${delay_days}d "; fi - - delay_hours="$(( (delay %86400) /3600 ))" - if [ "${delay_hours}" -eq 0 ]; then delay_hours="" - else delay_hours="${delay_hours}h "; fi - - delay_minutes="$(( ((delay %86400) %3600) /60 ))" - if [ "${delay_minutes}" -eq 0 ]; then delay_minutes="" - else delay_minutes="${delay_minutes}m "; fi - - delay_seconds="$(( ((delay %86400) %3600) %60 ))" - if [ "${delay_seconds}" -eq 0 ]; then delay_seconds="" - else delay_days="${delay_days}s "; fi - - echo "ALERTS DISABLED for ${disable_names} (since ${formatted_last_change}, delay: ${delay_days}${delay_hours}${delay_minutes}${delay_seconds})." - fi - done + if [ "${is_disabled}" == "True" ]; then + delay=$(delay_from_disable_file "${disable_file}") + delay_str="$(delay_to_string "${delay}")" + echo "ALERTS DISABLED for ${check_name} until ${delay_str}." + fi echo "${check_stdout}" - if [ "${disabled}" == "True" ]; then + if [ "${is_disabled}" == "True" ]; then if [ ${check_rc} = 0 ]; then - # Nagios OK - exit 0 + exit 0 # Nagios OK else - # Nagios WARNING - exit 1 + exit 1 # Nagios WARNING fi else exit ${check_rc} @@ -156,42 +160,21 @@ if [[ "${1}" =~ -.* ]]; then show_version exit 0 ;; - - -m|--max|--maximum|--limit) + -n|--name) # with value separated by space if [ -n "$2" ]; then - disable_max_time=$2 - shift - else - printf 'ERROR: "--maximum" requires a non-empty option argument.\n' >&2 - exit 1 - fi - ;; - -m|--max|--maximum|--limit=?*) - # with value speparated by = - disable_max_time=${1#*=} - ;; - -m|--max|--maximum|--limit=) - # without value - printf 'ERROR: "--maximum" requires a non-empty option argument.\n' >&2 - exit 1 - ;; - - -n|--name|--names) - # with value separated by space - if [ -n "$2" ]; then - disable_names=$2 + check_name=$2 shift else printf 'ERROR: "--name" requires a non-empty option argument.\n' >&2 exit 1 fi ;; - -n|--name=?*|--names=?*) - # with value speparated by = - disable_names=${1#*=} + -n|--name=?*) + # with value separated by = + check_name=${1#*=} ;; - -n|--name=|--names=) + -n|--name=) # without value printf 'ERROR: "--name" requires a non-empty option argument.\n' >&2 exit 1 @@ -219,28 +202,23 @@ if [[ "${1}" =~ -.* ]]; then check_command="$*" else # no option is passed (backward compatibility with previous version) - # treat the first argument as disable_names and the rest as the command - disable_names="${1}" + # treat the first argument as check_name and the rest as the command + check_name="${1}" shift check_command="$*" fi -# Default values or errors -if [ -z "${disable_max_time}" ]; then - disable_max_time="${disable_max_time_default}" +if [ -z "${check_name}" ]; then + printf 'ERROR: You must specify a check name, with --names.\n' >&2 + exit 1 fi -#if [ -z "${disable_names}" ]; then -# printf 'ERROR: You must specify a check name, with --names.\n' >&2 -# exit 1 -#fi if [ -z "${check_command}" ]; then printf 'ERROR: You must specify a command to execute.\n' >&2 exit 1 fi -disable_names="all $(echo "${disable_names}" | tr ',' ' ')" -readonly disable_names +readonly check_name readonly check_command -readonly disable_max_time +readonly disable_file="${base_dir}/${check_name}_alerts_disabled" main diff --git a/nagios-nrpe/files/monitoringctl.sh b/nagios-nrpe/files/monitoringctl.sh index 48374589..2592b236 100755 --- a/nagios-nrpe/files/monitoringctl.sh +++ b/nagios-nrpe/files/monitoringctl.sh @@ -2,12 +2,15 @@ #set -x +VERSION="24.03.00" + readonly base_dir="/var/lib/monitoringctl" readonly log_path="/var/log/monitoringctl.log" readonly conf_path="/etc/nagios/nrpe.cfg" function show_help { cat <&2 echo "$1" @@ -149,7 +157,6 @@ function get_check_commands { function check { # $1: check name - check_nrpe_bin=/usr/lib/nagios/plugins/check_nrpe if [ ! -f "${check_nrpe_bin}" ]; then @@ -218,7 +225,7 @@ function filter_duration { if [[ "$1" =~ ${time_regex} ]]; then echo "$1" else - usage_error "Option --duration: \"$1\" is not a valid duration." + usage_error "Option --during: \"$1\" is not a valid duration." fi } @@ -248,19 +255,19 @@ function disable_alerts { # -> mauvais indicateur, cf. le timeout à l'intérieur + le max autorisé dans la commande alerts_wrapper #if [ -f "${base_dir}/all_alerts_disabled" ]; then # echo "All alerts are already disabled." - # alerts-status + # status #fi default_msg="." if [ "${default_duration}" = "True" ]; then default_msg=" (default value). - Hint: use --duration DURATION to change default time length." + Hint: use --during DURATION to change default time length." fi cat <