From 6aab5f1aa24a1836eb6cfb5743c4a056302b86ba Mon Sep 17 00:00:00 2001 From: William Hirigoyen Date: Tue, 27 Feb 2024 18:04:20 +0100 Subject: [PATCH] =?UTF-8?q?debug,=20am=C3=A9lioration=20compl=C3=A9tion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nagios-nrpe/files/alerts_switch | 69 +++--- nagios-nrpe/files/alerts_wrapper | 27 +-- nagios-nrpe/files/monitoringctl.sh | 200 ++++++++---------- nagios-nrpe/files/monitoringctl_completion | 92 ++++++-- .../files/monitoringctl_completion_dev | 95 --------- 5 files changed, 214 insertions(+), 269 deletions(-) mode change 100755 => 100644 nagios-nrpe/files/monitoringctl_completion delete mode 100644 nagios-nrpe/files/monitoringctl_completion_dev diff --git a/nagios-nrpe/files/alerts_switch b/nagios-nrpe/files/alerts_switch index 36f48c79..3b40b8c7 100755 --- a/nagios-nrpe/files/alerts_switch +++ b/nagios-nrpe/files/alerts_switch @@ -4,8 +4,6 @@ # https://gitea.evolix.org/evolix/ansible-roles/src/branch/stable/nagios-nrpe # -set -e - readonly PROGNAME=$(basename $0) readonly base_dir="/var/lib/monitoringctl" readonly log_file="/var/log/monitoringctl.log" @@ -41,28 +39,32 @@ 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 + elif echo "${1}" | grep -E -q '^([0-9]+h[0-9]+$)'; then + echo "${1}" | sed 's/h/ * 3600 + /g; s/$/ * 60/' | xargs expr + elif echo "${1}" | grep -E -q '^([0-9]+m[0-9]+$)'; then + echo "${1}" | sed 's/m/ * 60 + /g' | xargs expr else error "Invalid duration: '${1}'." fi } get_disable_names() { - 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 + grep "alerts_wrapper" -Rs /etc/nagios/ | grep -vE "^\s*#" | awk '{ for (i=1; i<=NF; i++) { if ($i ~ /^(-n|--name)$/) { print $(i+1); break } } }' | tr ',' '\n' | sort | uniq } disable_alerts () { - # $1: check name - disable_file_path="${base_dir}/${check_name}_alerts_disabled" - echo "${duration_sec}" > "${disable_file_path}" + # $1: check name, $2: duration_sec + now_secs=$(date +"%s") + disable_until_secs=$(( now_secs + $2 )) + disable_file_path="${base_dir}/${1}_alerts_disabled" + echo "${disable_until_secs}" > "${disable_file_path}" chmod 0644 "${disable_file_path}" log_disable "$1" } enable_alerts () { # $1: check name - disable_file_path="${base_dir}/${check_name}_alerts_disabled" + disable_file_path="${base_dir}/${1}_alerts_disabled" if [ -e "${disable_file_path}" ]; then rm "${disable_file_path}" fi @@ -85,18 +87,25 @@ log_enable () { main () { - mkdir -p "${base_dir}" - - #TODO - if all - for check in $(get_disable_names); do - done + install --mode=0755 --directory "${base_dir}" if [ "${action}" == 'enable' ]; then - enable_alerts "${check_name}" + if [ "${check_name}" == "all" ]; then + for check in $(get_disable_names); do + enable_alerts "${check}" + done + else + enable_alerts "${check_name}" + fi elif [ "${action}" == 'disable' ]; then duration_sec=$(time_to_seconds "${duration}") - disable_alerts "${check_name}" + if [ "${check_name}" == "all" ]; then + for check in $(get_disable_names); do + disable_alerts "${check}" "${duration_sec}" + done + else + disable_alerts "${check_name}" "${duration_sec}" + fi elif [ "${action}" == 'help' ]; then usage fi @@ -116,20 +125,22 @@ while :; do fi shift; shift;; *) - if [ -z "${action}" ]; 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 ] || [ "$1" == "all" ]; then - check_name="$1" + if [ -n "$1" ]; then + get_disable_names | grep --quiet -E "^$1$" + arg_is_in_disable_names_rc=$? + if [ "${arg_is_in_disable_names_rc}" -eq 0 ] || [ "$1" == "all" ]; then + check_name="$1" + else + error "Unknown argument '$1', or NAME not defined in NRPE configuration." + fi else - error "Unknown argument '$1', or NAME not defined in NRPE configuration." + if [ -z "${action}" ]; then + error "Missing action argument." + elif [ -z "$1" ]; then + break + fi fi + shift;; esac done diff --git a/nagios-nrpe/files/alerts_wrapper b/nagios-nrpe/files/alerts_wrapper index da68c24e..1696cf4a 100755 --- a/nagios-nrpe/files/alerts_wrapper +++ b/nagios-nrpe/files/alerts_wrapper @@ -55,7 +55,7 @@ time_to_seconds() { delay_from_disable_file() { # $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}') + 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") @@ -71,19 +71,19 @@ 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 + 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 + 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 + 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 + else delay_seconds="${delay_seconds}s"; fi echo "${delay_days}${delay_hours}${delay_minutes}${delay_seconds}" } @@ -120,7 +120,7 @@ main() { timeout_command="timeout 9" fi - check_stdout=$(${timeout_command} "${check_command}") + check_stdout=$(${timeout_command} ${check_command}) check_rc=$? if [ "${is_disabled}" == "True" ] && [ "${check_rc}" -eq 124 ] && [ -z "${check_stdout}" ]; then @@ -130,11 +130,11 @@ main() { 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}." + echo "ALERT DISABLED until ${delay_str} - ${check_stdout}" + else + echo "${check_stdout}" fi - echo "${check_stdout}" - if [ "${is_disabled}" == "True" ]; then if [ ${check_rc} = 0 ]; then exit 0 # Nagios OK @@ -179,7 +179,6 @@ if [[ "${1}" =~ -.* ]]; then printf 'ERROR: "--name" requires a non-empty option argument.\n' >&2 exit 1 ;; - --) # End of all options. shift @@ -210,11 +209,15 @@ fi if [ -z "${check_name}" ]; then printf 'ERROR: You must specify a check name, with --names.\n' >&2 - exit 1 + exit 2 fi if [ -z "${check_command}" ]; then printf 'ERROR: You must specify a command to execute.\n' >&2 - exit 1 + exit 2 +fi +if [ -e "${base_dir}" ] && [ ! -x "${base_dir}" ]; then + printf "ERROR: %s exists but is no reachable.\n" "${base_dir}" + exit 2 fi readonly check_name diff --git a/nagios-nrpe/files/monitoringctl.sh b/nagios-nrpe/files/monitoringctl.sh index 2592b236..f47b4289 100755 --- a/nagios-nrpe/files/monitoringctl.sh +++ b/nagios-nrpe/files/monitoringctl.sh @@ -20,7 +20,6 @@ GENERAL OPTIONS: -h, --help Print this message and exit. -V, --version Print version number and exit. -# -v, --verbose Print more informations. ACTIONS: @@ -28,40 +27,30 @@ ACTIONS: Ask CHECK_NAME status to NRPE as an HTTP request. Indicates which command NRPE has supposedly run (from its configuration). - - Options: - - -b, --bypass-nrpe Execute directly command from NRPE configuration, - without requesting to NRPE. + -b, --bypass-nrpe Execute directly command from NRPE configuration, + without passing the request to NRPE. status - Print : - - Wether alerts are enabled or not (silenced). - - If alerts are disabled (silenced): - - Comment. - - Time left before automatic re-enable. + Print whether alerts are enabled or not (silenced). + If alerts are disabled (silenced), show comment and time left before automatic re-enabling. - disable [--during DURATION] 'COMMENT' + disable CHECK_NAME|all [--during DURATION] --comment 'COMMENT' - Disable (silence) all alerts (only global for now) for DURATION and write COMMENT into the log. + Disable (silence) CHECK_NAME or all alerts for DURATION and write COMMENT into the log. Checks output is still printed, so alerts history won't be lost. - Options: - - -d, --during DURATION Specify disable duration (default: 1h). - - enable 'COMMENT' + enable CHECK_NAME|all --comment 'COMMENT' Re-enable all alerts (only global for now) COMMENT: - (mandatory) Comment (string) to be written in log. + Comment string to be written in log (mandatory). DURATION: - (optional, default: "1h") Time (string) during which alerts will be disabled (silenced). + Time (string) during which alerts will be disabled (optional, default: "1h"). Format: You can use 'd' (day), 'h' (hour) and 'm' (minute) , or a combination of them, to specify a duration. @@ -89,7 +78,7 @@ function now { function log { # $1: message - echo "$(now) - $1" >> "${log_path}" + echo "$(now) - monitoringctl: $1" >> "${log_path}" } @@ -152,6 +141,10 @@ function get_check_commands { echo "$conf_lines" | grep -E "command\[check_$1\]" | cut -d'=' -f2- } +# Print the names that are defined in the wrappers of the checks +function get_wrappers_names() { + grep "alerts_wrapper" -Rs /etc/nagios/ | grep -v -E "^\s*#" | awk '{ for (i=1 ; i<=NF; i++) { if ($i ~ /^(-n|--name)$/) { print $(i+1); break } } }' | tr ',' '\n' | sort | uniq +} ### CHECK ACTION ########################## @@ -164,10 +157,10 @@ function check { exit 1 fi - server_address=$(echo "$conf_lines" | grep "server_address" | cut -d'=' -f2) + server_address=$(echo "$conf_lines" | grep "server_address" | tail -n1 | cut -d'=' -f2) if [ -z "${server_address}" ]; then server_address="127.0.0.1"; fi - server_port=$(echo "$conf_lines" | grep "server_port" | cut -d'=' -f2) + server_port=$(echo "$conf_lines" | grep "server_port" | tail -n1 | cut -d'=' -f2) if [ -z "${server_port}" ]; then server_port="5666"; fi check_commands=$(get_check_commands "$1") @@ -220,7 +213,7 @@ function check { function filter_duration { # Format (in brief): XdYhZm # Minutes unit 'm' is not mandatory after Xh - time_regex="^([0-9]+[d])?(([0-9]+[h]([0-9]+[m]?)?)|(([0-9]+[m])?)))?$" + 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 echo "$1" @@ -243,7 +236,7 @@ function is_nrpe_wrapped { } function disable_alerts { - # $1: comment + # $1: check name, $2: comment if ! command -v alerts_switch &> /dev/null; then >&2 echo "Error: script 'alerts_switch' is not installed." @@ -267,7 +260,7 @@ function disable_alerts { Alerts will be disabled for ${duration}${default_msg} Our monitoring system will continue to gather checks outputs, so alerts history won't be lost. To re-enable alerts before ${duration}, execute (as root or with sudo): - monitoringctl enable + monitoringctl enable $1 --comment 'YOUR REASON' EOF echo -n "Confirm (y/N)? " read -r answer @@ -276,41 +269,39 @@ EOF exit 0 fi - log "Action disable requested for ${duration} by user $(logname || echo unknown): '$1'" + log "Action disable $1 requested for ${duration} by user $(logname || echo unknown). Comment: '$2'" # Log a warning if a check has no wrapper - for check in $(get_checks_list); do + if [ "$1" == "all" ]; then + checks=$(get_checks_list) + else + checks="$1" + fi + for check in ${checks}; do command=$(get_check_commands "${check}" | tail -n1) if ! echo "${command}" | grep --quiet --no-messages alerts_wrapper; then log "Warning: check '${check}' has no alerts_wrapper, it will not be disabled." fi done - #wrapper_names=$(get_check_commands "${check}" | tail -n1 | awk '{match($0, /.*--name\s+([^[:space:]]+)/, arr); print arr[1]}') - #for name in $(echo "${wrapper_names=}" | tr ',' '\n'); do - # log "Executing 'alerts_switch disable ${name}'" - # alerts_switch disable "${name}" - #done - #done + log "Executing 'alerts_switch disable $1 --during \"${duration}\"'" + alerts_switch disable "$1" --during "${duration}" - log "Executing 'alerts_switch disable all --during \"${duration}\"'" - alerts_switch disable all --during "${duration}" - - echo "All alerts are now disabled for ${duration}." + echo "$1 alerts are now disabled for ${duration}." } function enable { - # $1: comment + # $1: check name, $2: comment - log "Action enable requested by user $(logname || echo unknown): '${1}'" - log "Executing 'alerts_switch enable all'" - alerts_switch enable all + log "Action enable $1 requested by user $(logname || echo unknown). Comment: '$2'" + log "Executing 'alerts_switch enable $1'" + alerts_switch enable "$1" - echo "All alerts are now enabled." + echo "$1 alerts are now enabled." } -### status ACTION ########################## +### STATUS ACTION ########################## # Converts human writable duration into seconds function duration_to_seconds { @@ -348,28 +339,6 @@ function seconds_to_duration { echo "${delay_days}${delay_hours}${delay_minutes}${delay_seconds}" } -# Get from NRPE / alerts_wrapper options the maximum duration of disable. -# If different values are found for the same disable name, the lowest is keept. -function get_max_disable_duration { - min_of_max_duration="" - min_of_max_sec="" - for check in $(get_checks_list); do - cmd=$(get_check_commands "${check}" | tail -n1) - - max_duration=$(echo "${cmd}" | awk '{ for (i=1; i<=NF; i++) { if ($i ~ /(-m|--max|--maximum|--limit)[ =]/) print $(i+1) } }') - if [ -z "${max_duration}" ]; then - continue - fi - - max_sec=$(duration_to_seconds "${max_duration}") - if [ -z "${min_of_max_sec}" ] || [ "${max_sec}" -lt "${min_of_max_sec}" ]; then - min_of_max_sec="${max_sec}" - min_of_max_duration="${max_duration}" - fi - done - echo "${min_of_max_duration:-"1d"}" # 1d is alerts_wrapper default --max -} - function disabled_secs_left { disabled_file="${base_dir}/all_alerts_disabled" if [ ! -e "${disabled_file}" ]; then @@ -377,13 +346,7 @@ function disabled_secs_left { return fi - max_disable_duration="$(get_max_disable_duration)" - max_disable_secs="$(duration_to_seconds "${max_disable_duration}")" disable_secs="$(grep -v -E "^\s*#" "${disabled_file}" | grep -E "[0-9]+" | head -n1 | awk '{print$1}')" - - if [ "${disable_secs}" -gt "${max_disable_secs}" ]; then - disable_secs="${max_disable_secs}" - fi disable_date=$(date --date "${disable_secs} seconds ago" +"%s") last_change=$(stat -c %Z "${disabled_file}") @@ -391,7 +354,9 @@ function disabled_secs_left { } function alerts_status { - local disabled_secs_left=$(disabled_secs_left) + echo "Not implemented yet" + exit 1 + disabled_secs_left=$(disabled_secs_left) disabled_duration_left="$(seconds_to_duration "${disabled_secs_left}")" if [ -z "${disabled_duration_left}" ]; then @@ -449,25 +414,46 @@ while :; do usage_error "Option --during: defined multiple times." fi if [ "$#" -gt 1 ]; then - duration=$(filter_duration "$2") + if ! duration=$(filter_duration "$2"); then + exit 1 + fi default_duration="False" else usage_error "Option --during: missing value." fi shift; shift;; - check|enable|disable|status) + -c|--comment) + if [ "$#" -gt 1 ]; then + comment="$2" + else + usage_error "Option --comment: missing comment string." + fi + shift; shift;; + status) action="$1" shift;; - *) - if [ "${action}" = "check" ] && [ -n "$1" ]; then - if get_checks_list | grep --quiet -E "^$1$"; then - check_name=$1 - shift + check|enable|disable) + action="$1" + if [ "${action}" == "check" ]; then + names="$(get_checks_list)" + else + names="all $(get_wrappers_names)" + fi + if [ "$#" -gt 1 ]; then + if echo "${names}" | grep --quiet -E "^$2$"; then + check_name="$2" + shift; shift else - usage_error "Action check: unknown argument '$1'." + usage_error "Action ${action}: unknown check '$2'." fi else - # Other arguments are the comment + usage_error "Action ${action}: missing check argument." + fi + ;; + *) + if [ -n "$1" ]; then + usage_error "Action '${action}': unknown argument '$1'." + else break fi ;; @@ -475,53 +461,37 @@ while :; do done +if [ "$#" -gt 0 ]; then + usage_error "Too many arguments." +fi if [ -z "${action}" ]; then usage_error "Missing or invalid ACTION argument." fi -if [ "${action}" = "check" ]; then - if [ "$#" -gt 0 ]; then - usage_error "Action check: too many arguments." - fi - if [ "${default_duration}" = "False" ]; then - usage_error "Action check: there is no --during option." +if [[ "${action}" =~ ((en|dis)able) ]]; then + if [ -z "${comment}" ]; then + usage_error "Action ${action}: missing --comment argument." fi +fi - check "$check_name" +if [ ! "${action}" == "disable" ]; then + if [ "${default_duration}" = "False" ]; then + usage_error "Action ${action}: there is no --during option." + fi +fi + +if [ "${action}" = "check" ]; then + check "${check_name}" elif [ "${action}" = "enable" ]; then - if [ "$#" = 0 ]; then - usage_error "Action enable: missing COMMENT argument." - fi - if [ "$#" -gt 1 ]; then - usage_error "Action enable: too many arguments." - fi - if [ "${default_duration}" = "False" ]; then - usage_error "Action enable: there is no --during option." - fi - - comment="$1" - enable "${comment}" + enable "${check_name}" "${comment}" elif [ "${action}" = "disable" ]; then - if [ "$#" = 0 ]; then - usage_error "Action disable: missing COMMENT argument." - fi - if [ "$#" -gt 1 ]; then - usage_error "Action disable: too many arguments." - fi - is_nrpe_wrapped - - comment="$1" - disable_alerts "${comment}" + disable_alerts "${check_name}" "${comment}" elif [ "${action}" = "status" ]; then - if [ "$#" -gt 0 ]; then - usage_error "Action status: too many arguments." - fi - alerts_status fi diff --git a/nagios-nrpe/files/monitoringctl_completion b/nagios-nrpe/files/monitoringctl_completion old mode 100755 new mode 100644 index 8104ce45..195cf243 --- a/nagios-nrpe/files/monitoringctl_completion +++ b/nagios-nrpe/files/monitoringctl_completion @@ -1,25 +1,81 @@ -#!/usr/bin/env bash +#!/usr/bin/bash +# -# List of available checks -_monitoringctl_completion() { - local cur=${COMP_WORDS[COMP_CWORD]}; - - if [ "${#COMP_WORDS[@]}" == "1" ]; then - COMPREPLY=($(compgen -W "check status enable disable" "${cur}")) - elif [ "${COMP_WORDS[@]}" =~ (check|enable|disable) ]; then - - fi - - COMPREPLY=( $( compgen -W '$(grep "\[check_" -Rs /etc/nagios/ | grep -vE "^[[:blank:]]*#" | awk -F"[\\\[\\\]=]" "{print \$2}" | sed "s/check_//" | sort | uniq)' -- "${cur}" ) ); +_get_wrappers_names() { + grep "alerts_wrapper" -Rs /etc/nagios/ | grep -v -E "^\s*#" | awk '{ for (i=1 ; i<=NF; i++) { if ($i ~ /^(-n|--name)$/) { print $(i+1); break } } }' | tr ',' '\n' | sort | uniq } -# List of available checks -_disable_names_list_completion() { +_monitoringctl_completion() { local cur=${COMP_WORDS[COMP_CWORD]}; - command="echo \"all\"; echo \$(grep \"alerts_wrapper\" -Rs /etc/nagios/ | awk '!/^\s*#/ { for (i=1; i<=NF; i++) if (\$i ~ /--name[s]?/) { sub(\",\", \"\n\", \$(i+1)); print \$(i+1) } }' | sort | uniq)" - COMPREPLY=( $( compgen -W '$($command)' -- "$cur" ) ); + local prev=${COMP_WORDS[COMP_CWORD-1]}; -complete -F _monitoringctl_completion monitoringctl -#complete -F _disable_names_list_completion alerts_switch + local action="" + for w in "${COMP_WORDS[@]}"; do + case "$w" in + status|check|enable|disable) + action="${w}" + ;; + esac + done + local words="--help" + case "${action}" in + status) + words="${words}" + ;; + check) + local checks="$(_get_wrappers_names)" + local check="" + for w in "${COMP_WORDS[@]}"; do + for c in ${checks}; do + if [ "${c}" == "${w}" ]; then + check="${w}" + break + fi + done + done + if [ -z "${check}" ]; then + words="--bypass-nrpe ${checks} ${words}" + else + words="--bypass-nrpe ${words}" + fi + ;; + enable) + if [ "${prev}" == "enable" ]; then + words="all $(_get_wrappers_names)" + else + words="--comment ${words}" + fi + ;; + disable) + if [ "${prev}" == "disable" ]; then + words="all $(_get_wrappers_names)" + elif [ "${prev}" == "-d" ] || [ "${prev}" == "--during" ]; then + words="1d 1d12h 1h 1h30m 1m 1m30s 30s" + else + words="--during --comment ${words}" + fi + ;; + *) + words="status check enable disable ${words}" + ;; + esac + + # Avoid double + opts=(); + for i in ${words}; do + for j in "${COMP_WORDS[@]}"; do + if [[ "$i" == "$j" ]]; then + continue 2 + fi + done + opts+=("$i") + done + + COMPREPLY=($(compgen -W "${opts[*]}" -- "${cur}")) + return 0 + +} + +complete -F _monitoringctl_completion monitoringctl.sh diff --git a/nagios-nrpe/files/monitoringctl_completion_dev b/nagios-nrpe/files/monitoringctl_completion_dev deleted file mode 100644 index 35cc3ee6..00000000 --- a/nagios-nrpe/files/monitoringctl_completion_dev +++ /dev/null @@ -1,95 +0,0 @@ -#!/usr/bin/bash -# -_monitoringctl_completion() { - local cur=${COMP_WORDS[COMP_CWORD]}; - local prev=${COMP_WORDS[COMP_CWORD-1]}; - - local action="" - for w in "${COMP_WORDS[@]}"; do - if [[ "${w}" =~ (status|check|enable|disable) ]]; then - action="${w}" - break - fi - done - - - local words="--help --verbose" - case "${action}" in - status) - words="${words}" - ;; - check) - local checks="load disk1 https ssl" - local check="" - for w in "${COMP_WORDS[@]}"; do - for c in ${checks}; do - if [ "${c}" == "${w}" ]; then - check="${w}" - break - fi - done - done - if [ -z "${check}" ]; then - words="--bypass-nrpe ${checks} ${words}" - else - words="--bypass-nrpe ${words}" - fi - ;; - enable) - words="${words}" - ;; - disable) - if [[ "${prev}" =~ (-d|--during) ]]; then - local last_char="${cur: -1}" - if [ -z "${cur}" ]; then - COMPREPLY=("1d" "1d12h" "1h" "1h30m" "1m" "1m30s" "30s") - else - if [[ "${last_char}" == "s" ]]; then - COMPREPLY=() - elif [[ "${last_char}" == "m" ]]; then - COMPREPLY=("${cur}30s") - elif [[ "${last_char}" == "h" ]]; then - COMPREPLY=("${cur}15m" "${cur}30m" "${cur}45m") - elif [[ "${last_char}" == "d" ]]; then - COMPREPLY=("${cur}6h" "${cur}12h" "${cur}18h") - elif [[ "${last_char}" =~ [0-9] ]]; then - if [[ "${cur}" =~ s ]]; then - COMPREPLY=() - elif [[ "${cur}" =~ m ]]; then - COMPREPLY=("${cur}s") - elif [[ "${cur}" =~ h ]]; then - COMPREPLY=("${cur}m") - elif [[ "${cur}" =~ d ]]; then - COMPREPLY=("${cur}h") - else - COMPREPLY=("${cur}d" "${cur}h" "${cur}m" "${cur}s") - fi - fi - fi - return 0 - else - words="--during ${words}" - fi - ;; - *) - words="status check enable disable ${words}" - ;; - esac - - opts=(); - for i in ${words}; do - for j in "${COMP_WORDS[@]}"; do - if [[ "$i" == "$j" ]]; then - continue 2 - fi - done - opts+=("$i") - done - - COMPREPLY=($(compgen -W "${opts[*]}" -- "${cur}")) - return 0 - -} - -complete -F _monitoringctl_completion ttt -