#!/usr/bin/sh
#
# gp_gbl_mem - Munin plug-in to fetch global memory utilisation via Glance
#	      (should only run on HP-UX with GlancePlus (GP) installed therefore)		
#
# Requires:   Separate GP adviser syntax file whose location is made known
#             by ADVISER environment variable which defaults to
#             @@LIBDIR@@/gp_gbl_mem_util.adv.
#
#             Configuration for this plugin may look like this, the example
#             shows the defaults, so only set them to override them.
#             All the environment settings are needed by Glance.
#
#	[gp_*]
#	    env.HOME	    /home/munin
#             Munin users home dir, could also be /var/opt/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
#
#
#	    Note, to avoid parsing problems on start of munin-node
#	    add this entry to your munin-node.conf if you place the *.adv file 
#	    in above mentioned location: "ignore_file \.adv$"
#
#	    The contents of the syntax file for this plugin 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
#	}
#
#
#	    For details about adviser syntax please refer to the documentation in:
#
#	    	/opt/perf/paperdocs/gp/C
#
#
#	    For "installation" of plugin run as root:
#
# @@SBINDIR@@/munin-node-configure --families auto --shell|grep gp_gbl_mem_util|sh -
#
#	    and restart your munin-node
#
# Contributed by <ralph dot grothe at itdz minus berlin dot de>
#
# $Id$
#
#%# family=auto
#%# capabilities=autoconf

PATH=/usr/bin:/usr/sbin
MUNIN_RUN=@@SBINDIR@@/munin-run
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
	    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
