Replaced variables with arrays for easier addition of more options/vars
This commit is contained in:
@@ -2,47 +2,40 @@
|
|||||||
|
|
||||||
[ "$1" = "set.txt" ] && . set.txt 2> /dev/null
|
[ "$1" = "set.txt" ] && . set.txt 2> /dev/null
|
||||||
|
|
||||||
varName="servicedesc"
|
declare -A myVar
|
||||||
varDefault=""
|
myVar[Name]="servicedesc"
|
||||||
myDelim="[ :]"
|
myVar[myDelim]="[ :]"
|
||||||
myField=""
|
|
||||||
myHost=""
|
declare -A myOptions
|
||||||
myService=""
|
|
||||||
myContact=""
|
|
||||||
myString=""
|
|
||||||
doNumber=""
|
|
||||||
doPipe=""
|
|
||||||
doLower=""
|
|
||||||
doUpper=""
|
|
||||||
doVerbose=""
|
|
||||||
doNCPA=""
|
|
||||||
|
|
||||||
do_help() {
|
do_help() {
|
||||||
cat << HELP_EOF
|
cat << HELP_EOF
|
||||||
This utility is designed to be called by the ARGx parameters of a Nagios configuration as one of the following:
|
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 <var2>] [-t <default>] [-f|-F <field>] [-d <delim>] [-g|-s <val>] [-u|-l] [-v]
|
||||||
\$\$(\$USER1\$/env [-n <var>] [-t <default>] [-f|-F <field>] [-d <delim>] [-s <val>] [-u|-l] [-H|-S|-C <suffix>] [-v]
|
\$\$(\$USER1\$/env [-NT] [-NP] [-HT|-ST|-CT suffix]
|
||||||
\$\$(\$USER1\$/env -N [-NP] [-H suffix]
|
-H|-S|-C will cause var to be of type NAGIOS__<HOST|SERVICE|CONTACT><var> and then continue with normal processing
|
||||||
|
Use --pre <prefix> and --post <postfix> to add prefix/postfix to the output string after all other processing has completed, but only if not blank.
|
||||||
|
|
||||||
Where:
|
Where:
|
||||||
|
|
||||||
var the name of a case-insensitive NAGIOS_variable (defaults to "servicedesc")
|
var the name of a case-insensitive NAGIOS_variable (defaults to "servicedesc")
|
||||||
default a default value if \$NAGIOS_var\$ is blank (case sensitive)
|
default a default value if \$NAGIOS_var\$ is blank (case sensitive)
|
||||||
field an awk-compatible field number (potentially to use with <delim>) (see below)
|
field an awk-compatible field number (potentially to use with <delim>) (see below)
|
||||||
delim an awk-compatible field delimiter (defaults to space and colon)
|
delim an awk-compatible field delimiter (defaults to space and colon)
|
||||||
|
-T if var returns blank, try vars as NAGIOS___HOSTvar2 before applying -t as a default
|
||||||
|
Example: svc="HTTP: 8080" and $0 -T HTTPPORT -t 80 -f -1 --num will return 8080 but "svc: HTTP" will return NAGIOS__HTTPPORT if exists, or 80 if not
|
||||||
-u Convert the output to uppercase
|
-u Convert the output to uppercase
|
||||||
-l Convert the output to lowercase
|
-l Convert the output to lowercase
|
||||||
-p Convert / to | (mainly for disk names in NCPA checks)
|
-p Convert / to | (mainly for disk names in NCPA checks)
|
||||||
suffix Take var result and use the it to get NAGIOS__[HOST|SERVICE|CONTACT]<result><suffix>
|
suffix Take var result and use the it to get NAGIOS__[HOST|SERVICE|CONTACT]<result><suffix>
|
||||||
-v Be verbose (only for debugging)
|
-v Be verbose (only for debugging)
|
||||||
|
-g Returns "val" if "val" appears anywhere in the value of var (basically, a grep) and then continues processing as normal
|
||||||
-s The result must match this value or else it will return blank
|
-s The result must match this value or else it will return blank
|
||||||
--num The result will have all non-numbers stripped from it
|
--num The result will have all non-numbers stripped from it
|
||||||
-NP Returns NAGIOS__HOSTNCPAPORT (implied default of 5693)
|
-NP Returns NAGIOS__HOSTNCPAPORT (implied default of 5693)
|
||||||
-N Look for NAGIOS__HOSTNCPA<suffix> (defaults to TOKEN)
|
-N DEPRECATED version of -NT
|
||||||
Can do double lookup if -H is specified directly: -H TOKEN will find NAGIOS__HOSTNCPATOKEN=Level1 which then finds NAGIOS__HOSTLEVEL1TOKEN=Secret
|
-NT Look for NAGIOS__HOSTNCPA<suffix> (defaults to TOKEN)
|
||||||
Or -H PASS will find NAGIOS__HOSTNCPAPASS=Level2 which will then find NAGIOS__HOSTLEVEL2PASS=SuperSecret
|
Can do double lookup if -HT is specified directly: -HT TOKEN will find NAGIOS__HOSTNCPATOKEN=Level1 which then finds NAGIOS__HOSTLEVEL1TOKEN=Secret
|
||||||
|
Or -HT 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.
|
The value will always be trimmed of leading and/or trailing spaces/tabs.
|
||||||
|
|
||||||
@@ -52,15 +45,14 @@ If -F is used, -d is forced to " " and the field number reflects is the first, s
|
|||||||
Ex: Disk check on / 80 90 With -F 1=/, -F 2=80, -F 3=90
|
Ex: Disk check on / 80 90 With -F 1=/, -F 2=80, -F 3=90
|
||||||
|
|
||||||
Examples (service_description is assumed to be "MySQL Check: myDatabase performance"):
|
Examples (service_description is assumed to be "MySQL Check: myDatabase performance"):
|
||||||
|
$0 Return: "MySQL Check: myDatabase performance"
|
||||||
.../env Return: "MySQL Check: myDatabase performance"
|
$0 -n hostName Return: "localhost" (or whatever the name of the host is that ran the check)
|
||||||
.../env -n hostName Return: "localhost" (or whatever the name of the host is that ran the check)
|
$0 -n SERVICESTATE -t 0 Return: current state of the service (0, 1, 2) or 0 if the state is not defined
|
||||||
.../env -n SERVICESTATE -t 0 Return: current state of the service (0, 1, 2) or 0 if the state is not defined
|
$0 -n servicedesc -f 2 Return: "Check" (watch out for defaults!)
|
||||||
.../env -n servicedesc -f 2 Return: "Check" (watch out for defaults!)
|
$0 -n servicedesc -f 4 Return: "myDatabase" (watch out for defaults!)
|
||||||
.../env -n servicedesc -f 4 Return: "myDatabase" (watch out for defaults!)
|
$0 -n servicedesc -d : -f 2 Return: "myDatabase performance"
|
||||||
.../env -n servicedesc -d : -f 2 Return: "myDatabase performance"
|
$0 -n servicedesc -d : -f -2 Return: "MySQL Check" (2nd field from right, split by :)*
|
||||||
.../env -n servicedesc -d : -f -2 Return: "MySQL Check" (2nd field from right, split by :)*
|
$0 -n servicedesc -f -2 -HT pass Return: the value of \$NAGIOS__HOSTNAGIOSXIPASS\$
|
||||||
.../env -n servicedesc -f -2 -H pass Return: the value of \$NAGIOS__HOSTNAGIOSXIPASS\$
|
|
||||||
|
|
||||||
see https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/macrolist.html for possible macro names.
|
see https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/macrolist.html for possible macro names.
|
||||||
*0 for this field will print the entire line
|
*0 for this field will print the entire line
|
||||||
@@ -71,80 +63,119 @@ HELP_EOF
|
|||||||
while [ -n "$1" ]; do
|
while [ -n "$1" ]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-h|--help) do_help;;
|
-h|--help) do_help;;
|
||||||
-n) varName="$2"; shift 2;;
|
-n) myVar[Name]="$2"; shift 2;;
|
||||||
-t) varDefault="$2"; shift 2;;
|
-T) myVar[HostVar]="$2"; shift 2;;
|
||||||
-d) myDelim="$2"; shift 2;;
|
-t) myVar[Default]="$2"; shift 2;;
|
||||||
-f) myField="$2"; shift 2;;
|
-d) myVar[myDelim]="$2"; shift 2;;
|
||||||
-F) myDelim=" "; myField=$(($2-4)); shift 2;;
|
-f) myVar[myField]="$2"; shift 2;;
|
||||||
-H) myHost="$2"; shift 2;;
|
-F) myVar[myDelim]=" "; myVar[myField]=$(($2-4)); shift 2;;
|
||||||
-S) myService="$2"; shift 2;;
|
-HT) myVar[myHT]="$2"; shift 2;;
|
||||||
-C) myContact="$2"; shift 2;;
|
-ST) myVar[myST]="$2"; shift 2;;
|
||||||
-s) myString="$2"; shift 2;;
|
-CT) myVar[myCT]="$2"; shift 2;;
|
||||||
--num) doNumber="true"; shift 1;;
|
-g) myVar[myGrep]="$2"; shift 2;;
|
||||||
-N) doNCPA="true"; shift 1;;
|
-s) myVar[myString]="$2"; shift 2;;
|
||||||
-NP) varName="NAGIOS__HOSTNCPAPORT"; varDefault="5693"; shift 1;;
|
--pre) myvar[myPrefix]="$2"; shift 2;;
|
||||||
-p) doPipe="true"; shift 1;;
|
--post) myVar[myPostfix]="$2"; shift 2;;
|
||||||
-u) doUpper="true"; shift 1;;
|
-N|-NT) myOptions[doNCPA]="true"; shift 1;;
|
||||||
-l) doLower="true"; shift 1;;
|
--num) myOptions[doNumber]="true"; shift 1;;
|
||||||
-v) doVerbose="true"; shift 1;;
|
-NP) myVar[Name]="NAGIOS__HOSTNCPAPORT"; myVar[Default]="5693"; shift 1;;
|
||||||
|
-H) myOptions[doHost]="true"; shift 1;;
|
||||||
|
-S) myOptions[doService]="true"; shift 1;;
|
||||||
|
-C) myOptions[doContact]="true"; shift 1;;
|
||||||
|
-p) myOptions[doPipe]="true"; shift 1;;
|
||||||
|
-u) myOptions[doUpper]="true"; shift 1;;
|
||||||
|
-l) myOptions[doLower]="true"; shift 1;;
|
||||||
|
-v) myOptions[doVerbose]="true"; shift 1;;
|
||||||
*) shift 1;;
|
*) shift 1;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
do_debug() {
|
do_debug() {
|
||||||
[ -n "$doVerbose" ] && echo "DEBUG: $*"
|
[ -n "${myOptions[doVerbose]}" ] && echo "### DEBUG: $1"
|
||||||
}
|
}
|
||||||
|
|
||||||
if [ -n "$doNCPA" ]; then
|
do_lookup() {
|
||||||
do_debug "NCPA mode active. myHost=$myHost"
|
var="$1"; def="$2"
|
||||||
varName=`echo "NAGIOS__HOSTNCPA${myHost:-token}" | tr "a-z" "A-Z"`
|
val=${!var:-$def}
|
||||||
|
echo "$val"
|
||||||
|
}
|
||||||
|
|
||||||
|
do_output() {
|
||||||
|
val="$1"
|
||||||
|
[ -z "$val" ] && return
|
||||||
|
echo "${myVar[myPrefix]}${val}${myVar[myPostfix]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ -n "${myOptions[doNCPA]}" ]; then
|
||||||
|
do_debug "NCPA mode active. myVar[myHT]}=${myVar[myHT]}"
|
||||||
|
myVar[Name]=`echo "NAGIOS__HOSTNCPA${myVar[myHT]:-token}" | tr "a-z" "A-Z"`
|
||||||
else
|
else
|
||||||
do_debug "NCPA mode NOT active."
|
do_debug "NCPA mode NOT active."
|
||||||
varName=`echo "NAGIOS_$varName" | tr "a-z" "A-Z"`
|
do_debug "myOptions[doHost]=${myOptions[doHost]} myOptions[doService]=${myOptions[doService]} myOptions[doContact]=${myOptions[doContact]}"
|
||||||
|
[ -n "${myOptions[doHost]}" ] && myVar[Name]="_HOST${myVar[Name]}"
|
||||||
|
[ -n "${myOptions[doService]}" ] && myVar[Name]="_SERVICE${myVar[Name]}"
|
||||||
|
[ -n "${myOptions[doContact]}" ] && myVar[Name]="_CONTACT${myVar[Name]}"
|
||||||
|
myVar[Name]=`echo "NAGIOS_${myVar[Name]}" | tr "a-z" "A-Z"`
|
||||||
fi
|
fi
|
||||||
do_debug "Looking for $varName..."
|
|
||||||
varValue="${!varName}"
|
do_debug "1: Looking for ${myVar[Name]}..."
|
||||||
do_debug "Got varValue=$varValue"
|
myVar[Value]=`do_lookup "${myVar[Name]}"`
|
||||||
if [ -n "$myField" ]; then
|
do_debug "2: Got myVar[Value]=${myVar[Value]}"
|
||||||
do_debug "myField=$myField"
|
|
||||||
if [ "$myField" -lt 0 ]; then
|
# if -g was specified, then only return the portion of the result that matches
|
||||||
varValue=`echo "$varValue" | rev | awk -F "$myDelim" -v f="${myField:1}" '{print $f}' | rev`
|
if [ -n "${myVar[myGrep]}" ]; then
|
||||||
|
do_debug "1: grepping ${myVar[Name]} for ${myVar[myGrep]}"
|
||||||
|
myVar[Value]=`echo "${myVar[Value]}" | egrep -o -- "${myVar[myGrep]}"`
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "${myVar[myField]}" ]; then
|
||||||
|
do_debug "myVar[myField]=${myVar[myField]}"
|
||||||
|
if [ "${myVar[myField]}" -lt 0 ]; then
|
||||||
|
myVar[Value]=`echo "${myVar[Value]}" | rev | awk -F "${myVar[myDelim]}" -v f="${myVar[myField]:1}" '{print $f}' | rev`
|
||||||
else
|
else
|
||||||
varValue=`echo "$varValue" | awk -F "$myDelim" -v f="$myField" '{print $(f)}'`
|
myVar[Value]=`echo "${myVar[Value]}" | awk -F "${myVar[myDelim]}" -v f="${myVar[myField]}" '{print $(f)}'`
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
varValue=`echo $varValue | sed -e "s/^[ \t]*//" -e "s/[ \t]*$//"`
|
myVar[Value]=`echo ${myVar[Value]} | sed -e "s/^[ \t]*//" -e "s/[ \t]*$//"`
|
||||||
do_debug "After processing varValue=$varValue"
|
do_debug "3: After processing myVar[Value]=${myVar[Value]}"
|
||||||
|
|
||||||
[ -n "$doUpper" ] && varValue=`echo "$varValue" | tr "a-z" "A-Z"`
|
[ -n "${myOptions[doUpper]}" ] && myVar[Value]=`echo "${myVar[Value]}" | tr "a-z" "A-Z"`
|
||||||
[ -n "$doLower" ] && varValue=`echo "$varValue" | tr "A-Z" "a-z"`
|
[ -n "${myOptions[doLower]}" ] && myVar[Value]=`echo "${myVar[Value]}" | tr "A-Z" "a-z"`
|
||||||
|
|
||||||
# Check for myHost
|
# Check for myHT
|
||||||
if [ -n "$myHost" ]; then
|
if [ -n "${myVar[myHT]}" ]; then
|
||||||
myHost=`echo "NAGIOS__HOST${varValue}${myHost}" | tr "a-z" "A-Z"`
|
myVar[myHT]=`echo "NAGIOS__HOST${myVar[Value]}${myVar[myHT]}" | tr "a-z" "A-Z"`
|
||||||
do_debug "myHost=$myHost"
|
do_debug "4: myVar[myHT]=${myVar[myHT]}"
|
||||||
varValue=${!myHost}
|
myVar[Value]=`do_lookup ${myVar[myHT]}`
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check for myService
|
# Check for myST
|
||||||
if [ -n "$myService" ]; then
|
if [ -n "${myVar[myST]}" ]; then
|
||||||
myService=`echo "NAGIOS__SERVICE${varValue}${myService}" | tr "a-z" "A-Z"`
|
myVar[myST]=`echo "NAGIOS__SERVICE${myVar[Value]}${myVar[myST]}" | tr "a-z" "A-Z"`
|
||||||
do_debug "myService=$myService"
|
do_debug "4: myVar[myST]}=${myVar[myST]}"
|
||||||
varValue=${!myService}
|
myVar[Value]=`do_lookup ${myVar[myST]}`
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check for myContact
|
# Check for myCT
|
||||||
if [ -n "$myContact" ]; then
|
if [ -n "${myVar[myCT]}" ]; then
|
||||||
myContact=`echo "NAGIOS__CONTACT${varValue}${myContact}" | tr "a-z" "A-Z"`
|
myVar[myCT]=`echo "NAGIOS__CONTACT${myVar[Value]}${myVar[myCT]}" | tr "a-z" "A-Z"`
|
||||||
do_debug "myContact=$myContact"
|
do_debug "4: myVar[myCT]=${myVar[myCT]}"
|
||||||
varValue=${!myContact}
|
myVar[Value]=`do_lookup "${myVar[myCT]}"`
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[ -n "$myString" -a "$myString" != "$varValue" ] && varValue=""
|
[ -n "${myVar[myString]}" -a "${myVar[myString]}" != "${myVar[Value]}" ] && myVar[Value]=""
|
||||||
[ -n "$doNumber" ] && varValue=`echo $varValue | tr -d -c "0-9"`
|
[ -n "${myOptions[doNumber]}" ] && myVar[Value]=`echo ${myVar[Value]} | tr -d -c "0-9"`
|
||||||
[ -n "$doPipe" ] && varValue=`echo $varValue | tr "/" "|"`
|
[ -n "${myOptions[doPipe]}" ] && myVar[Value]=`echo ${myVar[Value]} | tr "/" "|"`
|
||||||
|
|
||||||
[ -z "$varValue" ] && varValue="$varDefault"
|
do_debug "About to check for -T options because myVar[Value]=${myVar[Value]}"
|
||||||
echo "$varValue"
|
if [ -z "${myVar[Value]}" ]; then
|
||||||
|
do_debug "myVar[Value] is blank, checking for ${myVar[HostVar]}"
|
||||||
|
myVar[Value]=`do_lookup "NAGIOS__HOST${myVar[HostVar]}"`
|
||||||
|
do_debug "received ${myVar[Value]}"
|
||||||
|
[ -z "${myVar[Value]}" ] && myVar[Value]="${myVar[Default]}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Print the result
|
||||||
|
do_output "${myVar[Value]}"
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
|||||||
@@ -52,23 +52,30 @@ test-env: ENV
|
|||||||
@NAGIOS_TEST=$(ENVtest1) ./ENV -n test
|
@NAGIOS_TEST=$(ENVtest1) ./ENV -n test
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "This should give / then 80 then 90 then |"
|
@echo "This should give / then 80 then 90 then |"
|
||||||
@NAGIOS_TEST=$(ENVtest2) ./ENV -n test -F 1
|
NAGIOS_TEST=$(ENVtest2) ./ENV -n test -F 1
|
||||||
@NAGIOS_TEST=$(ENVtest2) ./ENV -n test -F 2
|
NAGIOS_TEST=$(ENVtest2) ./ENV -n test -F 2
|
||||||
@NAGIOS_TEST=$(ENVtest2) ./ENV -n test -F 3
|
NAGIOS_TEST=$(ENVtest2) ./ENV -n test -F 3
|
||||||
@NAGIOS_TEST=$(ENVtest2) ./ENV -n test -F 1 -p
|
NAGIOS_TEST=$(ENVtest2) ./ENV -n test -F 1 -p
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "This should give Check, then myDatabase, then myDatabase performance, then MySQL Check"
|
@echo "This should give Check, then myDatabase, then myDatabase performance, then MySQL Check"
|
||||||
@NAGIOS_TEST=$(ENVtest1) ./ENV -n test -f 2
|
NAGIOS_TEST=$(ENVtest1) ./ENV -n test -f 2
|
||||||
@NAGIOS_TEST=$(ENVtest1) ./ENV -n test -f 4
|
NAGIOS_TEST=$(ENVtest1) ./ENV -n test -f 4
|
||||||
@NAGIOS_TEST=$(ENVtest1) ./ENV -n test -d : -f 2
|
NAGIOS_TEST=$(ENVtest1) ./ENV -n test -d : -f 2
|
||||||
@NAGIOS_TEST=$(ENVtest1) ./ENV -n test -d : -f -2
|
NAGIOS_TEST=$(ENVtest1) ./ENV -n test -d : -f -2
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "This should give Password"
|
@echo "This should give Password"
|
||||||
@NAGIOS_TEST=$(ENVtest1) NAGIOS__HOSTMYDATABASEPASS=Password ./ENV -n test -f -2 -H pass
|
NAGIOS_TEST=$(ENVtest1) NAGIOS__HOSTMYDATABASEPASS=Password ./ENV -n test -f -2 -HT pass
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "Test string is: $(ENVtest)"
|
@echo "Test string is: $(ENVtest3)"
|
||||||
@echo "The first test should be the full test line, then blank line, then number 5, then FourToken"
|
@echo "The first test should be the full test line, then blank line, then number 5, then FourToken twice"
|
||||||
@NAGIOS_TEST=$(ENVtest3) ./ENV -n TeSt
|
NAGIOS_TEST=$(ENVtest3) ./ENV -n TeSt
|
||||||
@NAGIOS_TEST=$(ENVtest3) ./ENV -n TEST -F 2 -S 5
|
NAGIOS_TEST=$(ENVtest3) ./ENV -n TEST -F 2 -S 5
|
||||||
@NAGIOS_TEST=$(ENVtest3) ./ENV -n test -F 2 --num
|
NAGIOS_TEST=$(ENVtest3) ./ENV -n test -F 2 --num
|
||||||
@NAGIOS_TEST=$(ENVtest3) NAGIOS__HOSTFOURTOKEN="FourToken" ./ENV -n test -F 1 -H TOKEN
|
NAGIOS_TEST=$(ENVtest3) NAGIOS__HOSTFOURTOKEN="FourToken" ./ENV -H -n fourtoken
|
||||||
|
NAGIOS_TEST=$(ENVtest3) NAGIOS__HOSTFOURTOKEN="FourToken" ./ENV -n test -F 1 -HT TOKEN
|
||||||
|
@echo ""
|
||||||
|
@echo "This should return Level2"
|
||||||
|
NAGIOS__HOSTNCPAPASS="Level2" ./ENV set.txt -n asdf -T NCPAPASS
|
||||||
|
@echo ""
|
||||||
|
@echo "This should return 91"
|
||||||
|
NAGIOS_TEST="HTTP -S 8091" ./ENV set.txt -n TEST -g "8.*" -d 0 -f 2
|
||||||
|
|||||||
Reference in New Issue
Block a user