#!/bin/sh

# Copyright  2005 Sun Microsystems, Inc.  All rights reserved.
#
# Sun Microsystems, Inc. has intellectual property rights relating to
# technology embodied in the product that is described in this document.
# In particular, and without limitation, these intellectual property rights
# may include one or more of the U.S. patents listed at
# http://www.sun.com/patents and one or more additional patents or pending
# patent applications in the U.S. and in other countries.
#
# U.S. Government Rights - Commercial software.  Government users are subject
# to the Sun Microsystems, Inc. standard license agreement and applicable
# provisions of the FAR and its supplements.
#
# Use is subject to license terms.
#
# This distribution may include materials developed by third parties.Sun,
# Sun Microsystems and  the Sun logo are trademarks or registered trademarks
# of Sun Microsystems, Inc. in the U.S. and other countries.  
#
# Copyright  2005 Sun Microsystems, Inc. Tous droits rservs.
# Sun Microsystems, Inc. dtient les droits de proprit intellectuels relatifs
#  la technologie incorpore dans le produit qui est dcrit dans ce document.
# En particulier, et ce sans limitation, ces droits de proprit
# intellectuelle peuvent inclure un ou plus des brevets amricains lists
#  l'adresse http://www.sun.com/patents et un ou les brevets supplmentaires
# ou les applications de brevet en attente aux Etats - Unis et dans les
# autres pays.
#
# L'utilisation est soumise aux termes du contrat de licence.
#
# Cette distribution peut comprendre des composants dvelopps par des
# tierces parties.
#
# Sun,  Sun Microsystems et  le logo Sun sont des marques de fabrique ou des
# marques dposes de Sun Microsystems, Inc. aux Etats-Unis et dans
# d'autres pays.


#############################################################################
OSVERSION=`/bin/uname -r`

tuneEtcSystem() {
    #Solaris Kernel Tuning

    tune_file="/etc/system"

    $ECHO $LINE_SEP
    $ECHO "Solaris Kernel Tuning..."
    $ECHO
    $ECHO "File                 : $tune_file"
    $ECHO "Parameter tuning     :"
    $ECHO
    $ECHO "1.   rlim_fd_max"
    $ECHO "Current Value        :   rlim_fd_max=`getEntry 'rlim_fd_max=' $tune_file`"
    $ECHO "Recommended Value    :   rlim_fd_max=$AMTUNE_NUM_FILE_DESCRIPTORS"
    $ECHO
    $ECHO "2.   rlim_fd_cur"
    $ECHO "Current Value        :   rlim_fd_cur=`getEntry 'rlim_fd_cur=' $tune_file`"
    $ECHO "Recommended Value    :   rlim_fd_cur=$AMTUNE_NUM_FILE_DESCRIPTORS"
    $ECHO

if [ "$OSVERSION" = "5.9" ] || [ "$OSVERSION" = "5.8" ]; then
    
    $ECHO "3.   tcp:tcp_conn_hash_size=$AMTUNE_NUM_TCP_CONN_SIZE"
    $ECHO "Current Value        :   tcp:tcp_conn_hash_size=`getEntry 'tcp:tcp_conn_hash_size=' $tune_file`"
    $ECHO "Recommended Value    :   tcp:tcp_conn_hash_size=$AMTUNE_NUM_TCP_CONN_SIZE"
    $ECHO
fi
    $ECHO

    if [ "$AMTUNE_MODE" = "REVIEW" ]; then
        return
    fi

    check_file_for_write $tune_file
    if [ $? = 100 ]; then
        return
    fi

    delete_line $tune_file "* Start: Access Manager Tuning"
    delete_line $tune_file "rlim_fd_max"
    delete_line $tune_file "rlim_fd_cur"
    delete_line $tune_file "sq_max_size"
    delete_line $tune_file "tcp:tcp_conn_hash_size"
    delete_line $tune_file "* End: Access Manager Tuning"

    add_to_end $tune_file "* Start: Access Manager Tuning : `date`"
    add_to_end $tune_file "set rlim_fd_max=$AMTUNE_NUM_FILE_DESCRIPTORS"
    add_to_end $tune_file "set rlim_fd_cur=$AMTUNE_NUM_FILE_DESCRIPTORS"
    
if [ "$OSVERSION" = "5.9" ] || [ "$OSVERSION" = "5.8" ]; then

    add_to_end $tune_file "set tcp:tcp_conn_hash_size=$AMTUNE_NUM_TCP_CONN_SIZE"
fi

    add_to_end $tune_file "* End: Access Manager Tuning"

}

tuneTCP() {
    #Solaris TCP Tuning

    tune_file="/etc/rc2.d/S71ndd_tcp"

    #backup the file if it already exists. amutils does this, but the orig file (S71ndd_tcp-orig-<pid>)
    #take precedence over the S71ndd_tcp file
    if [ -f $tune_file ]; then
        base_file_name=`$BASENAME $tune_file`
        dir_name=`$DIRNAME $tune_file`
        $CP $tune_file $dir_name/orig-$$-$base_file_name
    fi

    $ECHO $LINE_SEP
    $ECHO "Solaris TCP Tuning using ndd..."
    $ECHO
    $ECHO "File                 : $tune_file"
    $ECHO "Parameter tuning     :"
    $ECHO
    $ECHO "1.   /dev/tcp tcp_fin_wait_2_flush_interval"
    $ECHO "Current Value        :   /dev/tcp tcp_fin_wait_2_flush_interval `$NDD -get /dev/tcp tcp_time_wait_interval`"
    $ECHO "Recommended Value    :   /dev/tcp tcp_fin_wait_2_flush_interval 67500"
    $ECHO
    $ECHO "2.   /dev/tcp tcp_conn_req_max_q"
    $ECHO "Current Value        :   /dev/tcp tcp_conn_req_max_q `$NDD -get /dev/tcp tcp_conn_req_max_q`"
    $ECHO "Recommended Value    :   /dev/tcp tcp_conn_req_max_q $AMTUNE_NUM_TCP_CONN_SIZE"
    $ECHO
    $ECHO "3.   /dev/tcp tcp_conn_req_max_q0"
    $ECHO "Current Value        :   /dev/tcp tcp_conn_req_max_q0 `$NDD -get /dev/tcp tcp_conn_req_max_q0`"
    $ECHO "Recommended Value    :   /dev/tcp tcp_conn_req_max_q0 $AMTUNE_NUM_TCP_CONN_SIZE"
    $ECHO
    $ECHO "4.   /dev/tcp tcp_keepalive_interval"
    $ECHO "Current Value        :   /dev/tcp tcp_keepalive_interval `$NDD -get /dev/tcp tcp_keepalive_interval`"
    $ECHO "Recommended Value    :   /dev/tcp tcp_keepalive_interval 90000"
    $ECHO
    $ECHO "5.  /dev/tcp tcp_smallest_anon_port"
    $ECHO "Current Value        :   /dev/tcp tcp_smallest_anon_port `$NDD -get /dev/tcp tcp_smallest_anon_port`"
    $ECHO "Recommended Value    :   /dev/tcp tcp_smallest_anon_port 1024"
    $ECHO
    $ECHO "6.  /dev/tcp tcp_slow_start_initial"
    $ECHO "Current Value        :   /dev/tcp tcp_slow_start_initial `$NDD -get /dev/tcp tcp_slow_start_initial`"
    $ECHO "Recommended Value    :   /dev/tcp tcp_slow_start_initial 2"
    $ECHO
    $ECHO "7.  /dev/tcp tcp_xmit_hiwat"
    $ECHO "Current Value        :   /dev/tcp tcp_xmit_hiwat `$NDD -get /dev/tcp tcp_xmit_hiwat`"
    $ECHO "Recommended Value    :   /dev/tcp tcp_xmit_hiwat $AMTUNE_NUM_FILE_DESCRIPTORS"
    $ECHO
    $ECHO "8.  /dev/tcp tcp_recv_hiwat"
    $ECHO "Current Value        :   /dev/tcp tcp_recv_hiwat `$NDD -get /dev/tcp tcp_recv_hiwat`"
    $ECHO "Recommended Value    :   /dev/tcp tcp_recv_hiwat $AMTUNE_NUM_FILE_DESCRIPTORS"
    $ECHO
    $ECHO "9.  /dev/tcp tcp_ip_abort_cinterval"
    $ECHO "Current Value        :   /dev/tcp tcp_ip_abort_cinterval `$NDD -get /dev/tcp tcp_ip_abort_cinterval`"
    $ECHO "Recommended Value    :   /dev/tcp tcp_ip_abort_cinterval 10000"
    $ECHO
    $ECHO "10.  /dev/tcp tcp_deferred_ack_interval"
    $ECHO "Current Value        :   /dev/tcp tcp_deferred_ack_interval `$NDD -get /dev/tcp tcp_deferred_ack_interval`"
    $ECHO "Recommended Value    :   /dev/tcp tcp_deferred_ack_interval 5"
    $ECHO
    $ECHO "11.  /dev/tcp tcp_strong_iss"
    $ECHO "Current Value        :   /dev/tcp tcp_strong_iss `$NDD -get /dev/tcp tcp_strong_iss`"
    $ECHO "Recommended Value    :   /dev/tcp tcp_strong_iss 2"
    $ECHO
    $ECHO

    if [ "$AMTUNE_MODE" = "REVIEW" ]; then
        return
    fi

    if [ ! -f $tune_file ]; then
        touch $tune_file
    fi

    check_file_for_write $tune_file
    if [ $? = 100 ]; then
        return
    fi

    delete_line $tune_file "# Start: Access Manager Tuning"
    delete_line $tune_file "tcp_time_wait_interval"
    delete_line $tune_file "tcp_fin_wait_2_flush_interval"
    delete_line $tune_file "tcp_conn_req_max_q"
    delete_line $tune_file "tcp_conn_req_max_q0"
    delete_line $tune_file "tcp_ip_abort_interval"
    delete_line $tune_file "tcp_keepalive_interval"
    delete_line $tune_file "tcp_rexmit_interval_max"
    delete_line $tune_file "tcp_rexmit_interval_min"
    delete_line $tune_file "tcp_rexmit_interval_initial"
    delete_line $tune_file "tcp_smallest_anon_port"
    delete_line $tune_file "tcp_slow_start_initial"
    delete_line $tune_file "tcp_xmit_hiwat"
    delete_line $tune_file "tcp_recv_hiwat"
    delete_line $tune_file "tcp_ip_abort_cinterval"
    delete_line $tune_file "tcp_ip_abort_interval"
    delete_line $tune_file "tcp_deferred_ack_interval"
    delete_line $tune_file "tcp_strong_iss"
    delete_line $tune_file "# End: Access Manager Tuning"


    add_to_end $tune_file "# Start: Access Manager Tuning : `date`"
    add_to_end $tune_file "ndd -set /dev/tcp tcp_fin_wait_2_flush_interval 67500"
    add_to_end $tune_file "ndd -set /dev/tcp tcp_conn_req_max_q $AMTUNE_NUM_TCP_CONN_SIZE"
    add_to_end $tune_file "ndd -set /dev/tcp tcp_conn_req_max_q0 $AMTUNE_NUM_TCP_CONN_SIZE"
    add_to_end $tune_file "ndd -set /dev/tcp tcp_keepalive_interval 90000"
    add_to_end $tune_file "ndd -set /dev/tcp tcp_smallest_anon_port 1024"
    add_to_end $tune_file "ndd -set /dev/tcp tcp_slow_start_initial 2"
    add_to_end $tune_file "ndd -set /dev/tcp tcp_xmit_hiwat $AMTUNE_NUM_FILE_DESCRIPTORS"
    add_to_end $tune_file "ndd -set /dev/tcp tcp_recv_hiwat $AMTUNE_NUM_FILE_DESCRIPTORS"
    add_to_end $tune_file "ndd -set /dev/tcp tcp_ip_abort_cinterval 10000"
    add_to_end $tune_file "ndd -set /dev/tcp tcp_deferred_ack_interval 5"
    add_to_end $tune_file "ndd -set /dev/tcp tcp_strong_iss 2"
    add_to_end $tune_file "# End: Access Manager Tuning"

    $RM $tune_file-orig-$$
    
    #source the tcp tune file so that the settings are visible immediately
    . $tune_file


}
#############################################################################
# Start of main program
#############################################################################
SCRIPT_LOCATION=`dirname $0`

# import the environment
if [ -f $SCRIPT_LOCATION/amtune-env ]; then
    if [ ! "$INIT_STATUS" = "INIT_COMPLETE" ]; then
        . $SCRIPT_LOCATION/amtune-env
    fi
fi

$ECHO "Solaris Tuning Script"                           1>>$DEBUG_FILE 2>&1

tuneEtcSystem                                           1>>$DEBUG_FILE 2>&1

tuneTCP                                                 1>>$DEBUG_FILE 2>&1

$ECHO $PARA_SEP                                         1>>$DEBUG_FILE 2>&1

