#!/bin/bash
#
# Wildcard-plugin to monitor Sun Grid Engine queue state.
# To monitor a queue, link sge_queue_<queue> to this file. E.g.
#
#   ln -s /usr/share/munin/plugins/sge_queue_ /etc/munin/plugins/sge_queue_all.q
#
# Based on the condor_queue_ plugin by Šarūnas Burdulis.
#
# Runs 'qstat -g c' for the specified queue and gets the number of
# used, reserved, unavailable and free queue slots.
# Free slots are defined as available slots minus reserved slots.
# Unavailable slots have queue states Disabled/Suspended/Error/Ambiguous config/Unknown
#
# Parameters understood:
#
#   config   (required)
#   autoconf (optional - used by munin-config)
#   suggest  (optional - used by munin-config)
#
# Configurable variables:
#
#   env.sge_settings - Path to SGE settings.sh script, defaults to /opt/sge/default/common/settings.sh
#   env.title        - Graph title, overrides "SGE Queue state".
#   env.options      - Additional command line options to qstat. 
#
# Revisions:
#   v1.0 2009-07-19
#
# Magic markers
#%# family=contrib
#%# capabilities=autoconf suggest

# env.sge_settings -- alternatively you can just modify the path below
if [ ! -z "$sge_settings" ]; then
    SGE_SETTINGS="$sge_settings"
else
    SGE_SETTINGS=/opt/sge/default/common/settings.sh
fi

# queue name in symlink
QUEUE=`basename $0 | sed 's/^sge_queue_//g'`
if [ -z "$QUEUE" ]; then
    QUEUE="all.q"
fi

# env.title
if [ ! -z "$title" ]; then
    GRAPHTITLE="$title"
else
    GRAPHTITLE="SGE Queue state (${QUEUE})"
fi

if [ "$1" = "config" ]; then
    echo "graph_title "$GRAPHTITLE""
    echo "graph_order used reserved unavailable free"
    echo "graph_args --lower-limit 0 "
    echo "graph_vlabel Queue slots"
    echo "graph_scale no"
    echo "graph_info Shows global Sun Grid Engine queue state."
    echo "graph_category SGE"
    echo "graph_period minute"
    echo "used.label Used"
    echo "used.draw AREA"
    echo "used.type GAUGE"
    echo "used.info Currently used slots."
    echo "unavailable.label Unavailable"
    echo "unavailable.draw STACK"
    echo "unavailable.type GAUGE"
    echo "unavailable.info Queue state Disabled/Suspended/Error/Ambiguous config/Unknown."
    echo "reserved.label Reserved"
    echo "reserved.draw STACK"
    echo "reserved.type GAUGE"
    echo "reserved.info Reserved slots."
    echo "free.label Free"
    echo "free.draw STACK"
    echo "free.type GAUGE"
    echo "free.info Free slots."
    exit 0
fi

# source settings script, needed for qstat
if [ -f "$SGE_SETTINGS" ]; then
    source "$SGE_SETTINGS"
fi

if [ "$1" = "suggest" ]; then
    qstat -g c | tail -n +3 | cut -d ' ' -f 1
    exit 0
fi

if [ "$1" = "autoconf" ]; then
    if which qstat > /dev/null; then
        echo "yes"
        exit 0
    else 
        echo "no"
        exit 1
    fi
fi

# env.options
OPTIONS="-g c -q $QUEUE $options"

# qstat -g c example output: 
# CLUSTER QUEUE                   CQLOAD   USED    RES  AVAIL  TOTAL aoACDS  cdsuE 
# --------------------------------------------------------------------------------
# all.q                             0.00     16      8     48     64      0      0
qstat $OPTIONS | tail -n 1 | awk '{print "unavailable.value " $8 "\nreserved.value " $4 "\nused.value " $3 "\nfree.value " $5-$4}'

# for SGE 6.1 or earlier use this:
# qstat $OPTIONS | tail -n 1 | awk '{print "unavailable.value " $7 "\nreserved.value 0"  "\nused.value " $3 "\nfree.value " $4}'
