#!/bin/bash varName="servicedesc" varDefault="" 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 ] [-u] [-H|-S|-C ] Where: 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 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 $}' Examples (service_description is assumed to be "MySQL Check: myDatabase performance"): .../env -n servicedesc 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!) .../env -n servicedesc -f 4 Return: "myDatabase" (watch out for defaults!) .../env -n servicedesc -d : -f 2 Return: "myDatabase performance" .../env -n servicedesc -d : -f -2 Return: "MySQL Check" (2nd field from right, split by :)* .../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. *0 for this field will print the entire line HELP_EOF exit } while [ -n "$1" ]; do case "$1" in -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 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 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