#!/bin/sh

# SGE job metrics for Ganglia, e.g. run under cron.
# Assumed to run on qmaster.
# Copyright (C) 2014  Dave Love, University of Liverpool
# Licence: FreeBSD <http://www.gnu.org/licenses/license-list.html#FreeBSD>

. /etc/profile.d/sge.sh

qstat -u \* |
 /usr/bin/awk '
   BEGIN { pending=running=error=0; }
   /^ *[1-9][0-9]/ { if ($5 ~ /^qw/) pending++;
                     if ($5 ~ /[rRt]/) running++;
                     if ($5 ~ /E/) error++; }
   END {print pending, running, error}' |
 { read pending running error
   /usr/bin/gmetric -g SGE --name sge_pending --value $pending --type uint16
   /usr/bin/gmetric -g SGE --name sge_running --value $running --type uint16
   /usr/bin/gmetric -g SGE --name sge_error --value $error --type uint16
 }
