From 7cada9d791ff536546abacb2ae8f88b20c3cc0a8 Mon Sep 17 00:00:00 2001 From: Eric Loyd Date: Tue, 6 Aug 2024 14:51:11 -0400 Subject: [PATCH] ENV is now available to pull information about the host/service into the configuration --- ENV | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 ENV diff --git a/ENV b/ENV new file mode 100755 index 0000000..e83e1ce --- /dev/null +++ b/ENV @@ -0,0 +1,61 @@ +#!/bin/bash + +varName="novalue" +varDefault="" +myDelim=" " +myField="" + +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 ] + +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) + +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: + + eche \$NAGIOS_var\$ | awk -F '{print $}' + +Examples: + + .../env -n servicedesc + Will return "Root Partition" (or whatever the service check is named) + .../env -n hostName + 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 -d : -f 2 + If the service name is "MySQL Check: myDatabase" then it will return "myDatabase" (trimmed) + +HELP_EOF +} + +while [ -n "$1" ]; do + case "$1" in + -n) varName=`echo "NAGIOS_$2" | tr "a-z" "A-Z"`; shift 2;; + -t) varDefault="$2"; shift 2;; + -d) myDelim="$2"; shift 2;; + -f) myField="$2"; shift 2;; + *) 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}'` +fi +[ -z "$varValue" ] && varValue="$varDefault" +varValue=`echo $varValue | sed -e "s/^[ \t]*//" -e "s/[ \t]*$//"` + +echo "$varValue" + +exit 0