#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_ds - Runs show commands for show tech-support ds
#
#  Copyright (c) 2011-2015, 2017-2019 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------
. /pkg/bin/show_tech_main_fragment

__cardtype="unspecified"
ctrace_directory="/var/log/ctrace"
decode_directory="/pkg/ctrace"

# Parse the arguments to the script.
while [ "$#" -gt "0" ]; do 
    case "$1" in
        -t) __cardtype="$2"; shift 2;; 
        *)  default_parser_function "$@"; shift $#;;
    esac
done

if [ "$__filename" = "unspecified" ]; then
    echo "show_tech_ds_fast: output file not specified"
    exit
fi

if [ "$__cardtype" == "unspecified" ]; then
    __cardtype=`node_type`
fi

# ***********************************************************
#  Show commands to be run by the show tech-support commands
# ***********************************************************

# Commands that run on once per system

sys_exec[1]="show platform"
sys__ksh[1]='show_platform_sysdb'
sys_exec[2]="show platform vm"
sys__ksh[2]='show_platform_sysdb -v'
sys_exec[3]="show redundancy"
sys__ksh[3]='redcon_show'
sys_exec[4]="show logging" 
sys__ksh[4]='show_logging'

#############################################################
# Commands that run on all SCs & RPs
#ds

#rp_exec[1]='List of inventory DB clients'
#rp__ksh[1]='/pkg/bin/test_inv_svc_client 1 20 3'

#############################################################
# Commands that run on all LCs
#ds

#lc_exec[1]='List of inventory DB clients'
#lc__ksh[1]='/pkg/bin/test_inv_svc_client 1 20 3'

#############################################################
# Commands that run on all nodes

all_exec[1]="run pstack `pidof ds`"
all__ksh[1]="pstack `pidof ds`"
all_exec[2]="show proc dsr"
all__ksh[2]='sysmgr_show -p dsr'
all_exec[3]="run /pkg/bin/ds_test_client -d"
all__ksh[3]='ds_test_client -d'
all_exec[4]="run check_fds.sh ds" 
all__ksh[4]='check_fds.sh ds'
all_exec[5]="run dsrtc dump" 
all__ksh[5]='dsrtc dump'


##############################################################

display() {
    print_main_heading "show tech-support ds location $ext_node_name"

    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
    else
        case "$__cardtype" in
        "SC")
            exec_commands rp
            ;;

        "RP")
            exec_commands rp
            ;;
        "LC")
            exec_commands lc
            ;;
        esac

        exec_commands all

    fi

    print_main_heading "show tech-support ds location $ext_node_name complete"
}

collect_ctrace_data() {
    print_main_heading "ctrace tech-support info ds"

    if [ "$__cardtype" != "SYS" ]; then
        cd $__tar_file_directory_on_node
        if [[ $? != 0 ]]; then
            echo "ABORT: cd to showtech directory is not successful"
            return
        fi

        find /var/log/ctrace/ds -name *.ctrl | xargs touch > /dev/null 2>&1
        if [[ $? != 0 ]]; then
            echo "updating buffer with ds trace files not successful"
        fi

        find /var/log/ctrace/dsr -name *.ctrl | xargs touch > /dev/null 2>&1
        if [[ $? != 0 ]]; then
            echo "updating buffer with dsr trace files not successful"
        fi

        find /var/log/ctrace/processmgr -name *.ctrl | xargs touch > /dev/null 2>&1
        if [[ $? != 0 ]]; then
            echo "updating buffer with processmgr trace files not successful"
        fi

        sleep 2

        node_name=`uname -n`

        tar -zcPf ctrace_"$node_name".tgz -b 1024 "$ctrace_directory"
        if [[ $? != 0 ]]; then
            echo "ABORT: Tar of ctrace directory is not successful"
            return
        fi

        tar -zhcPf decode_"$node_name".tgz -b 1024 "$decode_directory"
        if [[ $? != 0 ]]; then
            echo "tar of decode binaries is not successful"
        fi

    fi

}

collect_ctrace_data

# Run the appropriate function depending on the node specified and if a
# file is specified write the output to that file. We need to redirect
# stderr to stdout when writing to a file because some of the show
# commands output to stderr instead of stdout
. /pkg/bin/show_tech_file_fragment
