ENV now supports HOST SERVICE and CONTACT custom macro double-decode.

dev
Eric Loyd 1 year ago
parent 7cada9d791
commit a83f240ed7

76
ENV

@ -1,26 +1,32 @@
#!/bin/bash #!/bin/bash
varName="novalue" varName="servicedesc"
varDefault="" varDefault=""
myDelim=" " myDelim="[ :-]"
myField="" myField=""
myHost=""
myService=""
myContact=""
doUpper=""
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 follows: This utility is designed to be called by the ARGx parameters of a Nagios configuration as follows:
\$\$(\$USER1\$/env -n <var> [-t <default>] [-f <field>] [-d <delim>] \$\$(\$USER1\$/env -n <var> [-t <default>] [-f <field>] [-d <delim>] [-u] [-H|-S|-C <suffix>]
Where: Where:
var is the name of a NAGIOS_variable (see https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/macrolist.html) (case insensitive) var the name of a case-insensitive NAGIOS_variable (defaults to "servicedesc")
default is a default value if \$NAGIOS_var\$ is blank (case sensitive) default a default value if \$NAGIOS_var\$ is blank (case sensitive)
word is an awk-compatible field number (potentially to use with <delim>) field an awk-compatible field number (potentially to use with <delim>)
delim is an awk-compatible field delimiter (defaults to space) delim an awk-compatible field delimiter (defaults to space, dash, and colon)
-u Convert the output to uppercase (useful for further levels of indirection)
suffix Take var result and use the it to get NAGIOS__[HOST|SERVICE|CONTACT]<result><suffix>
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.
If -f is used, then the result will be as if the request were: 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>}' eche \$NAGIOS_var\$ | awk -F <delim> '{print $<field>}'
@ -32,30 +38,70 @@ Examples:
Will return "localhost" (or whatever the name of the host is that ran the check) Will return "localhost" (or whatever the name of the host is that ran the check)
.../env -n SERVICESTATE -t 0 .../env -n SERVICESTATE -t 0
Will return the current state of the service (0, 1, 2) or 0 if the state is not defined Will return the current state of the service (0, 1, 2) or 0 if the state is not defined
.../env -n servicedesc -f 2
If the service name is "MySQL Check: myDatabase performance" then it will return "Check" (watch out for defaults!)
.../env -n servicedesc -f 4
If the service name is "MySQL Check: myDatabase performance" then it will return "myDatabase" (watch out for defaults!)
.../env -n servicedesc -d : -f 2 .../env -n servicedesc -d : -f 2
If the service name is "MySQL Check: myDatabase" then it will return "myDatabase" (trimmed) If the service name is "MySQL Check: myDatabase performance" then it will return "myDatabase performance"
.../env -n servicedesc -d : -f -2
If the service name is "MySQL Check: myDatabase performance" then it will return "MySQL Check" (2nd field from right, split by :)*
.../env -n servicedesc -f -2 -H pass
If the service name is "MySQL Check: myDatabase performance" it will return the value of \$NAGIOS__HOSTNAGIOSXIPASS\$
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
HELP_EOF HELP_EOF
exit
} }
while [ -n "$1" ]; do while [ -n "$1" ]; do
case "$1" in case "$1" in
-n) varName=`echo "NAGIOS_$2" | tr "a-z" "A-Z"`; shift 2;; -h|--help) do_help;;
-n) varName="$2"; shift 2;;
-t) varDefault="$2"; shift 2;; -t) varDefault="$2"; shift 2;;
-d) myDelim="$2"; shift 2;; -d) myDelim="$2"; shift 2;;
-f) myField="$2"; shift 2;; -f) myField="$2"; shift 2;;
-H) myHost="$2"; shift 2;;
-S) myService="$2"; shift 2;;
-C) myContact="$2"; shift 2;;
-u) doUpper="true"; shift 1;;
*) shift 1;; *) shift 1;;
esac esac
done done
# If we don't have a config file, then we don't have variables varName=`echo "NAGIOS_$varName" | tr "a-z" "A-Z"`
if [ -f "$NAGIOS_MAINCONFIGFILE" ]; then varValue="${!varName}"
varValue="${!varName}" if [ -n "$myField" ]; then
[ -n "$varValue" -a -n "$myField" ] && varValue=`echo "$varValue" | awk -F "$myDelim" --assign f="$myField" '{print $f}'` if [ "$myField" -lt 0 ]; then
varValue=`echo "$varValue" | rev | awk -F "$myDelim" -v f="${myField:1}" '{print $f}' | rev`
else
varValue=`echo "$varValue" | awk -F "$myDelim" -v f="$myField" '{print $(f)}'`
fi
fi fi
[ -z "$varValue" ] && varValue="$varDefault"
varValue=`echo $varValue | sed -e "s/^[ \t]*//" -e "s/[ \t]*$//"` varValue=`echo $varValue | sed -e "s/^[ \t]*//" -e "s/[ \t]*$//"`
[ -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"`
varValue=${!myHost}
fi
# Check for myService
if [ -n "$myService" ]; then
myService=`echo "NAGIOS__SERVICE${varValue}${myService}" | tr "a-z" "A-Z"`
varValue=${!myService}
fi
# Check for myContact
if [ -n "$myContact" ]; then
myContact=`echo "NAGIOS__CONTACT${varValue}${myContact}" | tr "a-z" "A-Z"`
varValue=${!myContact}
fi
[ -z "$varValue" ] && varValue="$varDefault"
echo "$varValue" echo "$varValue"
exit 0 exit 0

Loading…
Cancel
Save