Fin de créneau (code non utilisable)
This commit is contained in:
parent
8230fbbbc5
commit
7edd8ac5b8
|
@ -14,44 +14,59 @@ readonly log_file="/var/log/monitoringctl.log"
|
||||||
duration="1h"
|
duration="1h"
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
echo "$PROGNAME disables or enables NRPE alerts wrapped by the script 'alerts_wrapper' in NRPE configuration."
|
cat <<END
|
||||||
echo "Usage: $PROGNAME disable [-d|--duration <DURATION>] <NAME>"
|
$PROGNAME disables or enables NRPE alerts wrapped by the script 'alerts_wrapper' in NRPE configuration.
|
||||||
echo " $PROGNAME enable <NAME>"
|
|
||||||
echo " $PROGNAME help"
|
Usage: $PROGNAME disable [-d|--during <DURATION>] <CHECK_NAME|ALL>
|
||||||
echo "NAME: one of the names given to '--names' option of 'alerts_wrapper'."
|
$PROGNAME enable <CHECK_NAME|ALL>
|
||||||
echo "DURATION: Duration of alert disabling."
|
$PROGNAME help
|
||||||
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"
|
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
|
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]+$)'; then
|
||||||
echo "${1} * 3600" | xargs expr
|
echo "${1} * 3600" | xargs expr
|
||||||
else
|
else
|
||||||
>&2 echo "Invalid duration: '${1}'."
|
error "Invalid duration: '${1}'."
|
||||||
usage
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
get_disable_names() {
|
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 () {
|
disable_alerts () {
|
||||||
|
# $1: check name
|
||||||
|
disable_file_path="${base_dir}/${check_name}_alerts_disabled"
|
||||||
echo "${duration_sec}" > "${disable_file_path}"
|
echo "${duration_sec}" > "${disable_file_path}"
|
||||||
chmod 0644 "${disable_file_path}"
|
chmod 0644 "${disable_file_path}"
|
||||||
log_disable
|
log_disable "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
enable_alerts () {
|
enable_alerts () {
|
||||||
|
# $1: check name
|
||||||
|
disable_file_path="${base_dir}/${check_name}_alerts_disabled"
|
||||||
if [ -e "${disable_file_path}" ]; then
|
if [ -e "${disable_file_path}" ]; then
|
||||||
rm "${disable_file_path}"
|
rm "${disable_file_path}"
|
||||||
fi
|
fi
|
||||||
log_enable
|
log_enable "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
now () {
|
now () {
|
||||||
|
@ -59,24 +74,29 @@ now () {
|
||||||
}
|
}
|
||||||
|
|
||||||
log_disable () {
|
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 () {
|
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 () {
|
main () {
|
||||||
disable_file_path="${base_dir}/${disable_name}_alerts_disabled"
|
|
||||||
|
|
||||||
mkdir -p "${base_dir}"
|
mkdir -p "${base_dir}"
|
||||||
|
|
||||||
|
#TODO
|
||||||
|
if all
|
||||||
|
for check in $(get_disable_names); do
|
||||||
|
done
|
||||||
|
|
||||||
if [ "${action}" == 'enable' ]; then
|
if [ "${action}" == 'enable' ]; then
|
||||||
enable_alerts
|
enable_alerts "${check_name}"
|
||||||
elif [ "${action}" == 'disable' ]; then
|
elif [ "${action}" == 'disable' ]; then
|
||||||
duration_sec=$(time_in_seconds "${duration}")
|
duration_sec=$(time_to_seconds "${duration}")
|
||||||
disable_alerts
|
disable_alerts "${check_name}"
|
||||||
elif [ "${action}" == 'help' ]; then
|
elif [ "${action}" == 'help' ]; then
|
||||||
usage
|
usage
|
||||||
fi
|
fi
|
||||||
|
@ -88,42 +108,39 @@ while :; do
|
||||||
enable|disable|help)
|
enable|disable|help)
|
||||||
action="$1"
|
action="$1"
|
||||||
shift;;
|
shift;;
|
||||||
-d|--duration)
|
-d|--during)
|
||||||
if [ "$#" -gt 1 ]; then
|
if [ "$#" -gt 1 ]; then
|
||||||
duration="$2"
|
duration="$2"
|
||||||
else
|
else
|
||||||
>&2 echo "Missing --duration argument."
|
error "Missing --during argument."
|
||||||
usage
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
shift; shift;;
|
shift; shift;;
|
||||||
*)
|
*)
|
||||||
if [ -z "${action}" ]; then
|
if [ -z "${action}" ]; then
|
||||||
>&2 echo "Missing action argument."
|
error "Missing action argument."
|
||||||
usage
|
elif [ -n "${check_name}" ]; then
|
||||||
exit 1
|
error "Unknown argument '$1'."
|
||||||
fi
|
elif [ -z "$1" ]; then
|
||||||
if [ -z "$1" ]; then
|
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
get_disable_names | grep --quiet -E "^$1$"
|
get_disable_names | grep --quiet -E "^$1$"
|
||||||
arg_is_in_disable_names_rc=$?
|
arg_is_in_disable_names_rc=$?
|
||||||
if [ "${arg_is_in_disable_names_rc}" -eq 0 ] && [ -z "${disable_name}" ]; then
|
if [ "${arg_is_in_disable_names_rc}" -eq 0 ] || [ "$1" == "all" ]; then
|
||||||
disable_name="$1"
|
check_name="$1"
|
||||||
else
|
else
|
||||||
>&2 echo "Unknown argument '$1', or NAME not defined in NRPE configuration."
|
error "Unknown argument '$1', or NAME not defined in NRPE configuration."
|
||||||
usage
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
shift;;
|
shift;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ -z "${disable_name}" ] && [ "${action}" != 'help' ] ; then
|
if [ -z "${check_name}" ] && [ "${action}" != 'help' ] ; then
|
||||||
>&2 echo "Missing NAME argument."
|
error "Missing CHECK_NAME argument."
|
||||||
usage
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
readonly check_name
|
||||||
|
readonly duration
|
||||||
|
readonly action
|
||||||
|
|
||||||
main
|
main
|
||||||
|
|
||||||
|
|
|
@ -4,15 +4,14 @@
|
||||||
# https://gitea.evolix.org/evolix/ansible-roles/src/branch/stable/nagios-nrpe
|
# https://gitea.evolix.org/evolix/ansible-roles/src/branch/stable/nagios-nrpe
|
||||||
#
|
#
|
||||||
|
|
||||||
VERSION="21.04"
|
readonly VERSION="21.04"
|
||||||
readonly VERSION
|
|
||||||
|
# Location of disable files
|
||||||
readonly base_dir="/var/lib/monitoringctl"
|
readonly base_dir="/var/lib/monitoringctl"
|
||||||
|
|
||||||
# Default maximum allowed disable time
|
# If no datetime is found in file, this value is used
|
||||||
disable_max_time_default="1d"
|
readonly default_disabled_time="1d"
|
||||||
readonly disable_max_time_default
|
|
||||||
|
|
||||||
# base functions
|
|
||||||
|
|
||||||
show_version() {
|
show_version() {
|
||||||
cat <<END
|
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.
|
See the GNU General Public License v3.0 for details.
|
||||||
END
|
END
|
||||||
}
|
}
|
||||||
|
|
||||||
show_help() {
|
show_help() {
|
||||||
cat <<END
|
cat <<END
|
||||||
alerts_wrapper wraps an NRPE command and overrides the return code.
|
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 --name <CHECK_NAME> <CHECK_COMMAND>
|
||||||
Usage: alerts_wrapper disable_name <check command with optional arguments>
|
Usage: alerts_wrapper <CHECK_NAME> <CHECK_COMMAND> (deprecated)
|
||||||
|
|
||||||
Options
|
Options
|
||||||
-m|--max|--maximum Max age of the disable file.
|
--name Check name (like load, disk1…)
|
||||||
Can be "1d" for 1 day, "5m" for 5 minutes…
|
Special name: 'all' is already hard-coded.
|
||||||
or more complex expressions like "1w2d10m42s"
|
-h, --help Print this message and exit.
|
||||||
If no time unit is provided, hour is assumed.
|
-V, --version Print version and exit.
|
||||||
--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.
|
|
||||||
END
|
END
|
||||||
}
|
}
|
||||||
|
|
||||||
time_in_seconds() {
|
time_to_seconds() {
|
||||||
|
# $1: formated time string
|
||||||
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]+$)'; then
|
||||||
|
@ -59,23 +54,57 @@ time_in_seconds() {
|
||||||
}
|
}
|
||||||
|
|
||||||
delay_from_disable_file() {
|
delay_from_disable_file() {
|
||||||
# $1: disabled file
|
# $1: disabled file containing the re-enable time in sec
|
||||||
last_change=$(stat -c %Z "$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
|
||||||
disable_secs=$(grep -v -E "^\s*#" "${1}" | grep -E "[0-9]+" | head -n1 | awk '{print$1}')
|
if [ -z "${enable_secs}" ]; then
|
||||||
disable_max_secs=$(time_in_seconds "${disable_max_time}" || time_in_seconds "${disable_max_time_default}")
|
file_last_change_secs=$(stat -c %Z "$1")
|
||||||
|
default_disabled_time_secs="$(time_to_seconds "${default_disabled_time}")"
|
||||||
if [ "${disable_secs}" -gt "${disable_max_secs}" ]; then
|
enable_secs="$(( file_last_change_secs + default_disabled_time_secs))"
|
||||||
disable_secs="${disable_max_secs}"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
disable_date=$(date --date "${disable_secs} seconds ago" +"%s")
|
now_secs=$(date +"%s")
|
||||||
|
echo $(( enable_secs - now_secs ))
|
||||||
echo $(( last_change - disable_date ))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enable_checks() {
|
delay_to_string() {
|
||||||
# $1: disable name
|
#$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
|
if [ "$(id -u)" -eq "0" ] ; then
|
||||||
/usr/local/bin/alerts_switch enable "$1"
|
/usr/local/bin/alerts_switch enable "$1"
|
||||||
else
|
else
|
||||||
|
@ -84,58 +113,33 @@ enable_checks() {
|
||||||
}
|
}
|
||||||
|
|
||||||
main() {
|
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=$?
|
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)"
|
check_stdout="Check timeout (9 sec)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
delay=0
|
if [ "${is_disabled}" == "True" ]; then
|
||||||
disabled="False"
|
delay=$(delay_from_disable_file "${disable_file}")
|
||||||
for disable_name in ${disable_names}; do
|
delay_str="$(delay_to_string "${delay}")"
|
||||||
disable_file="${base_dir}/${disable_name}_alerts_disabled"
|
echo "ALERTS DISABLED for ${check_name} until ${delay_str}."
|
||||||
if [ -e "${disable_file}" ]; then
|
fi
|
||||||
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
|
|
||||||
|
|
||||||
echo "${check_stdout}"
|
echo "${check_stdout}"
|
||||||
|
|
||||||
if [ "${disabled}" == "True" ]; then
|
if [ "${is_disabled}" == "True" ]; then
|
||||||
if [ ${check_rc} = 0 ]; then
|
if [ ${check_rc} = 0 ]; then
|
||||||
# Nagios OK
|
exit 0 # Nagios OK
|
||||||
exit 0
|
|
||||||
else
|
else
|
||||||
# Nagios WARNING
|
exit 1 # Nagios WARNING
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
exit ${check_rc}
|
exit ${check_rc}
|
||||||
|
@ -156,42 +160,21 @@ if [[ "${1}" =~ -.* ]]; then
|
||||||
show_version
|
show_version
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
|
-n|--name)
|
||||||
-m|--max|--maximum|--limit)
|
|
||||||
# with value separated by space
|
# with value separated by space
|
||||||
if [ -n "$2" ]; then
|
if [ -n "$2" ]; then
|
||||||
disable_max_time=$2
|
check_name=$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
|
|
||||||
shift
|
shift
|
||||||
else
|
else
|
||||||
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
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
-n|--name=?*|--names=?*)
|
-n|--name=?*)
|
||||||
# with value speparated by =
|
# with value separated by =
|
||||||
disable_names=${1#*=}
|
check_name=${1#*=}
|
||||||
;;
|
;;
|
||||||
-n|--name=|--names=)
|
-n|--name=)
|
||||||
# without value
|
# without value
|
||||||
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
|
||||||
|
@ -219,28 +202,23 @@ if [[ "${1}" =~ -.* ]]; then
|
||||||
check_command="$*"
|
check_command="$*"
|
||||||
else
|
else
|
||||||
# no option is passed (backward compatibility with previous version)
|
# no option is passed (backward compatibility with previous version)
|
||||||
# treat the first argument as disable_names and the rest as the command
|
# treat the first argument as check_name and the rest as the command
|
||||||
disable_names="${1}"
|
check_name="${1}"
|
||||||
shift
|
shift
|
||||||
check_command="$*"
|
check_command="$*"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Default values or errors
|
if [ -z "${check_name}" ]; then
|
||||||
if [ -z "${disable_max_time}" ]; then
|
printf 'ERROR: You must specify a check name, with --names.\n' >&2
|
||||||
disable_max_time="${disable_max_time_default}"
|
exit 1
|
||||||
fi
|
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
|
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 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
disable_names="all $(echo "${disable_names}" | tr ',' ' ')"
|
readonly check_name
|
||||||
readonly disable_names
|
|
||||||
readonly check_command
|
readonly check_command
|
||||||
readonly disable_max_time
|
readonly disable_file="${base_dir}/${check_name}_alerts_disabled"
|
||||||
|
|
||||||
main
|
main
|
||||||
|
|
|
@ -2,12 +2,15 @@
|
||||||
|
|
||||||
#set -x
|
#set -x
|
||||||
|
|
||||||
|
VERSION="24.03.00"
|
||||||
|
|
||||||
readonly base_dir="/var/lib/monitoringctl"
|
readonly base_dir="/var/lib/monitoringctl"
|
||||||
readonly log_path="/var/log/monitoringctl.log"
|
readonly log_path="/var/log/monitoringctl.log"
|
||||||
readonly conf_path="/etc/nagios/nrpe.cfg"
|
readonly conf_path="/etc/nagios/nrpe.cfg"
|
||||||
|
|
||||||
function show_help {
|
function show_help {
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
|
monitoringctl version ${VERSION}.
|
||||||
|
|
||||||
monitoringctl gives some control over NRPE checks and alerts.
|
monitoringctl gives some control over NRPE checks and alerts.
|
||||||
|
|
||||||
|
@ -16,7 +19,8 @@ Usage: monitoringctl [OPTIONS] ACTION ARGUMENTS
|
||||||
GENERAL OPTIONS:
|
GENERAL OPTIONS:
|
||||||
|
|
||||||
-h, --help Print this message and exit.
|
-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:
|
ACTIONS:
|
||||||
|
|
||||||
|
@ -30,7 +34,7 @@ ACTIONS:
|
||||||
-b, --bypass-nrpe Execute directly command from NRPE configuration,
|
-b, --bypass-nrpe Execute directly command from NRPE configuration,
|
||||||
without requesting to NRPE.
|
without requesting to NRPE.
|
||||||
|
|
||||||
alerts-status
|
status
|
||||||
|
|
||||||
Print :
|
Print :
|
||||||
- Wether alerts are enabled or not (silenced).
|
- Wether alerts are enabled or not (silenced).
|
||||||
|
@ -38,16 +42,16 @@ ACTIONS:
|
||||||
- Comment.
|
- Comment.
|
||||||
- Time left before automatic re-enable.
|
- 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.
|
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.
|
Checks output is still printed, so alerts history won't be lost.
|
||||||
|
|
||||||
Options:
|
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)
|
Re-enable all alerts (only global for now)
|
||||||
|
|
||||||
|
@ -57,7 +61,7 @@ COMMENT:
|
||||||
|
|
||||||
DURATION:
|
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:
|
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.
|
||||||
|
@ -68,6 +72,10 @@ Log path: ${log_path}
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function show_version {
|
||||||
|
echo "monitoringctl version ${VERSION}."
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function usage_error {
|
function usage_error {
|
||||||
>&2 echo "$1"
|
>&2 echo "$1"
|
||||||
|
@ -149,7 +157,6 @@ function get_check_commands {
|
||||||
|
|
||||||
function check {
|
function check {
|
||||||
# $1: check name
|
# $1: check name
|
||||||
|
|
||||||
check_nrpe_bin=/usr/lib/nagios/plugins/check_nrpe
|
check_nrpe_bin=/usr/lib/nagios/plugins/check_nrpe
|
||||||
|
|
||||||
if [ ! -f "${check_nrpe_bin}" ]; then
|
if [ ! -f "${check_nrpe_bin}" ]; then
|
||||||
|
@ -218,7 +225,7 @@ function filter_duration {
|
||||||
if [[ "$1" =~ ${time_regex} ]]; then
|
if [[ "$1" =~ ${time_regex} ]]; then
|
||||||
echo "$1"
|
echo "$1"
|
||||||
else
|
else
|
||||||
usage_error "Option --duration: \"$1\" is not a valid duration."
|
usage_error "Option --during: \"$1\" is not a valid duration."
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -248,19 +255,19 @@ function disable_alerts {
|
||||||
# -> mauvais indicateur, cf. le timeout à l'intérieur + le max autorisé dans la commande alerts_wrapper
|
# -> mauvais indicateur, cf. le timeout à l'intérieur + le max autorisé dans la commande alerts_wrapper
|
||||||
#if [ -f "${base_dir}/all_alerts_disabled" ]; then
|
#if [ -f "${base_dir}/all_alerts_disabled" ]; then
|
||||||
# echo "All alerts are already disabled."
|
# echo "All alerts are already disabled."
|
||||||
# alerts-status
|
# status
|
||||||
#fi
|
#fi
|
||||||
|
|
||||||
default_msg="."
|
default_msg="."
|
||||||
if [ "${default_duration}" = "True" ]; then
|
if [ "${default_duration}" = "True" ]; then
|
||||||
default_msg=" (default value).
|
default_msg=" (default value).
|
||||||
Hint: use --duration DURATION to change default time length."
|
Hint: use --during DURATION to change default time length."
|
||||||
fi
|
fi
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
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-alerts
|
monitoringctl enable
|
||||||
EOF
|
EOF
|
||||||
echo -n "Confirm (y/N)? "
|
echo -n "Confirm (y/N)? "
|
||||||
read -r answer
|
read -r answer
|
||||||
|
@ -269,7 +276,7 @@ EOF
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
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
|
# Log a warning if a check has no wrapper
|
||||||
for check in $(get_checks_list); do
|
for check in $(get_checks_list); do
|
||||||
|
@ -286,23 +293,24 @@ EOF
|
||||||
#done
|
#done
|
||||||
#done
|
#done
|
||||||
|
|
||||||
log "Executing 'alerts_switch disable all'"
|
log "Executing 'alerts_switch disable all --during \"${duration}\"'"
|
||||||
alerts_switch disable all --duration "${duration}"
|
alerts_switch disable all --during "${duration}"
|
||||||
|
|
||||||
echo "All alerts are now disabled for ${duration}."
|
echo "All alerts are now disabled for ${duration}."
|
||||||
}
|
}
|
||||||
|
|
||||||
function enable-alerts {
|
function enable {
|
||||||
# $1: comment
|
# $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'"
|
log "Executing 'alerts_switch enable all'"
|
||||||
|
alerts_switch enable all
|
||||||
|
|
||||||
echo "All alerts are now enabled."
|
echo "All alerts are now enabled."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
### ALERTS-STATUS ACTION ##########################
|
### status ACTION ##########################
|
||||||
|
|
||||||
# Converts human writable duration into seconds
|
# Converts human writable duration into seconds
|
||||||
function duration_to_seconds {
|
function duration_to_seconds {
|
||||||
|
@ -427,24 +435,27 @@ while :; do
|
||||||
-h|-\?|--help)
|
-h|-\?|--help)
|
||||||
show_help
|
show_help
|
||||||
exit 0;;
|
exit 0;;
|
||||||
|
-V|--version)
|
||||||
|
show_version
|
||||||
|
exit 0;;
|
||||||
-v|--verbose)
|
-v|--verbose)
|
||||||
verbose="True"
|
verbose="True"
|
||||||
shift;;
|
shift;;
|
||||||
-b|--bypass-nrpe)
|
-b|--bypass-nrpe)
|
||||||
bypass_nrpe="True"
|
bypass_nrpe="True"
|
||||||
shift;;
|
shift;;
|
||||||
-d|--duration)
|
-d|--during)
|
||||||
if [ "${default_duration}" = "False" ]; then
|
if [ "${default_duration}" = "False" ]; then
|
||||||
usage_error "Option --duration: defined multiple times."
|
usage_error "Option --during: defined multiple times."
|
||||||
fi
|
fi
|
||||||
if [ "$#" -gt 1 ]; then
|
if [ "$#" -gt 1 ]; then
|
||||||
duration=$(filter_duration "$2")
|
duration=$(filter_duration "$2")
|
||||||
default_duration="False"
|
default_duration="False"
|
||||||
else
|
else
|
||||||
usage_error "Option --duration: missing value."
|
usage_error "Option --during: missing value."
|
||||||
fi
|
fi
|
||||||
shift; shift;;
|
shift; shift;;
|
||||||
check|enable-alerts|disable-alerts|alerts-status)
|
check|enable|disable|status)
|
||||||
action="$1"
|
action="$1"
|
||||||
shift;;
|
shift;;
|
||||||
*)
|
*)
|
||||||
|
@ -474,31 +485,31 @@ if [ "${action}" = "check" ]; then
|
||||||
usage_error "Action check: too many arguments."
|
usage_error "Action check: too many arguments."
|
||||||
fi
|
fi
|
||||||
if [ "${default_duration}" = "False" ]; then
|
if [ "${default_duration}" = "False" ]; then
|
||||||
usage_error "Action check: there is no --duration option."
|
usage_error "Action check: there is no --during option."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
check "$check_name"
|
check "$check_name"
|
||||||
|
|
||||||
elif [ "${action}" = "enable-alerts" ]; then
|
elif [ "${action}" = "enable" ]; then
|
||||||
if [ "$#" = 0 ]; then
|
if [ "$#" = 0 ]; then
|
||||||
usage_error "Action enable-alerts: missing COMMENT argument."
|
usage_error "Action enable: missing COMMENT argument."
|
||||||
fi
|
fi
|
||||||
if [ "$#" -gt 1 ]; then
|
if [ "$#" -gt 1 ]; then
|
||||||
usage_error "Action enable-alerts: too many arguments."
|
usage_error "Action enable: too many arguments."
|
||||||
fi
|
fi
|
||||||
if [ "${default_duration}" = "False" ]; then
|
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
|
fi
|
||||||
|
|
||||||
comment="$1"
|
comment="$1"
|
||||||
enable-alerts "${comment}"
|
enable "${comment}"
|
||||||
|
|
||||||
elif [ "${action}" = "disable-alerts" ]; then
|
elif [ "${action}" = "disable" ]; then
|
||||||
if [ "$#" = 0 ]; then
|
if [ "$#" = 0 ]; then
|
||||||
usage_error "Action disable-alerts: missing COMMENT argument."
|
usage_error "Action disable: missing COMMENT argument."
|
||||||
fi
|
fi
|
||||||
if [ "$#" -gt 1 ]; then
|
if [ "$#" -gt 1 ]; then
|
||||||
usage_error "Action disable-alerts: too many arguments."
|
usage_error "Action disable: too many arguments."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
is_nrpe_wrapped
|
is_nrpe_wrapped
|
||||||
|
@ -506,9 +517,9 @@ elif [ "${action}" = "disable-alerts" ]; then
|
||||||
comment="$1"
|
comment="$1"
|
||||||
disable_alerts "${comment}"
|
disable_alerts "${comment}"
|
||||||
|
|
||||||
elif [ "${action}" = "alerts-status" ]; then
|
elif [ "${action}" = "status" ]; then
|
||||||
if [ "$#" -gt 0 ]; then
|
if [ "$#" -gt 0 ]; then
|
||||||
usage_error "Action alerts-status: too many arguments."
|
usage_error "Action status: too many arguments."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
alerts_status
|
alerts_status
|
||||||
|
|
25
nagios-nrpe/files/monitoringctl_completion
Executable file
25
nagios-nrpe/files/monitoringctl_completion
Executable 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
|
||||||
|
|
||||||
|
|
95
nagios-nrpe/files/monitoringctl_completion_dev
Normal file
95
nagios-nrpe/files/monitoringctl_completion_dev
Normal 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
|
||||||
|
|
Loading…
Reference in a new issue