#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_gsp_client - Runs cmds for show tech-support gsp client
#
# April 2009, Caina Wei
#
# Copyright (c) 2009-2012, 2017 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------
. /pkg/bin/show_tech_main_fragment

procname="unknown"
jid=-1
pid=-1

while [ $# -gt 0 ]; do
  case "$1" in
     -L) location="-L $2"; shift 2;;
     -t) __cardtype="$2";  shift 2;;
     -n) procname="$2";  shift 2;;
     -j) jid="$2";  shift 2;;
     *)  default_parser_function "$@"; shift $#;;
  esac
done

if [[ $jid -eq -1 ]]; then
    test_jid=`sysmgr_show -p $procname | grep Job |  cut -f 2 -d ":" | cut -f 2 -d " "`
    test_pid=`sysmgr_show -p $procname | grep PID |  cut -f 2 -d ":" | cut -f 2 -d " "`
    if [[ $test_jid != "" ]]; then
       jid=$test_jid
    fi
    if [[ $test_pid != "" ]]; then
       pid=$test_pid
    fi
else 
    test_pid=`sysmgr_show -p $jid | grep PID |  cut -f 2 -d ":" | cut -f 2 -d " "`
    if [[ $test_pid != "" ]]; then
       pid=$test_pid
    fi
fi

nodename=`uname -n`


#
# system Commands
#      
system_exec[1]='Get group rsi map in Admin plane'
system__ksh[1]='gsp_show -m -a'
system_exec[2]='Get group rsi map in LR plane'
system__ksh[2]='gsp_show -m'
system_exec[3]='Get group rsi map in LR Ctrl plane'
system__ksh[3]='gsp_show -m -l'
  
# client stats
stats_exec[1]="show gsp stats client job $jid"
stats__ksh[1]="gsp_show_api_stats -r $jid"
stats_exec[2]="show gsp stats server jid $jid"
stats__ksh[2]="gsp_show -c $jid"
stats_exec[3]="show gsp stats client memory $pid"
stats__ksh[3]="gsp_show -M -G -Q -D -p $pid"
stats_exec[4]="show gsp trace library gid 0 slow"
stats__ksh[4]="show_ltrace_gsp -G 0 -S"
stats_exec[5]="show gsp trace library gid 0 fast"
stats__ksh[5]="show_ltrace_gsp -G 0 -F"


display() {	
    if [ $jid -eq -1 ] || [ $pid -eq -1 ]; then
        echo "Process $procname jid $jid doesn't exist on $nodename"
    elif [ "$__cardtype" = "SYS" ]; then
        # Collect loggging 
        exec_commands system 
    else
        print_main_heading "show tech-support gsp client jobid $jid"
        	
	# Get the list of groups and then get library traces and stats
	#DDTS: CSCvf09791
	index=1
	for grp in $(gsp_show -c $jid | grep -E "Max DataThresh" | cut -f1 -d " "); do
	 
	    node_cnt=`gsp_show -g $grp | grep "node idx" | wc -l`
            
            groupstats_exec[$index]="show gsp stats server gid $grp"
            groupstats__ksh[$index]="gsp_stats -g $grp -n $node_cnt"
	    groupshow_exec[$index]="show gsp group gid $grp"
	    groupshow__ksh[$index]="gsp_show -g $grp"
	    slowtrace_exec[$index]="show gsp trace library gid $grp slow"
	    slowtrace__ksh[$index]="show_ltrace_gsp -G $grp -S"
	    fasttrace_exec[$index]="show gsp trace library gid $grp fast"
	    fasttrace__ksh[$index]="show_ltrace_gsp -G $grp -F"

	    index=$(($index + 1))
        done

	exec_commands stats
	exec_commands groupstats
	exec_commands groupshow
	exec_commands slowtrace
	exec_commands fasttrace
        print_main_heading "show tech-support gsp client jobid $jid complete"
    fi
}

. /pkg/bin/show_tech_file_fragment
