247 lines
7.4 KiB
Bash
Executable file
247 lines
7.4 KiB
Bash
Executable file
#!/bin/bash
|
|
#
|
|
# Source:
|
|
# https://gitea.evolix.org/evolix/ansible-roles/src/branch/stable/nagios-nrpe
|
|
#
|
|
|
|
VERSION="21.04"
|
|
readonly VERSION
|
|
readonly base_dir="/var/lib/monitoringctl"
|
|
|
|
# Default maximum allowed disable time
|
|
disable_max_time_default="1d"
|
|
readonly disable_max_time_default
|
|
|
|
# base functions
|
|
|
|
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 [--maximum 1d] [--names check,other_disable_name,...]] <check command with optional arguments>
|
|
Usage: alerts_wrapper disable_name <check command with optional arguments>
|
|
|
|
Options
|
|
-m|--max|--maximum Max age of the disable file.
|
|
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.
|
|
--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
|
|
}
|
|
|
|
time_in_seconds() {
|
|
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
|
|
last_change=$(stat -c %Z "$1")
|
|
|
|
disable_secs=$(grep -v -E "^\s*#" "${1}" | grep -E "[0-9]+" | head -n1 | awk '{print$1}')
|
|
disable_max_secs=$(time_in_seconds "${disable_max_time}" || time_in_seconds "${disable_max_time_default}")
|
|
|
|
if [ "${disable_secs}" -gt "${disable_max_secs}" ]; then
|
|
disable_secs="${disable_max_secs}"
|
|
fi
|
|
|
|
disable_date=$(date --date "${disable_secs} seconds ago" +"%s")
|
|
|
|
echo $(( last_change - disable_date ))
|
|
}
|
|
|
|
enable_checks() {
|
|
# $1: disable 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() {
|
|
check_stdout=$(timeout 9 ${check_command})
|
|
check_rc=$?
|
|
|
|
if [ "${check_rc}" -eq 124 ] && [ -z "${check_stdout}" ]; then
|
|
check_stdout="Check timeout (9 sec)"
|
|
fi
|
|
|
|
delay=0
|
|
disabled="False"
|
|
for disable_name in ${disable_names}; do
|
|
disable_file="${base_dir}/${disable_name}_alerts_disabled"
|
|
if [ -e "${disable_file}" ]; then
|
|
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}"
|
|
|
|
if [ "${disabled}" == "True" ]; then
|
|
if [ ${check_rc} = 0 ]; then
|
|
# Nagios OK
|
|
exit 0
|
|
else
|
|
# Nagios WARNING
|
|
exit 1
|
|
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
|
|
;;
|
|
|
|
-m|--max|--maximum|--limit)
|
|
# with value separated by space
|
|
if [ -n "$2" ]; then
|
|
disable_max_time=$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
|
|
else
|
|
printf 'ERROR: "--name" requires a non-empty option argument.\n' >&2
|
|
exit 1
|
|
fi
|
|
;;
|
|
-n|--name=?*|--names=?*)
|
|
# with value speparated by =
|
|
disable_names=${1#*=}
|
|
;;
|
|
-n|--name=|--names=)
|
|
# 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 disable_names and the rest as the command
|
|
disable_names="${1}"
|
|
shift
|
|
check_command="$*"
|
|
fi
|
|
|
|
# Default values or errors
|
|
if [ -z "${disable_max_time}" ]; then
|
|
disable_max_time="${disable_max_time_default}"
|
|
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
|
|
printf 'ERROR: You must specify a command to execute.\n' >&2
|
|
exit 1
|
|
fi
|
|
|
|
disable_names="all $(echo "${disable_names}" | tr ',' ' ')"
|
|
readonly disable_names
|
|
readonly check_command
|
|
readonly disable_max_time
|
|
|
|
main
|