#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lpp/bosinst/samples/AE/AS/tuneWASProfile.sh 1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2008,2009 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

# @(#)44        1.1  src/bos/usr/lpp/bosinst/samples/AE/AS/tuneWASProfile.sh, bosinst, bos720 3/29/09 15:20:33

#
# A utility for tuning a WAS profile.
#
# usage: sh tuneWASProfile  <parameters>

# source include for debug output
. /opt/IBM/AE/AS/debugWAS

WAS=/opt/IBM/WebSphere
WAS_HOME=$WAS/AppServer
WAS_PROFILES=$WAS/Profiles

touch $CONFIGWAS7/.started

echodebug "Tuning the profile configuration"
#WAS_PASSWORD=`cat $WAS_PROFILES/.ibm/password`
#WAS_USERNAME=`cat $WAS_PROFILES/.ibm/username`
INIT=_was.init
cd $WAS_HOME

CONFIG_PY=/tmp/config.py
MIN_RAM=512
profile_num=0
THREADS_PER_CPU=10
HEAP_RATIO=0.5

while [ $# -ne 0 ]
do
	case $1 in
		-type*)
		type=$2
		;;        
		-profile_num*)
		profile_num=$2
		;;
		-cell_name*)
		CELL_NAME=$2
		;;
		-node_name*)
		NODE_NAME=$2
		;;
		-threads_per_cpu*)
		THREADS_PER_CPU=$2
		;;
		-heap_ratio*)
		HEAP_RATIO=$2
		;;
		*)
		;;
	esac
	shift 2
done

#get system information for tuning

if [ "$UNAME" = "AIX" ]
then
    NUM_CPU=`lsdev -c processor -s sys -S a -r name | wc -l`
    AMT_MEM=0
    for m in `lsdev -c memory -t totmem -s sys -S a -r name`
    do
	n=`lsattr -l $m -a size -F value`
	let "AMT_MEM += n"
    done
else
    NUM_CPU=`cat /proc/cpuinfo | grep processor | wc -l`
    AMT_MEM=`cat /proc/meminfo | grep MemTotal | awk ' {print $2;}'` #in KBs
fi

#echo all parameters
if [ $type ] ; then 
   echodebug "type=$type"
fi
if [ $profile_num ] ; then 
   echodebug "profile_num=$profile_num"
fi




if [ $type == 'adminagent' ] ; then

   echodebug "Tuning administrative agent registered server profiles"
   
   THREADS=$(( $NUM_CPU*$THREADS_PER_CPU ))
   if [ $profile_num -eq 1 ] ; then
      HEAP_SIZE=$( echo $AMT_MEM $HEAP_RATIO | awk ' { printf("%.0f", $1 / 1024 * $2) }' ) #in MBs
   else 
      HEAP_SIZE=$( echo $AMT_MEM $profile_num | awk ' { printf("%.0f", ($1 / 1024 - 256) / $2) }' ) #in MBs
   fi

   # tune any app servers defined 
   profile_index=1
   while [ $profile_index -le $profile_num ]
   do 
         
      echodebug "Tune AA0$profile_index"
      
      PROF_NAME=DefaultAppSrvAA0$profile_index
      CELL_NAME_AA=$CELL_NAME"AA0"$profile_index
      NODE_NAME_AA=$NODE_NAME"AA0"$profile_index
      SERVER_NAME=server1   
      
      
      touch $CONFIG_PY
      echo "AdminConfig.modify('(cells/$CELL_NAME_AA/nodes/$NODE_NAME_AA/servers/$SERVER_NAME|server.xml#ThreadPool_1183122130079)', '[[maximumSize \"$THREADS\"] [minimumSize \"$THREADS\"]]')" > $CONFIG_PY
      cat $HEAP_SIZE
      if (( $HEAP_SIZE >= $MIN_RAM )) ; then
         echo "AdminConfig.modify('(cells/$CELL_NAME_AA/nodes/$NODE_NAME_AA/servers/$SERVER_NAME|server.xml#JavaVirtualMachine_1183122130078)', '[[initialHeapSize \"$HEAP_SIZE\"] [maximumHeapSize \"$HEAP_SIZE\"]]')" >> $CONFIG_PY
      fi

      echo "AdminConfig.save()" >> $CONFIG_PY
      
      CMD="/opt/IBM/WebSphere/Profiles/$PROF_NAME/bin/wsadmin.sh -conntype NONE -lang jython -f $CONFIG_PY"
      $CMD
      rm $CONFIG_PY
      let profile_index=$profile_index+1
   done 
   
fi

if [ $type == 'cell' ] ; then
   
   echodebug "Tuning the cell application server"
   # Only one service per node, so we start the nodeagent with the service.
   # Customer can set server1 to autostart via the nodeagent
   PROF_NAME=DefaultCellAppSrv01
   

   NODE_NAME_AA=$NODE_NAME"AppSrvNode"
   SERVER_NAME=server1
   
   THREADS=$(( $NUM_CPU*$THREADS_PER_CPU ))
   HEAP_SIZE=$( echo $AMT_MEM $HEAP_RATIO | awk ' { printf("%.0f", $1 / 1024 * $2) }' ) #in MBs

   touch $CONFIG_PY
   #ThreadPool_1212628649060
   #JavaVirtualMachine_1212628649062
   echo "AdminConfig.modify('(cells/$CELL_NAME/nodes/$NODE_NAME_AA/servers/$SERVER_NAME|server.xml#ThreadPool_1212628649060)', '[[maximumSize \"$THREADS\"] [minimumSize \"$THREADS\"]]')" >> $CONFIG_PY
   if (( $HEAP_SIZE >= $MIN_RAM )) ; then
      echo "AdminConfig.modify('(cells/$CELL_NAME_AA/nodes/$NODE_NAME_AA/servers/$SERVER_NAME|server.xml#JavaVirtualMachine_1183122130078)', '[[initialHeapSize \"$HEAP_SIZE\"] [maximumHeapSize \"$HEAP_SIZE\"]]')" >> $CONFIG_PY
   fi
   echo "AdminConfig.save()" >> $CONFIG_PY
   
   
   #set profile for Dmgr Side
   echodebug "Tuning the cell deployment manager"
   PROF_NAME=DefaultCellDmgr01
   CMD="/opt/IBM/WebSphere/Profiles/$PROF_NAME/bin/wsadmin.sh -verbose -conntype NONE -lang jython -f $CONFIG_PY"
   $CMD
     
   rm $CONFIG_PY

fi

if [ $type == 'default' ] ; then
   echodebug "Tuning the default application server"
   
   PROF_NAME=DefaultAppSrv01
   SERVER_NAME=server1


   THREADS=$(( $NUM_CPU*$THREADS_PER_CPU ))
   HEAP_SIZE=$( echo $AMT_MEM $HEAP_RATIO | awk ' { printf("%.0f", $1 / 1024 * $2) }' ) #in MBs
  
   touch $CONFIG_PY
   echo "AdminConfig.modify('(cells/$CELL_NAME/nodes/$NODE_NAME/servers/server1|server.xml#ThreadPool_1183122130079)', '[[maximumSize \"$THREADS\"] [minimumSize \"$THREADS\"]]')" >> $CONFIG_PY
   if (( $HEAP_SIZE >= $MIN_RAM )) ; then
      echo "AdminConfig.modify('(cells/$CELL_NAME_AA/nodes/$NODE_NAME_AA/servers/$SERVER_NAME|server.xml#JavaVirtualMachine_1183122130078)', '[[initialHeapSize \"$HEAP_SIZE\"] [maximumHeapSize \"$HEAP_SIZE\"]]')" >> $CONFIG_PY
   fi
   echo "AdminConfig.save()" >> $CONFIG_PY
   

   CMD="/opt/IBM/WebSphere/Profiles/$PROF_NAME/bin/wsadmin.sh -conntype NONE -lang jython -f $CONFIG_PY"
   $CMD
   rm $CONFIG_PY

fi

echodebug "Done tuning the profile configuration"
