#!/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 < Usage: alerts_wrapper (deprecated) Options --name Wrapper name, it is very recommended to use the 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 } function enable_wrapper() { # $1: wrapper name if [ "$(id -u)" -eq "0" ] ; then /usr/local/bin/alerts_switch enable "${1}" else sudo /usr/local/bin/alerts_switch enable "${1}" fi } function main() { is_disabled="$(is_disabled "${wrapper_name}")" if [ -e "${disable_file}" ] && [ "${is_disabled}" = "False" ]; then enable_wrapper "${wrapper_name}" fi 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 enable_time="$(get_enable_time "${wrapper_name}")" enable_delay="$(enable_delay "${enable_time}")" delay_str="$(delay_to_string "${enable_delay}")" enable_date="$(date --date "+${enable_delay} seconds" "+%d %h %Y at %H:%M:%S")" disable_msg="$(get_disable_message "${wrapper_name}")" if [ -n "${disable_msg}" ]; then disable_msg="- ${disable_msg} " fi echo "ALERT DISABLED until ${enable_date} (${delay_str} left) ${disable_msg}- Check output: ${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 wrapper_name="${2}" shift else printf 'ERROR: "--name" requires a non-empty option argument.\n' >&2 exit 2 fi ;; -n|--name=?*) # with value separated by = wrapper_name="${1#*=}" ;; -n|--name=) # without value printf 'ERROR: "--name" requires a non-empty option argument.\n' >&2 exit 2 ;; --) # End of all options. shift break ;; -?*) # ignore unknown options printf 'ERROR: Unknown option : %s\n' "${1}" >&2 exit 2 ;; *) # 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 wrapper_name and the rest as the command wrapper_name="${1}" shift check_command="$*" fi if [ -z "${wrapper_name}" ]; then printf 'ERROR: You must specify a wrapper 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 "${var_dir}" ] && [ ! -x "${var_dir}" ]; then printf "ERROR: %s exists but is no reachable.\n" "${var_dir}" exit 2 fi disable_file="$(get_disable_file_path "${wrapper_name}")" readonly wrapper_name check_command disable_file main