#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_sdr_mgbl - Runs show commands for show tech-support sdr_mgbl_proxy
#
# Copyright (c) 2011-2020 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_sdr_mgbl_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'
sys_exec[5]="show platform debug"
sys__ksh[5]='show_platform_sysdb -d'
sys_exec[6]="show diag debug"
sys__ksh[6]='show_diag -d'
sys_exec[7]="show inventory"
sys__ksh[7]='show_inventory'
sys_exec[8]="show inventory raw [debug]"
sys__ksh[8]='show_inventory -r -d'
sys_exec[9]="show version"
sys__ksh[9]='ng_show_version'
sys_exec[10]="show process blocked"
sys__ksh[10]='sh_proc_ng_blocked'
sys_exec[11]="show install active"
sys__ksh[11]='sdr_instcmd show install active'
sys_exec[12]="show placement program sdr_mgbl_proxy"
sys__ksh[12]='placed_show -p sdr_mgbl_proxy'

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

rp_exec[1]="show lrd trace all"
rp__ksh[1]='show_liblrd_trace -F all'
rp_exec[2]="show sdr detail"
rp__ksh[2]='show_sdr_sysdb -m'
rp_exec[3]='run /pkg/bin/show_sdr_mgbl_debug'
rp__ksh[3]='/pkg/bin/show_sdr_mgbl_debug'
rp_exec[4]="show sdr-invmgr trace all"
rp__ksh[4]='show_sdr_invmgr_trace -F all'
rp_exec[5]="show sdr-invmgr object-dump file"
rp__ksh[5]='invmgr_dump_tool file'
rp_exec[6]="show sdr-invmgr client connections"
rp__ksh[6]='show_sdr_invmgr_edm -c con'
rp_exec[7]="show sdr-invmgr client registrations"
rp__ksh[7]='show_sdr_invmgr_edm -c reg'
rp_exec[8]='Dump logic slot info'
rp__ksh[8]='/pkg/bin/test_cpmi_data_client 3 8 3'
rp_exec[9]="display logic slot info"
rp__ksh[9]='cat /var/log/inv_debug_xr.log'
rp_exec[10]="Dump node DB"
rp__ksh[10]='/pkg/bin/inv_dump_tool node -file show_tech_node.inv'
rp_exec[11]="Display node DB"
rp__ksh[11]='cat /var/log/show_tech_node.inv'
rp_exec[12]="Delete node DB file"
rp__ksh[12]='rm /var/log/show_tech_node.inv'
rp_exec[13]="capture invmgr_proxy traces "
rp__ksh[13]='show_invmgr_proxy_trace -F all'
rp_exec[14]="Dump invproxy trace"
rp__ksh[14]='invproxy_dump_tool file /var/log/invproxy_dump'
rp_exec[15]="Display invproxy dump"
rp__ksh[15]='cat /var/log/invproxy_dump'
rp_exec[16]="Delete invproxy dump"
rp__ksh[16]='rm /var/log/invproxy_dump'
rp_exec[17]="run cmp_dbg -s inv"
rp__ksh[17]='cmp_dbg -s inv'
rp_exec[18]="run cmp_dbg -i pm"
rp__ksh[18]='cmp_dbg -i pm'

#############################################################
# Commands that run on all LCs
#sdr_mgbl_proxy

lc_exec[1]='Dump logic slot info'
lc__ksh[1]='/pkg/bin/test_cpmi_data_client 1 8 3'
lc_exec[2]='Dump node DB'
lc__ksh[2]='/pkg/bin/inv_dump_tool node -file show_tech_node.inv'
lc_exec[3]='Display node DB'
lc__ksh[3]='cat /var/log/show_tech_node.inv'
lc_exec[4]='Delete node DB file'
lc__ksh[4]='rm /var/log/show_tech_node.inv'

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

all_exec[1]='run show_libcmp_trace -F all'
all__ksh[1]='show_libcmp_trace -F all'
all_exec[2]='run cmp_dbg -i inv'
all__ksh[2]='cmp_dbg -i inv'
all_exec[3]='run /pkg/bin/show_inv_cardlib_debug'
all__ksh[3]='/pkg/bin/show_inv_cardlib_debug'
all_exec[4]='run ds_test_client -l inv'
all__ksh[4]='ds_test_client -l inv'
all_exec[5]='run check_fds.sh sdr_mgbl_proxy'
all__ksh[5]='check_fds.sh sdr_mgbl_proxy'

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

find_and_touch_ctrace_ctrl_for_process() {
    proc_arr=( "$1" )
    for proc in ${proc_arr[@]};
        do
            find "$ctrace_directory/$proc" -name *.ctrl | xargs touch \
                > /dev/null 2>&1
            if [[ $? != 0 ]]; then
                echo "updating buffer with $proc trace files not successful"
            fi
        done
}

wait_for_ctrace_dump() {
    sleep 5
}

collect_ctrace_data() {
    proc_arr=( "$1" )
    for proc in ${proc_arr[@]};
        do
            str_cat="$str_cat $ctrace_directory/$proc"
        done
    
    cd $__tar_file_directory_on_node
    if [[ $? != 0 ]]; then
        echo "ABORT: cd to showtech directory is not successful"
        return
    fi

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

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

}


collect_ctrace_data_lc() {
    print_main_heading "ctrace info for invmgr components on LC"
 
    procs=(sdr_invmgr)    
    find_and_touch_ctrace_ctrl_for_process "$(echo ${procs[@]})"

    wait_for_ctrace_dump

    collect_ctrace_data "$(echo ${procs[@]})"
}


collect_ctrace_data_rp() {
    print_main_heading "ctrace info for invmgr components on RP"

    procs=(sdr_invmgr sdr_mgbl_proxy invmgr_proxy)    
    find_and_touch_ctrace_ctrl_for_process "$(echo ${procs[@]})"

    wait_for_ctrace_dump

    collect_ctrace_data "$(echo ${procs[@]})"
}

collect_large_output_in_separate_files() {
    print_main_heading "collecting entity/inv dumps in separate files"

    show_operational Inventory Entities _EOT_ Inventory Entities > /var/log/show_oper_InventoryEntities

    gzip /var/log/show_oper_InventoryEntities
    if [[ $? != 0 ]]; then
        echo "gzip of show_oper_Inventory is not successful"
    fi

    show_operational PlatformInventory _EOT_ PlatformInventory > /var/log/show_oper_PlatformInventory

    gzip /var/log/show_oper_PlatformInventory
    if [[ $? != 0 ]]; then
        echo "gzip of show_oper_PlatformInventory is not successful"
    fi

    /pkg/bin/inv_dump_tool system -file inv_XR_SYSTEM_db

    gzip /var/log/inv_XR_SYSTEM_db
    if [[ $? != 0 ]]; then
        echo "gzip of inv_XR_SYSTEM_db is not successful"
    fi


    mv /var/log/show_oper_InventoryEntities.gz $__tar_file_directory_on_node
    if [[ $? != 0 ]]; then
        echo "mv /var/log/show_oper_Inventory.gz show_tech_dir not successful"
    fi

    mv /var/log/show_oper_PlatformInventory.gz $__tar_file_directory_on_node
    if [[ $? != 0 ]]; then
        echo "mv /var/log/show_oper_PlatformInventory.gz show_tech_dir not successful"
    fi

    mv /var/log/inv_XR_SYSTEM_db.gz $__tar_file_directory_on_node
    if [[ $? != 0 ]]; then
        echo "mv /var/log/inv_XR_SYSTEM_db.gz show_tech_dir not successful"
    fi
}

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

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

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

        exec_commands all

    fi

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

# 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
