#! /pkg/bin/ksh
# ----------------------------------------------------------------------
# show_tech_ofa -- OFA show tech-support script utilities
#
# June 2015, Johnny Dai	 
#
# Copyright (c) 2015-2022 by cisco Systems, Inc.
# All rights reserved.
#-----------------------------------------------------------------------

. /pkg/bin/show_tech_main_fragment
. /pkg/bin/show_tech_ofa_util

if [ -e /pkg/bin/show_tech_ofa_pd ]; then
    . /pkg/bin/show_tech_ofa_pd
fi

. /pkg/bin/show_tech_grid_util

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

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

# Default timeout for each CLI is 15 mins. Reduce the timeout to 5 mins
__t_sec=300

collect_crash_debug_files() {   
    tar cvzf /tmp/var_log_npu.tar.gz /var/log/fia_driver_restart.log* /var/log/npu /var/log/dispatcher_assert.txt /var/log/npu_timeout.log
    mv /tmp/var_log_npu.tar.gz $__tar_file_directory_on_node
}

collect_large_output_in_separate_files() {
    file_name_objs="/ofa_show_objs_"$(uname -n)".gz"
    file_name_trace="/ofa_ltrace_"$(uname -n)".gz"
    file_name_ssm="/ofa_ssm_records_"$(uname -n)".gz"

    #With internet-opt feature removal, the iproute entries are not using mtrie
    #Hence, dpa_iproute_show_client now shows all entries which could run in millions
    #We need to hence limit the entries dumped to 320k, hence this change
    ofa_show_client -a -L 320000 -b | gzip > "$__tar_file_directory_on_node$file_name_objs"
    ofa_show_ltrace | gzip > "$__tar_file_directory_on_node$file_name_trace"
    ofa_ssm_show_all_records | gzip > "$__tar_file_directory_on_node$file_name_ssm"
}

################################################################################
# Commands that run on once per system
    cmd_index=1
    sys_exec[$cmd_index]='show platform'
    sys__ksh[$cmd_index]='show_platform_sysdb' 

    ((cmd_index++))
    sys_exec[$cmd_index]='show logging'
    sys__ksh[$cmd_index]='show_logging'

    ((cmd_index++))
    sys_exec[$cmd_index]='show diag details'
    sys__ksh[$cmd_index]='show_diag exec details'

    ((cmd_index++))
    sys_exec[$cmd_index]='show install active'
    sys__ksh[$cmd_index]='sdr_instcmd show install active'

    ((cmd_index++))
    sys_exec[$cmd_index]='show running-config'
    sys__ksh[$cmd_index]='nvgen -c -l 1 -t 1 -o 1'

    ((cmd_index++))
    sys_exec[$cmd_index]='show config commit list'
    sys__ksh[$cmd_index]='config_history -r -b -n 0xffffffff'

    ((cmd_index++))
    sys_exec[$cmd_index]='show config commit changes all'
    sys__ksh[$cmd_index]='show_config_changes -x'

    ((cmd_index++))
    sys_exec[$cmd_index]='show interfaces'
    sys__ksh[$cmd_index]='show_interface -a'

    ((cmd_index++))
    sys_exec[$cmd_index]='show ipv4 vrf all interface brief'
    sys__ksh[$cmd_index]='show_ip_interface -b -v all'

    ((cmd_index++))
    sys_exec[$cmd_index]='show interfaces brief'
    sys__ksh[$cmd_index]='show_interface -a -o 0x2'

    ((cmd_index++))
    sys_exec[$cmd_index]='show install active summary'
    sys__ksh[$cmd_index]='sdr_instcmd show install active summary'

    ((cmd_index++))
    sys_exec[$cmd_index]='show interface accounting'
    sys__ksh[$cmd_index]='show_interface -a -o 0x4'

    ((cmd_index++))
    sys_exec[$cmd_index]=''
    sys__ksh[$cmd_index]=''

#############################################################
# Commands that run on all RPs
#
i=1
j=1
if [ "$platform" != "enxr" ]; then

    rp_exec[i++]='show controllers npu voq-usage interface all instance all location all'
    rp__ksh[j++]='dpa_qosea_voq_show -v x -i 0x10 -n A -t n -p 0x0'

    rp_exec[i++]='show controller npu stats voq ingress interface all instance all location $location'
    rp__ksh[j++]='ofa_npu_stats_show -v a -i 0x10 -n A -t n -p A -s 0x0 -d 0x0'

    rp_exec[i++]='show im database brief location $location'
    rp__ksh[j++]='im_show database -l 0x1 -h $fq_nodeid'

    rp_exec[i++]='show process blocked location $location'
    rp__ksh[j++]='sh_proc_ng_blocked -l $fq_nodeid'

    rp_exec[i++]='show version'
    rp__ksh[j++]='show_version'

    cp /etc/show_version.txt $__tar_file_directory_on_node 2>/dev/null
    cp /etc/build-info.txt $__tar_file_directory_on_node 2>/dev/null
fi

rp_exec[i++]=''
rp__ksh[j++]=''

# ***********************************************************
# Commands that run on RP and LC
i=1
j=1

lc_exec[i++]='show ofa resources all location $location'
lc__ksh[j++]='ofa_sysdb_show_resources -r -t 0x270f -l $fq_nodeid'

lc_exec[i++]='show ofa statistics all location $location'
lc__ksh[j++]='ofa_sysdb_show_resources -s -t 0x270f -l $fq_nodeid'

lc_exec[i++]='show ofa backwalk all all location $location'
lc__ksh[j++]='ofa_sysdb_show_bwalk_stats -t 0x270f -r 0x64 -l $fq_nodeid'

lc_exec[i++]='show ofa rdesc-pool-usage all detail location $location'
lc__ksh[j++]='ofa_show_rdesc_pool_usage -p 0x270f -d -l $fq_nodeid'

if [ "$platform" == "enxr" ]; then
    client_all=0x3
else
    client_all=0x2
fi

lc_exec[i++]='show ofa transport async stats client all location $location'
lc__ksh[j++]='ofa_transport_show -t async -k stats -c $client_all -l $fq_nodeid'

lc_exec[i++]='show ofa transport async health client all location $location'
lc__ksh[j++]='ofa_transport_show -t async -k health -c $client_all -l $fq_nodeid'

lc_exec[i++]='show ofa transport async history client all location $location'
lc__ksh[j++]='ofa_transport_show -t async -k history -c $client_all -l $fq_nodeid'

lc_exec[i++]='show ofa shm chunk-stats location $location'
lc__ksh[j++]='ofa_shm_show "-t" "chunk_stats" "-l" $fq_nodeid'

lc_exec[i++]='show shmwin dpa pool all-pools location $location'
lc__ksh[j++]='shmwin_show "-N" "dpa" "-c" "0x11" "-n" $fq_nodeid'

lc_exec[i++]='show shmwin summary location $location'
lc__ksh[j++]='shmwin_show "-N" "all" "-S" "-n" $fq_nodeid'

lc_exec[i++]='show shmwin trace all location $location'
lc__ksh[j++]='show_shmwin_trace "-F" "all" "-i" $fq_nodeid'

lc_exec[i++]='show shmwin trace server location $location'
lc__ksh[j++]='shmwin_svr_show_ltrace "-i" $fq_nodeid'

lc_exec[i++]='show processes'
lc__ksh[j++]='show_processes'

lc_exec[i++]='show ofa rdesc-stats-model data location $location'
lc__ksh[j++]='ofa_rdesc_stats_model_show -t data -l $fq_nodeid'

lc_exec[i++]='show ofa performance all location $location'
lc__ksh[j++]='ofa_sysdb_show_perf -t 0x270f -l $fq_nodeid'

lc_exec[i++]='show shmwin dpa pool all-pools location $location'
lc__ksh[j++]='shmwin_show -N dpa -c 0x11 -n $fq_nodeid'

lc_exec[i++]='show ofa objects all object-count location $location \| ex \"0 entries\"'
lc__ksh[j++]='ofa_obj_show -t 0x270f -l $fq_nodeid -o c \| grep -v -E \"0 entries\"'

lc_exec[i++]=''
lc__ksh[j++]=''


################################################################
# Run show tech ofa commands
##################################################################
display ()
{
   print_main_heading 'show tech-support ofa'

   if [ "$__cardtype" == "SYS" ]; then
       exec_commands sys
   else
       case "$__cardtype" in
        
       "LC")
           exec_commands lc
           display_ofa_lc
           display_grid_lc
           collect_large_output_in_separate_files
           collect_crash_debug_files
           ;;
       
       "RP")
           exec_commands rp
           display_grid_rp
           ;;
       esac

       if type display_ofa_pd | grep -i -q function; then
           display_ofa_pd
       fi

   fi

   print_main_heading "show tech-support ofa complete"
}

. /pkg/bin/show_tech_file_fragment
