#!/usr/local/bin/bash
#

# shellcheck disable=SC1117
: <<=cut

=head1 NAME

gp_gbl_mem_util - Munin plugin to fetch global memory utilisation via Glance

=head1 APPLICABLE SYSTEMS

On HP-UX with GlancePlus (GP) installed

=head1 USAGE

Run this command as root user to install the plugin

  munin-node-configure --families auto --shell|grep gp_gbl_mem_util|sh -

and restart your munin-node.

=head2 REQUIRES

Separate GP adviser syntax file whose location is made known
by ADVISER environment variable which defaults to
@@LIBDIR@@/gp_gbl_mem_util.adv.

The contents of the syntax file should look like this:

  if (gbl_interval > 2) then
  {
      print "mem_util.value ",gbl_mem_util|6|2
      print "mem_user.value ",gbl_mem_user_util|6|2
      print "mem_sys.value ",gbl_mem_sys_and_cache_util|6|2
      print "mem_free.value ",gbl_mem_free_util|6|2
  }

=head1 CONFIGURATION

The example shows the defaults, so only set them to override them.
All the environment settings are needed by Glance.

  [gp_*]
      # Munin users home dir, can also be /var/opt/munin
      env.HOME /home/munin
      env.ADVISER_INT 4
      env.ADVISER_ITS 2
      env.GLANCE /opt/perf/bin/glance

  [gp_gbl_mem_util]
      env.ADVISER	    @@LIBDIR@@/gp_gbl_mem_util.adv

=head2 NOTE

If you put the *.adv file  in above mentioned location
add "ignore_file \.adv$" to your /etc/munin/munin-node.conf
to avoid parsing problems on start of munin-node.

=head1 AUTHOR

 Ralph Grothe <ralph dot grothe at itdz minus berlin dot de>

=head1 MAGIC MARKERS

  #%# family=auto
  #%# capabilities=autoconf

=cut

PATH=/usr/bin:/usr/sbin
PLUGIN=${0##*/}

# These variables are required to run Glance
: "${GLANCE:=/opt/perf/bin/glance}"
: "${HOME:=/home/munin}"
: "${ADVISER_INT:=4}"
: "${ADVISER_ITS:=2}"
: "${ADVISER:=@@LIBDIR@@/$PLUGIN.adv}"

export PATH
export HOME

if [[ $1 = autoconf ]]; then
    if [[ $(uname -s) != HP-UX ]]; then
	echo "no (This doesn't seem to be HP-UX but reports as '$(uname -s)')"
	exit 0
    fi
    set +u
    if [ ! -x "$GLANCE" ]; then
	gp_depot=$(swlist -l product|awk '{if(match(tolower($0),"glance")){print$1}}')
	if [[ -n $gp_depot ]]; then
	    # shellcheck disable=SC2086
	    GLANCE=$(swlist -l file $gp_depot|awk '$NF~/bin\/glance$/{print$NF}')
	    [ -n "$GLANCE" ] && [ -x "$GLANCE" ] || exit 1
	else
	    echo "no (Found no glance executable in a standard location)"
	    exit 0
	fi
    fi

    if [ ! -f "$ADVISER" ]; then
	echo "no (Found no adviser command file, please copy $PLUGIN.adv to $ADVISER)"
	exit 0
    fi
    echo yes
    exit 0
fi

if [[ $1 = config ]]; then
    echo "graph_title Glance Global Memory Utilisation"
    echo "graph_category Memory"
    echo "graph_vlabel %"
    echo "graph_args -l 0 -u 100"
    echo "graph_scale no"
    echo "graph_order mem_sys mem_user mem_free mem_util"
    for label in mem_sys mem_user mem_free mem_util; do
	echo "$label.label $label"
	if [[ $label = mem_sys ]]; then
	    echo "$label.draw AREA"
	elif [[ $label = mem_util ]]; then
	    echo "$label.draw LINE2"
	else
	    echo "$label.draw STACK"
	fi
	echo "$label.type GAUGE"
	echo "$label.min 0"
	echo "$label.max 100"
    done
    exit 0
fi

[ -f "$ADVISER" ] || exit 1
j=${ADVISER_INT:-4} i=${ADVISER_ITS:-2}
/opt/perf/bin/glance -j $j -iterations $i -adviser_only -syntax "$ADVISER" 2>/dev/null
