ansible-roles/nagios-nrpe/files/check-local

83 lines
2.2 KiB
Bash
Executable file

#!/usr/bin/env bash
#set -x
CHECK_BIN=/usr/lib/nagios/plugins/check_nrpe
server_address="127.0.0.1"
check_lookup="$1"
debian_major_version=$(cut -d "." -f 1 < /etc/debian_version)
function grep_conf {
grep -E -R --no-filename "^\s*(include(_dir)?=.+|command\[check_$1)" $2 | grep -v -E '^[[:blank:]]*#'
}
# Print check commands, in the same order as they are declared in the conf,
# with respect to the include and include_dir directives.
function get_config_dir_checks {
# $1: check name (load, disk1…)
# $2: nrpe conf dir
conf_dir=$(echo "${line}" | cut -d= -f2)
if [ $debian_major_version -ge 10 ]; then
# From Deb10, NRPE use scandir() with alphasort() function
sort_command="sort"
else
# Before Deb10, NRPE use loaddir(), like find utility
sort_command="cat -"
fi
# Add conf files in dir to be processed recursively
for file in $(find "$2" -maxdepth 1 -name "*.cfg" | "${sort_command}"); do
if [ -f "${file}" ]; then
get_config_file_checks "$1" "${file}"
elif [ -d "${file}" ]; then
get_config_dir_checks "$1" "${file}"
fi
done
}
function get_config_file_checks {
# $1: check name (load, disk1…)
# $2: nrpe conf file (.cfg)
conf_lines=$(grep_conf $1 $2)
while read line; do
if [[ "${line}" =~ .*"check_$1".* ]]; then
echo "${line}" | cut -d'=' -f2-
elif [[ "${line}" =~ .*'include='.* ]]; then
conf_file=$(echo "${line}" | cut -d= -f2)
get_config_file_checks "$1" "${conf_file}"
elif [[ "${line}" =~ .*'include_dir='.* ]]; then
conf_dir=$(echo "${line}" | cut -d= -f2)
get_config_dir_checks $1 "${conf_dir}"
fi
done <<< "${conf_lines}"
}
if [ ! -f "${CHECK_BIN}" ]; then
echo "${CHECK_BIN} is missing, please install nagios-nrpe-plugin package."
exit 1
fi
commands=$(get_config_file_checks $1 /etc/nagios/nrpe.cfg)
echo "$commands"
command=$(echo "${commands}" | tail -n1)
if [ -n "${command}" ]; then
echo "Command:"
echo " ${command}"
fi
check_output=$("${CHECK_BIN}" -H "${server_address}" -c "check_$1" 2&>1)
rc=$?
echo "NRPE daemon output:"
echo $check_output