#!/bin/sh
#
# (c) 2002 Sun Microsystems, Inc. Use is subject to license terms.  
#
#
#****** util/resources/ibm-loadsensor ***************************************
#
#  NAME
#     ibm-loadsensor -- loadsensor for IBM aix43
#
#  SYNOPSIS
#     ibm-loadsensor
#
#  FUNCTION
#     Due to copyright issues, the code to retrieve load values like 
#     memory consumption etc. (that can be found in tool like top)
#     cannot be included in the sge_execd as for other platforms.
#     
#     This script can be used as external loadsensor that provides the
#     required data to sge_execd. It uses the svmon utility to retrieve
#     the load and memory values.
#
#     It can be run on the commandline (as user root); to start the sampling
#     of load information, press return; to end the script, enter quit.
#
#     To integrate the loadsensor into sge_execd, either copy it to
#     $SGE_ROOT/bin/aix43/qloadsensor
#     or define it as loadsensor in the hosts cluster configuration, see
#     man page sge_conf(5).
#
#  RESULTS
#     Writes the load values to stdout in a format readable by sge_execd
#     as described in the man page cod_execd(8).
#
#  NOTES
#     The output format of svmon changed between AIX 4.3.2 and 4.3.3,
#     this script tries to read the os version from 
#     /usr/lpp/bos/aix_release.level, which is hopefully a stable interface.
#
#     Anybody from IBM volunteering to share the C-Code to retrieve load
#     and memory values to the Grid Engine project?
#
#***************************************************************************
#

#parsing of 'uptime' output does not work in foreign language mode
unset LANG

ARCH=`$SGE_ROOT/util/arch`
HOST=`$SGE_ROOT/utilbin/$ARCH/gethostname -name`

SVMON="su - root -c /usr/bin/svmon -G"
BLOCKSIZE=4096
BLOCKDIV=`echo "1024 * 1024 / $BLOCKSIZE" | bc`

VERSION=""

if [ -f /usr/lpp/bos/aix_release.level ]; then
   VERSION=`cat /usr/lpp/bos/aix_release.level | cut -c 1-5`
fi

end=false
while [ $end = false ]; do

   # ---------------------------------------- 
   # wait for an input
   #
   read input
   result=$?
   if [ $result != 0 ]; then
      end=true
      break
   fi
   
   if [ "$input" = "quit" ]; then
      end=true
      break
   fi

   # ---------------------------------------- 
   # send mark for begin of load report
   echo "begin"

   # ---------------------------------------- 
   # get load values
   #
   load=`uptime|sed 's/.*load average://'`

   # ---------------------------------------- 
   # send load value load_short
   #
   echo "$HOST:load_short:`echo $load|cut -f1 -d","`"

   # ---------------------------------------- 
   # send load value load_medium
   #
   echo "$HOST:load_medium:`echo $load|cut -f2 -d","`"
   echo "$HOST:load_avg:`echo $load|cut -f2 -d","`"

   # ---------------------------------------- 
   # send load value load_long
   #
   echo "$HOST:load_long:`echo $load|cut -f3 -d","`"

   # ---------------------------------------- 
   # get memory values
   #
   if [ x$VERSION != x ]; then
      $SVMON > /tmp/svmon.out 2>&1
      
      if [ $VERSION = "4.3.2" ]; then
         cat /tmp/svmon.out | tail -1 | read mem_total mem_used mem_free f4 f5 f6 f7 f8 f9 f10 swap_total swap_used
      fi

      if [ $VERSION = "4.3.3" ]; then
         cat /tmp/svmon.out | grep memory | read f1 mem_total mem_used mem_free f5 f6
         cat /tmp/svmon.out | grep "pg space" | read f1 f2 swap_total swap_used
      fi

      if [ $VERSION = "5.1.0" ]; then
         cat /tmp/svmon.out | grep memory | read f1 mem_total mem_used mem_free f5 f6
         cat /tmp/svmon.out | grep "pg space" | read f1 f2 swap_total swap_used
      fi
 
      swap_free=`expr $swap_total - $swap_used`
      virtual_total=`expr $mem_total + $swap_total`
      virtual_used=`expr $mem_used + $swap_used`
      virtual_free=`expr $mem_free + $swap_free`

      mem_total=`echo "scale = 6 ; $mem_total / $BLOCKDIV" | bc`
      mem_used=`echo "scale = 6 ; $mem_used / $BLOCKDIV" | bc`
      mem_free=`echo "scale = 6 ; $mem_free / $BLOCKDIV" | bc`

      swap_total=`echo "scale = 6 ; $swap_total / $BLOCKDIV" | bc`
      swap_used=`echo "scale = 6 ; $swap_used / $BLOCKDIV" | bc`
      swap_free=`echo "scale = 6 ; $swap_free / $BLOCKDIV" | bc`

      virtual_total=`echo "scale = 6 ; $virtual_total / $BLOCKDIV" | bc`
      virtual_used=`echo "scale = 6 ; $virtual_used / $BLOCKDIV" | bc`
      virtual_free=`echo "scale = 6 ; $virtual_free / $BLOCKDIV" | bc`

      # ---------------------------------------- 
      # send memory values
      #
      echo "$HOST:mem_total:${mem_total}M"
      echo "$HOST:mem_used:${mem_used}M"
      echo "$HOST:mem_free:${mem_free}M"

      echo "$HOST:swap_total:${swap_total}M"
      echo "$HOST:swap_used:${swap_used}M"
      echo "$HOST:swap_free:${swap_free}M"

      echo "$HOST:virtual_total:${virtual_total}M"
      echo "$HOST:virtual_used:${virtual_used}M"
      echo "$HOST:virtual_free:${virtual_free}M"
   fi
   # ---------------------------------------- 
   # send mark for end of load report
   echo "end"
done

