debug, amélioration complétion
All checks were successful
Ansible Lint |Total|New|Outstanding|Fixed|Trend |:-:|:-:|:-:|:-:|:-: |2688|4|2684|4|:-1: Reference build: <a href="https://jenkins.evolix.org/job/gitea/job/ansible-roles/job/monitoringctl/7//ansiblelint">Evolix » ansible-roles » monitoringctl #7</a>
gitea/ansible-roles/pipeline/head This commit looks good

This commit is contained in:
William Hirigoyen 2024-02-27 18:04:20 +01:00
parent ff007b9543
commit 6aab5f1aa2
5 changed files with 214 additions and 269 deletions

View file

@ -4,8 +4,6 @@
# https://gitea.evolix.org/evolix/ansible-roles/src/branch/stable/nagios-nrpe # https://gitea.evolix.org/evolix/ansible-roles/src/branch/stable/nagios-nrpe
# #
set -e
readonly PROGNAME=$(basename $0) readonly PROGNAME=$(basename $0)
readonly base_dir="/var/lib/monitoringctl" readonly base_dir="/var/lib/monitoringctl"
readonly log_file="/var/log/monitoringctl.log" readonly log_file="/var/log/monitoringctl.log"
@ -41,28 +39,32 @@ time_to_seconds() {
# $1: time in string format # $1: time in string format
if echo "${1}" | grep -E -q '^([0-9]+[wdhms])+$'; then 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 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 elif echo "${1}" | grep -E -q '^([0-9]+h[0-9]+$)'; then
echo "${1} * 3600" | xargs expr 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 else
error "Invalid duration: '${1}'." error "Invalid duration: '${1}'."
fi fi
} }
get_disable_names() { 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 () { disable_alerts () {
# $1: check name # $1: check name, $2: duration_sec
disable_file_path="${base_dir}/${check_name}_alerts_disabled" now_secs=$(date +"%s")
echo "${duration_sec}" > "${disable_file_path}" 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}" chmod 0644 "${disable_file_path}"
log_disable "$1" log_disable "$1"
} }
enable_alerts () { enable_alerts () {
# $1: check name # $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 if [ -e "${disable_file_path}" ]; then
rm "${disable_file_path}" rm "${disable_file_path}"
fi fi
@ -85,18 +87,25 @@ log_enable () {
main () { main () {
mkdir -p "${base_dir}" install --mode=0755 --directory "${base_dir}"
#TODO
if all
for check in $(get_disable_names); do
done
if [ "${action}" == 'enable' ]; then 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 elif [ "${action}" == 'disable' ]; then
duration_sec=$(time_to_seconds "${duration}") 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 elif [ "${action}" == 'help' ]; then
usage usage
fi fi
@ -116,20 +125,22 @@ while :; do
fi fi
shift; shift;; shift; shift;;
*) *)
if [ -z "${action}" ]; then if [ -n "$1" ]; then
error "Missing action argument." get_disable_names | grep --quiet -E "^$1$"
elif [ -n "${check_name}" ]; then arg_is_in_disable_names_rc=$?
error "Unknown argument '$1'." if [ "${arg_is_in_disable_names_rc}" -eq 0 ] || [ "$1" == "all" ]; then
elif [ -z "$1" ]; then check_name="$1"
break else
fi error "Unknown argument '$1', or NAME not defined in NRPE configuration."
get_disable_names | grep --quiet -E "^$1$" fi
arg_is_in_disable_names_rc=$?
if [ "${arg_is_in_disable_names_rc}" -eq 0 ] || [ "$1" == "all" ]; then
check_name="$1"
else 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 fi
shift;; shift;;
esac esac
done done

View file

@ -55,7 +55,7 @@ time_to_seconds() {
delay_from_disable_file() { delay_from_disable_file() {
# $1: disabled file containing the re-enable time in sec # $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 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 "$1") file_last_change_secs=$(stat -c %Z "$1")
@ -71,19 +71,19 @@ delay_to_string() {
#$1 delay in secs #$1 delay in secs
delay_days="$(( delay /86400 ))" delay_days="$(( delay /86400 ))"
if [ "${delay_days}" -eq 0 ]; then delay_days="" 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 ))" delay_hours="$(( (delay %86400) /3600 ))"
if [ "${delay_hours}" -eq 0 ]; then delay_hours="" 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 ))" delay_minutes="$(( ((delay %86400) %3600) /60 ))"
if [ "${delay_minutes}" -eq 0 ]; then delay_minutes="" 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 ))" delay_seconds="$(( ((delay %86400) %3600) %60 ))"
if [ "${delay_seconds}" -eq 0 ]; then delay_seconds="" 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}" echo "${delay_days}${delay_hours}${delay_minutes}${delay_seconds}"
} }
@ -120,7 +120,7 @@ main() {
timeout_command="timeout 9" timeout_command="timeout 9"
fi fi
check_stdout=$(${timeout_command} "${check_command}") check_stdout=$(${timeout_command} ${check_command})
check_rc=$? check_rc=$?
if [ "${is_disabled}" == "True" ] && [ "${check_rc}" -eq 124 ] && [ -z "${check_stdout}" ]; then if [ "${is_disabled}" == "True" ] && [ "${check_rc}" -eq 124 ] && [ -z "${check_stdout}" ]; then
@ -130,11 +130,11 @@ main() {
if [ "${is_disabled}" == "True" ]; then if [ "${is_disabled}" == "True" ]; then
delay=$(delay_from_disable_file "${disable_file}") delay=$(delay_from_disable_file "${disable_file}")
delay_str="$(delay_to_string "${delay}")" 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 fi
echo "${check_stdout}"
if [ "${is_disabled}" == "True" ]; then if [ "${is_disabled}" == "True" ]; then
if [ ${check_rc} = 0 ]; then if [ ${check_rc} = 0 ]; then
exit 0 # Nagios OK exit 0 # Nagios OK
@ -179,7 +179,6 @@ if [[ "${1}" =~ -.* ]]; then
printf 'ERROR: "--name" requires a non-empty option argument.\n' >&2 printf 'ERROR: "--name" requires a non-empty option argument.\n' >&2
exit 1 exit 1
;; ;;
--) --)
# End of all options. # End of all options.
shift shift
@ -210,11 +209,15 @@ fi
if [ -z "${check_name}" ]; then if [ -z "${check_name}" ]; then
printf 'ERROR: You must specify a check name, with --names.\n' >&2 printf 'ERROR: You must specify a check name, with --names.\n' >&2
exit 1 exit 2
fi fi
if [ -z "${check_command}" ]; then if [ -z "${check_command}" ]; then
printf 'ERROR: You must specify a command to execute.\n' >&2 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 fi
readonly check_name readonly check_name

View file

@ -20,7 +20,6 @@ GENERAL OPTIONS:
-h, --help Print this message and exit. -h, --help Print this message and exit.
-V, --version Print version number and exit. -V, --version Print version number and exit.
# -v, --verbose Print more informations.
ACTIONS: ACTIONS:
@ -28,40 +27,30 @@ ACTIONS:
Ask CHECK_NAME status to NRPE as an HTTP request. Ask CHECK_NAME status to NRPE as an HTTP request.
Indicates which command NRPE has supposedly run (from its configuration). Indicates which command NRPE has supposedly run (from its configuration).
-b, --bypass-nrpe Execute directly command from NRPE configuration,
Options: without passing the request to NRPE.
-b, --bypass-nrpe Execute directly command from NRPE configuration,
without requesting to NRPE.
status status
Print : Print whether alerts are enabled or not (silenced).
- Wether 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):
- Comment.
- Time left before automatic re-enable.
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. Checks output is still printed, so alerts history won't be lost.
Options: enable CHECK_NAME|all --comment 'COMMENT'
-d, --during DURATION Specify disable duration (default: 1h).
enable 'COMMENT'
Re-enable all alerts (only global for now) Re-enable all alerts (only global for now)
COMMENT: COMMENT:
(mandatory) Comment (string) to be written in log. Comment string to be written in log (mandatory).
DURATION: 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: Format:
You can use 'd' (day), 'h' (hour) and 'm' (minute) , or a combination of them, to specify a duration. 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 { function log {
# $1: message # $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- 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 ########################## ### CHECK ACTION ##########################
@ -164,10 +157,10 @@ function check {
exit 1 exit 1
fi 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 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 if [ -z "${server_port}" ]; then server_port="5666"; fi
check_commands=$(get_check_commands "$1") check_commands=$(get_check_commands "$1")
@ -220,7 +213,7 @@ function check {
function filter_duration { function filter_duration {
# Format (in brief): XdYhZm # Format (in brief): XdYhZm
# Minutes unit 'm' is not mandatory after Xh # 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 if [[ "$1" =~ ${time_regex} ]]; then
echo "$1" echo "$1"
@ -243,7 +236,7 @@ function is_nrpe_wrapped {
} }
function disable_alerts { function disable_alerts {
# $1: comment # $1: check name, $2: comment
if ! command -v alerts_switch &> /dev/null; then if ! command -v alerts_switch &> /dev/null; then
>&2 echo "Error: script 'alerts_switch' is not installed." >&2 echo "Error: script 'alerts_switch' is not installed."
@ -267,7 +260,7 @@ function disable_alerts {
Alerts will be disabled for ${duration}${default_msg} Alerts will be disabled for ${duration}${default_msg}
Our monitoring system will continue to gather checks outputs, so alerts history won't be lost. 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): To re-enable alerts before ${duration}, execute (as root or with sudo):
monitoringctl enable monitoringctl enable $1 --comment 'YOUR REASON'
EOF EOF
echo -n "Confirm (y/N)? " echo -n "Confirm (y/N)? "
read -r answer read -r answer
@ -276,41 +269,39 @@ EOF
exit 0 exit 0
fi 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 # 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) command=$(get_check_commands "${check}" | tail -n1)
if ! echo "${command}" | grep --quiet --no-messages alerts_wrapper; then if ! echo "${command}" | grep --quiet --no-messages alerts_wrapper; then
log "Warning: check '${check}' has no alerts_wrapper, it will not be disabled." log "Warning: check '${check}' has no alerts_wrapper, it will not be disabled."
fi fi
done done
#wrapper_names=$(get_check_commands "${check}" | tail -n1 | awk '{match($0, /.*--name\s+([^[:space:]]+)/, arr); print arr[1]}') log "Executing 'alerts_switch disable $1 --during \"${duration}\"'"
#for name in $(echo "${wrapper_names=}" | tr ',' '\n'); do alerts_switch disable "$1" --during "${duration}"
# log "Executing 'alerts_switch disable ${name}'"
# alerts_switch disable "${name}"
#done
#done
log "Executing 'alerts_switch disable all --during \"${duration}\"'" echo "$1 alerts are now disabled for ${duration}."
alerts_switch disable all --during "${duration}"
echo "All alerts are now disabled for ${duration}."
} }
function enable { function enable {
# $1: comment # $1: check name, $2: comment
log "Action enable requested by user $(logname || echo unknown): '${1}'" log "Action enable $1 requested by user $(logname || echo unknown). Comment: '$2'"
log "Executing 'alerts_switch enable all'" log "Executing 'alerts_switch enable $1'"
alerts_switch enable all 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 # Converts human writable duration into seconds
function duration_to_seconds { function duration_to_seconds {
@ -348,28 +339,6 @@ function seconds_to_duration {
echo "${delay_days}${delay_hours}${delay_minutes}${delay_seconds}" 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 { function disabled_secs_left {
disabled_file="${base_dir}/all_alerts_disabled" disabled_file="${base_dir}/all_alerts_disabled"
if [ ! -e "${disabled_file}" ]; then if [ ! -e "${disabled_file}" ]; then
@ -377,13 +346,7 @@ function disabled_secs_left {
return return
fi 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}')" 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") disable_date=$(date --date "${disable_secs} seconds ago" +"%s")
last_change=$(stat -c %Z "${disabled_file}") last_change=$(stat -c %Z "${disabled_file}")
@ -391,7 +354,9 @@ function disabled_secs_left {
} }
function alerts_status { 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}")" disabled_duration_left="$(seconds_to_duration "${disabled_secs_left}")"
if [ -z "${disabled_duration_left}" ]; then if [ -z "${disabled_duration_left}" ]; then
@ -449,25 +414,46 @@ while :; do
usage_error "Option --during: defined multiple times." usage_error "Option --during: defined multiple times."
fi fi
if [ "$#" -gt 1 ]; then if [ "$#" -gt 1 ]; then
duration=$(filter_duration "$2") if ! duration=$(filter_duration "$2"); then
exit 1
fi
default_duration="False" default_duration="False"
else else
usage_error "Option --during: missing value." usage_error "Option --during: missing value."
fi fi
shift; shift;; 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" action="$1"
shift;; shift;;
*) check|enable|disable)
if [ "${action}" = "check" ] && [ -n "$1" ]; then action="$1"
if get_checks_list | grep --quiet -E "^$1$"; then if [ "${action}" == "check" ]; then
check_name=$1 names="$(get_checks_list)"
shift 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 else
usage_error "Action check: unknown argument '$1'." usage_error "Action ${action}: unknown check '$2'."
fi fi
else 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 break
fi fi
;; ;;
@ -475,53 +461,37 @@ while :; do
done done
if [ "$#" -gt 0 ]; then
usage_error "Too many arguments."
fi
if [ -z "${action}" ]; then if [ -z "${action}" ]; then
usage_error "Missing or invalid ACTION argument." usage_error "Missing or invalid ACTION argument."
fi fi
if [ "${action}" = "check" ]; then if [[ "${action}" =~ ((en|dis)able) ]]; then
if [ "$#" -gt 0 ]; then if [ -z "${comment}" ]; then
usage_error "Action check: too many arguments." usage_error "Action ${action}: missing --comment argument."
fi
if [ "${default_duration}" = "False" ]; then
usage_error "Action check: there is no --during option."
fi 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 elif [ "${action}" = "enable" ]; then
if [ "$#" = 0 ]; then enable "${check_name}" "${comment}"
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}"
elif [ "${action}" = "disable" ]; then 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 is_nrpe_wrapped
disable_alerts "${check_name}" "${comment}"
comment="$1"
disable_alerts "${comment}"
elif [ "${action}" = "status" ]; then elif [ "${action}" = "status" ]; then
if [ "$#" -gt 0 ]; then
usage_error "Action status: too many arguments."
fi
alerts_status alerts_status
fi fi

92
nagios-nrpe/files/monitoringctl_completion Executable file → Normal file
View file

@ -1,25 +1,81 @@
#!/usr/bin/env bash #!/usr/bin/bash
#
# List of available checks _get_wrappers_names() {
_monitoringctl_completion() { 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
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 _monitoringctl_completion() {
_disable_names_list_completion() {
local cur=${COMP_WORDS[COMP_CWORD]}; 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)" local prev=${COMP_WORDS[COMP_CWORD-1]};
COMPREPLY=( $( compgen -W '$($command)' -- "$cur" ) );
complete -F _monitoringctl_completion monitoringctl local action=""
#complete -F _disable_names_list_completion alerts_switch 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

View file

@ -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