Fin de créneau (code non utilisable)
All checks were successful
Ansible Lint |Total|New|Outstanding|Fixed|Trend |:-:|:-:|:-:|:-:|:-: |2688|7|2681|6|:-1: Reference build: <a href="https://jenkins.evolix.org/job/gitea/job/ansible-roles/job/monitoringctl/6//ansiblelint">Evolix » ansible-roles » monitoringctl #6</a>
gitea/ansible-roles/pipeline/head This commit looks good

This commit is contained in:
William Hirigoyen 2024-02-16 11:58:19 +01:00
parent c37880626c
commit ff007b9543
5 changed files with 312 additions and 186 deletions

View file

@ -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 <DURATION>] <NAME>"
echo " $PROGNAME enable <NAME>"
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 <<END
$PROGNAME disables or enables NRPE alerts wrapped by the script 'alerts_wrapper' in NRPE configuration.
Usage: $PROGNAME disable [-d|--during <DURATION>] <CHECK_NAME|ALL>
$PROGNAME enable <CHECK_NAME|ALL>
$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

View file

@ -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 <<END
@ -27,28 +26,24 @@ and you are welcome to redistribute it under certain conditions.
See the GNU General Public License v3.0 for details.
END
}
show_help() {
cat <<END
alerts_wrapper wraps an NRPE command and overrides the return code.
Usage: alerts_wrapper [--maximum 1d] [--names check,other_disable_name,...]] <check command with optional arguments>
Usage: alerts_wrapper disable_name <check command with optional arguments>
Usage: alerts_wrapper --name <CHECK_NAME> <CHECK_COMMAND>
Usage: alerts_wrapper <CHECK_NAME> <CHECK_COMMAND> (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

View file

@ -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 <<EOF
monitoringctl version ${VERSION}.
monitoringctl gives some control over NRPE checks and alerts.
@ -16,7 +19,8 @@ Usage: monitoringctl [OPTIONS] ACTION ARGUMENTS
GENERAL OPTIONS:
-h, --help Print this message and exit.
-v, --verbose Print more informations.
-V, --version Print version number and exit.
# -v, --verbose Print more informations.
ACTIONS:
@ -30,7 +34,7 @@ ACTIONS:
-b, --bypass-nrpe Execute directly command from NRPE configuration,
without requesting to NRPE.
alerts-status
status
Print :
- Wether alerts are enabled or not (silenced).
@ -38,16 +42,16 @@ ACTIONS:
- Comment.
- Time left before automatic re-enable.
disable-alerts [--duration DURATION] 'COMMENT'
disable [--during DURATION] 'COMMENT'
Disable (silence) all alerts (only global for now) for DURATION and write COMMENT into the log.
Checks output is still printed, so alerts history won't be lost.
Options:
-d, --duration DURATION Specify disable-alerts duration (default: 1h).
-d, --during DURATION Specify disable duration (default: 1h).
enable-alerts 'COMMENT'
enable 'COMMENT'
Re-enable all alerts (only global for now)
@ -57,7 +61,7 @@ COMMENT:
DURATION:
(optional, default: "1h") Duration time (string) during which alerts will be disabled (silenced).
(optional, default: "1h") Time (string) during which alerts will be disabled (silenced).
Format:
You can use 'd' (day), 'h' (hour) and 'm' (minute) , or a combination of them, to specify a duration.
@ -68,6 +72,10 @@ Log path: ${log_path}
EOF
}
function show_version {
echo "monitoringctl version ${VERSION}."
}
function usage_error {
>&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 <<EOF
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-alerts
monitoringctl enable
EOF
echo -n "Confirm (y/N)? "
read -r answer
@ -269,7 +276,7 @@ EOF
exit 0
fi
log "Action disable-alerts requested for ${duration} by user $(logname || echo unknown): '$1'"
log "Action disable requested for ${duration} by user $(logname || echo unknown): '$1'"
# Log a warning if a check has no wrapper
for check in $(get_checks_list); do
@ -286,23 +293,24 @@ EOF
#done
#done
log "Executing 'alerts_switch disable all'"
alerts_switch disable all --duration "${duration}"
log "Executing 'alerts_switch disable all --during \"${duration}\"'"
alerts_switch disable all --during "${duration}"
echo "All alerts are now disabled for ${duration}."
}
function enable-alerts {
function enable {
# $1: comment
log "Action enable-alerts requested by user $(logname || echo unknown): '${1}'"
log "Action enable requested by user $(logname || echo unknown): '${1}'"
log "Executing 'alerts_switch enable all'"
alerts_switch enable all
echo "All alerts are now enabled."
}
### ALERTS-STATUS ACTION ##########################
### status ACTION ##########################
# Converts human writable duration into seconds
function duration_to_seconds {
@ -427,24 +435,27 @@ while :; do
-h|-\?|--help)
show_help
exit 0;;
-V|--version)
show_version
exit 0;;
-v|--verbose)
verbose="True"
shift;;
-b|--bypass-nrpe)
bypass_nrpe="True"
shift;;
-d|--duration)
-d|--during)
if [ "${default_duration}" = "False" ]; then
usage_error "Option --duration: defined multiple times."
usage_error "Option --during: defined multiple times."
fi
if [ "$#" -gt 1 ]; then
duration=$(filter_duration "$2")
default_duration="False"
else
usage_error "Option --duration: missing value."
usage_error "Option --during: missing value."
fi
shift; shift;;
check|enable-alerts|disable-alerts|alerts-status)
check|enable|disable|status)
action="$1"
shift;;
*)
@ -474,31 +485,31 @@ if [ "${action}" = "check" ]; then
usage_error "Action check: too many arguments."
fi
if [ "${default_duration}" = "False" ]; then
usage_error "Action check: there is no --duration option."
usage_error "Action check: there is no --during option."
fi
check "$check_name"
elif [ "${action}" = "enable-alerts" ]; then
elif [ "${action}" = "enable" ]; then
if [ "$#" = 0 ]; then
usage_error "Action enable-alerts: missing COMMENT argument."
usage_error "Action enable: missing COMMENT argument."
fi
if [ "$#" -gt 1 ]; then
usage_error "Action enable-alerts: too many arguments."
usage_error "Action enable: too many arguments."
fi
if [ "${default_duration}" = "False" ]; then
usage_error "Action enable-alerts: there is no --duration option."
usage_error "Action enable: there is no --during option."
fi
comment="$1"
enable-alerts "${comment}"
enable "${comment}"
elif [ "${action}" = "disable-alerts" ]; then
elif [ "${action}" = "disable" ]; then
if [ "$#" = 0 ]; then
usage_error "Action disable-alerts: missing COMMENT argument."
usage_error "Action disable: missing COMMENT argument."
fi
if [ "$#" -gt 1 ]; then
usage_error "Action disable-alerts: too many arguments."
usage_error "Action disable: too many arguments."
fi
is_nrpe_wrapped
@ -506,9 +517,9 @@ elif [ "${action}" = "disable-alerts" ]; then
comment="$1"
disable_alerts "${comment}"
elif [ "${action}" = "alerts-status" ]; then
elif [ "${action}" = "status" ]; then
if [ "$#" -gt 0 ]; then
usage_error "Action alerts-status: too many arguments."
usage_error "Action status: too many arguments."
fi
alerts_status

View file

@ -0,0 +1,25 @@
#!/usr/bin/env 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}" ) );
}
# List of available checks
_disable_names_list_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" ) );
complete -F _monitoringctl_completion monitoringctl
#complete -F _disable_names_list_completion alerts_switch

View file

@ -0,0 +1,95 @@
#!/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