From a83f240ed793e6ae5a27fae75ce029e98e7164d0 Mon Sep 17 00:00:00 2001 From: Eric Loyd Date: Wed, 7 Aug 2024 13:17:09 -0400 Subject: [PATCH] ENV now supports HOST SERVICE and CONTACT custom macro double-decode. --- ENV | 78 ++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 62 insertions(+), 16 deletions(-) diff --git a/ENV b/ENV index e83e1ce..a0e46b5 100755 --- a/ENV +++ b/ENV @@ -1,26 +1,32 @@ #!/bin/bash -varName="novalue" +varName="servicedesc" varDefault="" -myDelim=" " +myDelim="[ :-]" myField="" +myHost="" +myService="" +myContact="" +doUpper="" do_help() { cat << HELP_EOF This utility is designed to be called by the ARGx parameters of a Nagios configuration as follows: - \$\$(\$USER1\$/env -n [-t ] [-f ] [-d ] + \$\$(\$USER1\$/env -n [-t ] [-f ] [-d ] [-u] [-H|-S|-C ] Where: - var is the name of a NAGIOS_variable (see https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/macrolist.html) (case insensitive) - default is a default value if \$NAGIOS_var\$ is blank (case sensitive) - word is an awk-compatible field number (potentially to use with ) - delim is an awk-compatible field delimiter (defaults to space) + var the name of a case-insensitive NAGIOS_variable (defaults to "servicedesc") + default a default value if \$NAGIOS_var\$ is blank (case sensitive) + field an awk-compatible field number (potentially to use with ) + 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] 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 '{print $}' @@ -32,30 +38,70 @@ Examples: Will return "localhost" (or whatever the name of the host is that ran the check) .../env -n SERVICESTATE -t 0 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 - 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 + exit } while [ -n "$1" ]; do 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;; -d) myDelim="$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;; esac done -# If we don't have a config file, then we don't have variables -if [ -f "$NAGIOS_MAINCONFIGFILE" ]; then - varValue="${!varName}" - [ -n "$varValue" -a -n "$myField" ] && varValue=`echo "$varValue" | awk -F "$myDelim" --assign f="$myField" '{print $f}'` +varName=`echo "NAGIOS_$varName" | tr "a-z" "A-Z"` +varValue="${!varName}" +if [ -n "$myField" ]; then + 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 -[ -z "$varValue" ] && varValue="$varDefault" 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" exit 0