ansible-roles/nagios-nrpe/files/alerts_wrapper

225 lines
6.1 KiB
Plaintext
Raw Normal View History

#!/bin/bash
#
# Source:
# https://gitea.evolix.org/evolix/ansible-roles/src/branch/stable/nagios-nrpe
#
2024-02-16 11:58:19 +01:00
readonly VERSION="21.04"
# Location of disable files
readonly base_dir="/var/lib/monitoringctl"
2024-02-16 11:58:19 +01:00
# 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
}
2024-02-16 11:58:19 +01:00
show_help() {
cat <<END
2024-02-06 14:20:41 +01:00
alerts_wrapper wraps an NRPE command and overrides the return code.
2024-02-16 11:58:19 +01:00
Usage: alerts_wrapper --name <CHECK_NAME> <CHECK_COMMAND>
Usage: alerts_wrapper <CHECK_NAME> <CHECK_COMMAND> (deprecated)
Options
2024-02-16 11:58:19 +01:00
--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
}
2024-02-16 11:58:19 +01:00
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() {
2024-02-16 11:58:19 +01:00
# $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
2024-02-16 11:58:19 +01:00
now_secs=$(date +"%s")
echo $(( enable_secs - now_secs ))
}
2024-02-16 11:58:19 +01:00
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
2024-02-16 11:58:19 +01:00
delay_minutes="$(( ((delay %86400) %3600) /60 ))"
if [ "${delay_minutes}" -eq 0 ]; then delay_minutes=""
else delay_minutes="${delay_minutes}m "; fi
2024-02-16 11:58:19 +01:00
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}"
}
2024-02-16 11:58:19 +01:00
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
2024-02-06 14:20:41 +01:00
/usr/local/bin/alerts_switch enable "$1"
else
2024-02-06 14:20:41 +01:00
sudo /usr/local/bin/alerts_switch enable "$1"
fi
}
main() {
2024-02-16 11:58:19 +01:00
is_disabled=$(is_disabled "${disable_file}")
2024-02-16 11:58:19 +01:00
timeout_command=""
if [ "${is_disabled}" == "True" ]; then
timeout_command="timeout 9"
fi
2024-02-16 11:58:19 +01:00
check_stdout=$(${timeout_command} "${check_command}")
check_rc=$?
2024-02-16 11:58:19 +01:00
if [ "${is_disabled}" == "True" ] && [ "${check_rc}" -eq 124 ] && [ -z "${check_stdout}" ]; then
check_stdout="Check timeout (9 sec)"
fi
2024-02-16 11:58:19 +01:00
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}"
2024-02-16 11:58:19 +01:00
if [ "${is_disabled}" == "True" ]; then
if [ ${check_rc} = 0 ]; then
2024-02-16 11:58:19 +01:00
exit 0 # Nagios OK
else
2024-02-16 11:58:19 +01:00
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
;;
2024-02-16 11:58:19 +01:00
-n|--name)
# with value separated by space
if [ -n "$2" ]; then
2024-02-16 11:58:19 +01:00
check_name=$2
shift
else
printf 'ERROR: "--name" requires a non-empty option argument.\n' >&2
exit 1
fi
;;
2024-02-16 11:58:19 +01:00
-n|--name=?*)
# with value separated by =
check_name=${1#*=}
;;
2024-02-16 11:58:19 +01:00
-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)
2024-02-16 11:58:19 +01:00
# treat the first argument as check_name and the rest as the command
check_name="${1}"
shift
check_command="$*"
fi
2024-02-16 11:58:19 +01:00
if [ -z "${check_name}" ]; 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
2024-02-16 11:58:19 +01:00
readonly check_name
readonly check_command
2024-02-16 11:58:19 +01:00
readonly disable_file="${base_dir}/${check_name}_alerts_disabled"
main