#!/bin/bash
#
# Wrapper around the NRPE script "check_prtdiag" to interpret correct status
#

plugins=/usr/local/monitoring/nagios/libexec
port=9666
timeout=90

if [[ "$#" -ne "1" ]]; then echo "Usage: check_nrpe_prtdiag_wrapper <hostname>"; exit 3; fi

host=$1

output=`${plugins}/check_nrpe -p $port -t $timeout -H $host -c check_prtdiag 2>&1`

if echo $output | grep -q "^OK"; then    # OK/recovery alert
	stat=0; text_stat=OK
elif echo $output | grep -q "status is '\(failed\|faulty\)'"; then # failed/faulty = CRITICAL
	stat=2; text_stat=CRITICAL
elif echo $output | grep -q "status is"; then  # "status is" which is not CRITICAL is a WARNING
	stat=1; text_stat=WARNING
else                                     # Everything else is UNKNOWN
	stat=3; text_stat=UNKNOWN
fi

# Final output w/o the original text status
text_out=`echo $output | sed -n 's/^[^\-]\+\(.*\)/'$text_stat' \1/p'`

# Just a precaution... if the original text status didn't have "^\(OK\|WARNING\|CRITICAL\|UNKNOWN\) -", then simply use the original
if [[ -z "$text_out" ]]; then text_out="$output"; fi

echo $text_out

exit $stat
