142 lines
4.3 KiB
Bash
Executable file
142 lines
4.3 KiB
Bash
Executable file
#!/bin/bash
|
|
#
|
|
# Source:
|
|
# https://gitea.evolix.org/evolix/ansible-roles/src/branch/stable/nagios-nrpe
|
|
#
|
|
|
|
readonly PROGNAME=$(basename $0)
|
|
readonly VERSION="24.04.00"
|
|
|
|
# Load common functions and vars
|
|
readonly lib_dir="/usr/local/lib/monitoringctl"
|
|
if [ -r "${lib_dir}/common" ]; then
|
|
# shellcheck source=monitoringctl_common
|
|
source "${lib_dir}/common"
|
|
else
|
|
>&2 echo "Error: missing ${lib_dir}/common file."
|
|
exit 1
|
|
fi
|
|
|
|
function show_help() {
|
|
cat <<END
|
|
$PROGNAME disables or enables NRPE alerts wrapped by the script 'alerts_wrapper' in NRPE configuration.
|
|
|
|
Usage: $PROGNAME disable [-d|--during <DURATION>] [--message '<DISABLE_MESSAGE>'] <WRAPPER_NAME|all>
|
|
$PROGNAME enable [--message '<ENABLE_MESSAGE>'] <WRAPPER_NAME|all>
|
|
$PROGNAME help
|
|
|
|
WRAPPER_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
|
|
DISABLE_MESSAGE: Message that will be logged and printed by alerts_wrapper
|
|
when alert is disabled.
|
|
ENABLE_MESSAGE: Message that will be logged when alert is enabled
|
|
END
|
|
}
|
|
|
|
function disable_alerts() {
|
|
# $1: wrapper name, $2: duration_sec, $3: disable message
|
|
now_secs=$(date +"%s")
|
|
disable_until_secs=$(( now_secs + ${2} ))
|
|
disable_file_path="$(get_disable_file_path "${1}")"
|
|
echo "${disable_until_secs}" > "${disable_file_path}"
|
|
echo "$(logname || echo unknown): \"${3}\"" >> "${disable_file_path}"
|
|
chmod 0644 "${disable_file_path}"
|
|
log "${1} alerts disabled by $(logname || echo unknown)"
|
|
log "Disable message: ${3}"
|
|
}
|
|
|
|
function enable_alerts() {
|
|
# $1: wrapper name, $2: enable message
|
|
disable_file_path="$(get_disable_file_path "${1}")"
|
|
if [ -e "${disable_file_path}" ]; then
|
|
rm "${disable_file_path}"
|
|
fi
|
|
log "${1} alerts enabled by $(logname || echo unknown)"
|
|
log "Enable message: ${2}"
|
|
}
|
|
|
|
function main() {
|
|
install --mode=0755 --directory "${var_dir}"
|
|
|
|
if [ "${action}" == 'enable' ]; then
|
|
if [ "${wrapper_name}" == "all" ]; then
|
|
for wrapper in $(get_wrappers_names); do
|
|
enable_alerts "${wrapper}" "${message}"
|
|
done
|
|
else
|
|
enable_alerts "${wrapper_name}" "${message}"
|
|
fi
|
|
elif [ "${action}" == 'disable' ]; then
|
|
duration_sec=$(time_to_seconds "${duration}")
|
|
if [ "${wrapper_name}" == "all" ]; then
|
|
for wrapper in $(get_wrappers_names); do
|
|
disable_alerts "${wrapper}" "${duration_sec}" "${message}"
|
|
done
|
|
else
|
|
disable_alerts "${wrapper_name}" "${duration_sec}" "${message}"
|
|
fi
|
|
elif [ "${action}" == 'help' ]; then
|
|
show_help
|
|
fi
|
|
}
|
|
|
|
|
|
while :; do
|
|
case "${1}" in
|
|
enable|disable|help)
|
|
action="${1}"
|
|
shift;;
|
|
-d|--during)
|
|
if [ "$#" -gt 1 ]; then
|
|
if filter_duration "${2}"; then
|
|
duration="${2}"
|
|
else
|
|
usage_error "Option --during: \"${2}\" is not a valid duration."
|
|
fi
|
|
else
|
|
error "Missing --during argument."
|
|
fi
|
|
shift; shift;;
|
|
-m|--message)
|
|
if [ "$#" -gt 1 ]; then
|
|
message="${2}"
|
|
else
|
|
error "Missing --message argument."
|
|
fi
|
|
shift; shift;;
|
|
*)
|
|
if [ -n "${1}" ]; then
|
|
if is_wrapper "${1}" || [ "${1}" == "all" ]; then
|
|
wrapper_name="${1}"
|
|
else
|
|
error "Unknown argument '${1}', or NAME not defined in NRPE configuration."
|
|
fi
|
|
else
|
|
if [ -z "${action}" ]; then
|
|
error "Missing action argument."
|
|
elif [ -z "${1}" ]; then
|
|
break
|
|
fi
|
|
fi
|
|
|
|
shift;;
|
|
esac
|
|
done
|
|
|
|
if [ -z "${wrapper_name}" ] && [ "${action}" != 'help' ] ; then
|
|
error "Missing WRAPPER_NAME."
|
|
fi
|
|
|
|
if [ -z "${duration}" ]; then
|
|
duration="${default_disabled_time}"
|
|
fi
|
|
|
|
readonly wrapper_name duration action
|
|
|
|
main
|
|
|