Added NCPA mode (with potential double -H lookup) and updated debugging

dev
Eric Loyd 1 year ago
parent d497b68bae
commit 77c773b0dd

38
ENV

@ -8,12 +8,16 @@ myHost=""
myService=""
myContact=""
doUpper=""
doVerbose=""
doNCPA=""
do_help() {
cat << HELP_EOF
This utility is designed to be called by the ARGx parameters of a Nagios configuration as follows:
This utility is designed to be called by the ARGx parameters of a Nagios configuration as one of the following:
\$\$(\$USER1\$/env [-n <var>] [-t <default>] [-f <field>] [-d <delim>] [-u] [-H|-S|-C <suffix>] [-v]
\$\$(\$USER1\$/env -N [-H suffix]
\$\$(\$USER1\$/env [-n <var>] [-t <default>] [-f <field>] [-d <delim>] [-u] [-H|-S|-C <suffix>]
Where:
@ -21,18 +25,23 @@ Where:
default a default value if \$NAGIOS_var\$ is blank (case sensitive)
field an awk-compatible field number (potentially to use with <delim>)
delim an awk-compatible field delimiter (defaults to space, dash, and colon)
-u Convert the output to uppercase (useful for further levels of indirection)
-u Convert the output to uppercase
suffix Take var result and use the it to get NAGIOS__[HOST|SERVICE|CONTACT]<result><suffix>
-v Be verbose (only for debugging)
-N Look for NAGIOS__HOSTNCPA<suffix> (defaults to TOKEN)
Can do double lookup if -H is specified directly: -H TOKEN will find NAGIOS__HOSTNCPATOKEN=Level1 which then finds NAGIOS__HOSTLEVEL1TOKEN=Secret
Or -H PASS will find NAGIOS__HOSTNCPAPASS=Level2 which will then find NAGIOS__HOSTLEVEL2PASS=SuperSecret
The value will always be trimmed of leading and/or trailing spaces/tabs.
If -f is used, then the result will be as if the request were as follows, unless the field starts with a negative, in which case it will read from the last word forward:
eche \$NAGIOS_var\$ | awk -F <delim> '{print $<field>}'
echo \$NAGIOS_var\$ | awk -F <delim> '{print $<field>}'
Examples (service_description is assumed to be "MySQL Check: myDatabase performance"):
.../env -n servicedesc Return: "MySQL Check: myDatabase performance"
.../env Return: "MySQL Check: myDatabase performance"
.../env -n hostName Return: "localhost" (or whatever the name of the host is that ran the check)
.../env -n SERVICESTATE -t 0 Return: current state of the service (0, 1, 2) or 0 if the state is not defined
.../env -n servicedesc -f 2 Return: "Check" (watch out for defaults!)
@ -57,14 +66,29 @@ while [ -n "$1" ]; do
-H) myHost="$2"; shift 2;;
-S) myService="$2"; shift 2;;
-C) myContact="$2"; shift 2;;
-N) doNCPA="true"; shift 1;;
-u) doUpper="true"; shift 1;;
-v) doVerbose="true"; shift 1;;
*) shift 1;;
esac
done
do_debug() {
[ -n "$doVerbose" ] && echo "DEBUG: $*"
}
if [ -n "$doNCPA" ]; then
do_debug "NCPA mode active. myHost=$myHost"
varName=`echo "NAGIOS__HOSTNCPA${myHost:-token}" | tr "a-z" "A-Z"`
else
do_debug "NCPA mode NOT active."
varName=`echo "NAGIOS_$varName" | tr "a-z" "A-Z"`
fi
do_debug "Looking for $varName..."
varValue="${!varName}"
do_debug "Got varValue=$varValue"
if [ -n "$myField" ]; then
do_debug "myField=$myField"
if [ "$myField" -lt 0 ]; then
varValue=`echo "$varValue" | rev | awk -F "$myDelim" -v f="${myField:1}" '{print $f}' | rev`
else
@ -72,24 +96,28 @@ if [ -n "$myField" ]; then
fi
fi
varValue=`echo $varValue | sed -e "s/^[ \t]*//" -e "s/[ \t]*$//"`
do_debug "After processing varValue=$varValue"
[ -n "$doUpper" ] && varValue=`echo "$varValue" | tr "a-z" "A-Z"`
# Check for myHost
if [ -n "$myHost" ]; then
myHost=`echo "NAGIOS__HOST${varValue}${myHost}" | tr "a-z" "A-Z"`
do_debug "myHost=$myHost"
varValue=${!myHost}
fi
# Check for myService
if [ -n "$myService" ]; then
myService=`echo "NAGIOS__SERVICE${varValue}${myService}" | tr "a-z" "A-Z"`
do_debug "myService=$myService"
varValue=${!myService}
fi
# Check for myContact
if [ -n "$myContact" ]; then
myContact=`echo "NAGIOS__CONTACT${varValue}${myContact}" | tr "a-z" "A-Z"`
do_debug "myContact=$myContact"
varValue=${!myContact}
fi

Loading…
Cancel
Save