Implémentation raw de la recherche récursive d'une commande rnpe dans sa conf

This commit is contained in:
William Hirigoyen 2024-01-19 17:59:17 +01:00
parent 13aab31131
commit 0fa7877d62

View file

@ -1,60 +1,73 @@
#!/usr/bin/env bash #!/usr/bin/env bash
#set -x
CHECK_BIN=/usr/lib/nagios/plugins/check_nrpe CHECK_BIN=/usr/lib/nagios/plugins/check_nrpe
server_address="127.0.0.1" server_address="127.0.0.1"
#check_lookup="$1" check_lookup="$1"
#
## Array of commands found in the conf (first-found order) debian_major_version=$(cut -d "." -f 1 < /etc/debian_version)
#found_commands=()
# function grep_conf {
## Queue of conf files and directories included in Nagios grep -E -R --no-filename "^\s*(include(_dir)?=.+|command\[check_$1)" $2 | grep -v -E '^[[:blank:]]*#'
#includes_fifo=("/etc/nagios/nrpe.cfg") }
#
## Debian major version number # Print check commands, in the same order as they are declared in the conf,
#debian_major_version=$(cut -d "." -f 1 < /etc/debian_version) # with respect to the include and include_dir directives.
#
#function grep_conf { function get_config_dir_checks {
# grep -E -R "^\s*(include(_dir)?=.+|command\[check_$1)" $2 # $1: check name (load, disk1…)
#} # $2: nrpe conf dir
# conf_dir=$(echo "${line}" | cut -d= -f2)
## Print check commands, in the same order as they are declared in the conf, if [ $debian_major_version -ge 10 ]; then
## with respect to the include and include_dir directives. # From Deb10, NRPE use scandir() with alphasort() function
#for l in $(grep_conf "${check_lookup}" /etc/nagios/nrpe.cfg); do sort_command="sort"
# if [[ $l =~ "check_${check_lookup}" ]]; then else
# echo $l # Before Deb10, NRPE use loaddir(), like find utility
# elif [[ $l =~ 'include=' ]]; then sort_command="cat -"
# conf_file=$(echo $l | cut -d= -f2) fi
# grep_conf "${check_lookup}" "${conf_file}" # Add conf files in dir to be processed recursively
# elif [[ $l =~ 'include_dir=' ]]; then for file in $(find "$2" -maxdepth 1 -name "*.cfg" | "${sort_command}"); do
# true if [ -f "${file}" ]; then
# # todo if Debian <= 9 then find else find | sort get_config_file_checks "$1" "${file}"
# fi elif [ -d "${file}" ]; then
#done 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 ! test -f "${CHECK_BIN}"; then if [ ! -f "${CHECK_BIN}" ]; then
echo "${CHECK_BIN} is missing, please install nagios-nrpe-plugin package." echo "${CHECK_BIN} is missing, please install nagios-nrpe-plugin package."
exit 1 exit 1
fi fi
for file in /etc/nagios/{nrpe.cfg,nrpe_local.cfg,nrpe.d/evolix.cfg}; do commands=$(get_config_file_checks $1 /etc/nagios/nrpe.cfg)
if [ -r ${file} ]; then echo "$commands"
command_search=$(grep "\[check_$1\]" "${file}" | grep -v '^[[:blank:]]*#' | tail -n1 | cut -d'=' -f2-) command=$(echo "${commands}" | tail -n1)
fi
if [ -n "${command_search}" ]; then
command="${command_search}"
fi
if [ -r ${file} ]; then
server_address_search=$(grep "server_address" "${file}" | grep -v '^[[:blank:]]*#' | cut -d'=' -f2)
fi
if [ -n "${server_address_search}" ]; then
server_address="${server_address_search}"
fi
done
if [ -n "${command}" ]; then if [ -n "${command}" ]; then
echo "Command:" echo "Command:"