diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d0f0219..cc148fba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ The **patch** part changes incrementally at each release. * lxc: remove useless loop in apt execution * lxc: update our default template to be compatible with Debian 10 * minifirewall: no http filtering by default +* nagios-nrpe: update check_redis_instances (same as redis role) * php: By default, allow 128M for OpCache (instead of 64M) * php: Don't set a chroot for the default fpm pool * rbenv: install Ruby 2.6.5 by default diff --git a/nagios-nrpe/files/plugins/check_redis_instances b/nagios-nrpe/files/plugins/check_redis_instances index bd6ed634..5923eef7 100755 --- a/nagios-nrpe/files/plugins/check_redis_instances +++ b/nagios-nrpe/files/plugins/check_redis_instances @@ -1,5 +1,7 @@ #!/bin/sh +# {{ ansible_managed }} + set -u return=0 @@ -9,35 +11,66 @@ nb_ok=0 nb_unchk=0 output="" -instances=$(ls /etc/redis/redis-*.conf) -for instance in ${instances}; do - name=$(basename "${instance}"| sed '{s/redis-//;s/.conf//}') - port=$(grep "port" "${instance}"|grep -oE "[0-9]*") - socket=$(grep "unixsocket " "${instance}"|awk '{ print $2 }') - if [ -h "/etc/systemd/system/multi-user.target.wants/redis-server@${name}.service" ]; then - if [ "${port}" -ne 0 ]; then - /usr/lib/nagios/plugins/check_tcp -p "${port}" >/dev/null 2>&1 - ret="${?}" - else - /usr/lib/nagios/plugins/check_tcp -H "${socket}" >/dev/null 2>&1 - ret="${?}" - fi - if [ "${ret}" -ge 2 ]; then - nb_crit=$((nb_crit + 1)) - output="${output}CRITICAL - ${name} (${port})\n" - [ "${return}" -le 2 ] && return=2 - elif [ "${ret}" -ge 1 ]; then - nb_warn=$((nb_warn + 1)) - output="${output}WARNING - ${name} (${port})\n" - [ "${return}" -le 1 ] && return=1 - else - nb_ok=$((nb_ok + 1)) - output="${output}OK - ${name} (${port})\n" - [ "${return}" -le 0 ] && return=0 - fi +packaged_check=/usr/lib/nagios/plugins/check_redis +vendored_check=/usr/local/lib/nagios/plugins/check_redis + +if [ -x $packaged_check ]; then + check_bin=$packaged_check +elif [ -x $vendored_check ]; then + check_bin=$vendored_check +else + echo "UNCHK - can't find check_redis" + exit 3 +fi + +check_server() { + name=$1 + conf_file=$2 + + host=$(config_var "bind" "${conf_file}") + port=$(config_var "port" "${conf_file}") + pass=$(config_var "requirepass" "${conf_file}") + + cmd="${check_bin} -H ${host} -p ${port}" + if [ -n "${pass}" ]; then + cmd="${cmd} -x ${pass}" + fi + result=$($cmd) + ret="${?}" + if [ "${ret}" -ge 2 ]; then + nb_crit=$((nb_crit + 1)) + output="${output}${result}\n" + [ "${return}" -le 2 ] && return=2 + elif [ "${ret}" -ge 1 ]; then + nb_warn=$((nb_warn + 1)) + output="${output}${result}\n" + [ "${return}" -le 1 ] && return=1 + else + nb_ok=$((nb_ok + 1)) + output="${output}${result}\n" + [ "${return}" -le 0 ] && return=0 + fi +} +config_var() { + variable=$1 + file=$2 + test -f $file && grep -E "^${variable}\s+.+$" $file | awk '{ print $2 }' +} + +# default instance +if systemctl is-enabled -q redis-server; then + check_server "default" "/etc/redis/redis.conf" +fi + +# additional instances +conf_files=$(ls -1 /etc/redis-*/redis.conf) +for conf_file in ${conf_files}; do + name=$(dirname ${conf_file} | sed '{s|/etc/redis-||}') + if systemctl is-enabled -q "redis-server@${name}.service"; then + check_server $name $conf_file else nb_unchk=$((nb_unchk + 1)) - output="${output}UNCHK - ${name} (${port})\n" + output="${output}UNCHK - ${name} (unit is disabled or missing)\n" fi done @@ -47,9 +80,9 @@ done printf "%s - %s UNCHK / %s CRIT / %s WARN / %s OK\n\n" "${header}" "${nb_unchk}" "${nb_crit}" "${nb_warn}" "${nb_ok}" -printf "${output}" | grep -E "^CRITICAL" -printf "${output}" | grep -E "^WARNING" -printf "${output}" | grep -E "^OK" -printf "${output}" | grep -E "^UNCHK" +printf "%s" "${output}" | grep -E "^CRITICAL" +printf "%s" "${output}" | grep -E "^WARNING" +printf "%s" "${output}" | grep -E "^OK" +printf "%s" "${output}" | grep -E "^UNCHK" exit "${return}"