ansible-roles/nagios-nrpe/files/alerts_wrapper
2024-05-03 10:08:21 +02:00

228 lines
6.3 KiB
Bash
Executable file

#!/bin/bash
#
# Source:
# https://gitea.evolix.org/evolix/ansible-roles/src/branch/stable/nagios-nrpe
#
readonly VERSION="21.04"
# Location of disable files
readonly base_dir="/var/lib/monitoringctl"
# If no datetime is found in file, this value is used
readonly default_disabled_time="1d"
show_version() {
cat <<END
alerts_wrapper version ${VERSION}
Copyright 2018-2021 Evolix <info@evolix.fr>,
Jérémy Lecour <jlecour@evolix.fr>
and others.
alerts_wrapper comes with ABSOLUTELY NO WARRANTY.This is free software,
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 --name <CHECK_NAME> <CHECK_COMMAND>
Usage: alerts_wrapper <CHECK_NAME> <CHECK_COMMAND> (deprecated)
Options
--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_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
echo "${1} * 3600" | xargs expr
else
return 1
fi
}
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}')
# 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
now_secs=$(date +"%s")
echo $(( enable_secs - now_secs ))
}
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_seconds="${delay_seconds}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
sudo /usr/local/bin/alerts_switch enable "$1"
fi
}
main() {
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 [ "${is_disabled}" == "True" ] && [ "${check_rc}" -eq 124 ] && [ -z "${check_stdout}" ]; then
check_stdout="Check timeout (9 sec)"
fi
if [ "${is_disabled}" == "True" ]; then
delay=$(delay_from_disable_file "${disable_file}")
delay_str="$(delay_to_string "${delay}")"
echo "ALERT DISABLED until ${delay_str} - ${check_stdout}"
else
echo "${check_stdout}"
fi
if [ "${is_disabled}" == "True" ]; then
if [ ${check_rc} = 0 ]; then
exit 0 # Nagios OK
else
exit 1 # Nagios WARNING
fi
else
exit ${check_rc}
fi
}
if [[ "${1}" =~ -.* ]]; then
# parse options
# based on https://gist.github.com/deshion/10d3cb5f88a21671e17a
while :; do
case $1 in
-h|-\?|--help)
show_help
exit 0
;;
-V|--version)
show_version
exit 0
;;
-n|--name)
# with value separated by space
if [ -n "$2" ]; then
check_name=$2
shift
else
printf 'ERROR: "--name" requires a non-empty option argument.\n' >&2
exit 1
fi
;;
-n|--name=?*)
# with value separated by =
check_name=${1#*=}
;;
-n|--name=)
# without value
printf 'ERROR: "--name" requires a non-empty option argument.\n' >&2
exit 1
;;
--)
# End of all options.
shift
break
;;
-?*)
# ignore unknown options
printf 'WARN: Unknown option : %s\n' "$1" >&2
exit 1
;;
*)
# Default case: If no more options then break out of the loop.
break
;;
esac
shift
done
# The rest is the command
check_command="$*"
else
# no option is passed (backward compatibility with previous version)
# treat the first argument as check_name and the rest as the command
check_name="${1}"
shift
check_command="$*"
fi
if [ -z "${check_name}" ]; then
printf 'ERROR: You must specify a check name, with --names.\n' >&2
exit 2
fi
if [ -z "${check_command}" ]; then
printf 'ERROR: You must specify a command to execute.\n' >&2
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
readonly check_command
readonly disable_file="${base_dir}/${check_name}_alerts_disabled"
main