#!/bin/sh
#
#___INFO__MARK_BEGIN__
##########################################################################
#
#  The Contents of this file are made available subject to the terms of
#  the Sun Industry Standards Source License Version 1.2
#
#  Sun Microsystems Inc., March, 2001
#
#
#  Sun Industry Standards Source License Version 1.2
#  =================================================
#  The contents of this file are subject to the Sun Industry Standards
#  Source License Version 1.2 (the "License"); You may not use this file
#  except in compliance with the License. You may obtain a copy of the
#  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
#
#  Software provided under this License is provided on an "AS IS" basis,
#  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
#  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
#  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
#  See the License for the specific provisions governing your rights and
#  obligations concerning the Software.
#
#  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
#
#  Copyright: 2001 by Sun Microsystems, Inc.
#
#  All Rights Reserved.
#
##########################################################################
#___INFO__MARK_END__
#
#
#****** util/resources/ibm-loadsensor ***************************************
#
#  NAME
#     ibm-loadsensor -- loadsensor for IBM aix43, aix51
#
#  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/aix51/qloadsensor (if you have another version to
#     $SGE_ROOT/bin/<YOUR_AIX_VERSION>/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
      if [ -f /tmp/svmon-$$.out ]; then
         rm -f /tmp/svmon-$$.out
         touch /tmp/svmon-$$.out
      fi

      $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

      if [ $VERSION = "5.3.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

      # delete output file 
      rm -f /tmp/svmon-$$.out
 
      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

