#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# Fabric_snapshot - show commands for show tech-support fabric
#                     snapshot spawned by show_tech_fast
#
# 
#
# Copyright (c) 2012-2022 by cisco Systems, Inc.
# All rights reserved.
# --------------------------------------------------------------------
. /pkg/bin/show_tech_main_fragment
#. /pkg/bin/fabric_common_showtech

# Password flag to indicate whether log will include pw or not
__cardtype="unspecified"
__password="FALSE"
__sanitized="FALSE"
__mc_flag="FALSE"
# Reduce the timeout to 2 mins
__t_sec=120 
# Parse the arguments to the script.
while [ "$#" -gt "0" ]; do
    case "$1" in
        -p) __password="TRUE"; shift 1;;
        -s) __sanitized="TRUE"; shift 1;;
        -t) __cardtype="$2"; shift 2;;
        *)  default_parser_function "$@"; shift $#;;
    esac
done




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

is_ncs540l()
{ 
   if [[ "$check_platform_type" == "NCS540L" ]]; then
       return 1
   fi 
   return 0
}  

is_ncs5500_family()
{
    if [[ $check_platform_type == "FRETTA" || \
        $check_platform_type == "NCS540L"  || \
        $check_platform_type == "ZERMATT" ]]; then
        return 1
    fi
    return 0
}

is_ncs5700 ()
{ 
   if [[ "$check_platform_type" == "NCS5700" ]]; then
       return 1
   fi 
   return 0
}

is_ncs5500l ()
{
   if [[ "$check_platform_type" == "NCS5500L" ]]; then
       return 1
   fi
   return 0
}

# Function: exec_commands_dshell
#
# Runs commands from the array passed
#
# Arguments: Array name with suffix _exec, __ksh to be executed
#            to generate show tech output
#
# Note:Copied from exec_commands function in show tech infra
#      to run diag shell commands without exec_timeout 
#      as exec_timeout doesnt work with string arguments
#
exec_commands_dshell() {
    cmd_set="$1"
    # initialize variables
    location=$ext_node_name
    pq_nodeid=`node_conversion -I $location`
    fq_nodeid=`node_conversion -i $int_node_name`
 
    i=1; cmd_exec=$(eval "echo \${"$cmd_set"_exec[$i]}")
    while [ -n "$cmd_exec" ]; do
        cmd_exec=$(eval "echo $cmd_exec")
        print_heading "$cmd_exec [`iosclock -d 0`]" "+"
        cmd__ksh=$(eval "echo \${"$cmd_set"__ksh[$i]}")
        cmd__ksh=$(eval "echo $cmd__ksh")
        eval $cmd__ksh
        if [ "$?" != 0 ]; then
            echo "$__errormsg $cmd_exec"
        fi
        print_heading "$cmd_exec [`iosclock -d 0`]" "-"
 
        # next command from array
        i=$(($i + 1))
        cmd_exec=$(eval "echo \${"$cmd_set"_exec[$i]}")
    done
}


# This is a workaround till all platforms migrate to a specific script based
# implementation (tech_fabric_platform). Once all platforms have their
# tech_fabric_platform separated out, there is no platform based condition
# check required here - unless they cant be sepated out.
is_ncs5500_family
is_ncs5500=$?

is_ncs540l
isncs540l=$?

is_ncs5700
isncs5700=$?

is_ncs5500l
isncs5500l=$?

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

#
# RP commands executed only once.
#
### General information

sys_cmd_count=1

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

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

sys_exec[$sys_cmd_count]='show redundancy'
sys__ksh[$sys_cmd_count]='redcon_show'
((sys_cmd_count++))

sys_exec[$sys_cmd_count]='show platform'
sys__ksh[$sys_cmd_count]='show_platform_sysdb'
((sys_cmd_count++))

sys_exec[$sys_cmd_count]='show processes blocked location all'
sys__ksh[$sys_cmd_count]='sh_proc_ng_blocked -l all'
((sys_cmd_count++))

sys_exec[$sys_cmd_count]='show context'
sys__ksh[$sys_cmd_count]='corehelper_context -c 0x1 -n all'
((sys_cmd_count++))

if [[ $is_ncs5500 == 0 ]]; then
    sys_exec[$sys_cmd_count]='show controllers fia voq-usage interface all instance all location all'
    sys__ksh[$sys_cmd_count]='fia_driver_show -v x -b 0x30 -f 0x30 -i 0xff -n A -t n -p 0x1869f'
    ((sys_cmd_count++))
fi

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

sys_exec[$sys_cmd_count]='show version'
sys__ksh[$sys_cmd_count]='ng_show_version'
((sys_cmd_count++))

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

if [[ $isncs540l == 1 || $isncs5700 == 1 || $isncs5500l == 1 ]]; then
    sys_exec[$sys_cmd_count]='show controllers fabric fgid information all'
    sys__ksh[$sys_cmd_count]='fgid_mgr_show i 0xffffffff 0xffffffff brief'
    ((sys_cmd_count++))

    sys_exec[$sys_cmd_count]='show controllers fabric fgid statistics all detail'
    sys__ksh[$sys_cmd_count]='fgid_mgr_show s all all detail'
    ((sys_cmd_count++))

    sys_exec[$sys_cmd_count]='show controllers fabric fgid resource all'
    sys__ksh[$sys_cmd_count]='fgid_mgr_show r all all 0x0 0xffffffff'
    ((sys_cmd_count++))
fi

#############################################################
# Commands that run on all nodes
#
all_exec[1]=''
all__ksh[1]=''


#############################################################
# Commands that run on all SCs & RPs
#
##fsdb server
sc_exec[1]=''
sc__ksh[1]=''


#############################################################
# Commands that run on all RPs
#
rp_exec[1]='show controllers fia-cfg trace all location $location'
rp__ksh[1]='fia_cfg_show_ctrace -a'
#############################################################
# Commands that run on all RPs/LCs/DRPs
#
#INGRESSQ
rplc_exec[1]=''
rplc__ksh[1]=''


############################################################# 
# Commands that need to loop for all nodes within the rack
#
sc_loc_exec[1]=''
sc_loc__ksh[1]=''

#############################################################
# Commands that run on all LCs
#
# LC ingress info
#
lc_cmd_count=1

# fia statistics at start
lc_exec[$lc_cmd_count]='show controllers fia statistics detail instance all location $location'
lc__ksh[$lc_cmd_count]='fia_driver_show -s x -b 0x30 -f 0x30 -i 0xff -t n -p 0xffffffff'
((lc_cmd_count++))

lc_exec[$lc_cmd_count]='show processes fia_driver location $location'
lc__ksh[$lc_cmd_count]='sysmgr_show -o -p fia_driver'
((lc_cmd_count++))
  
#Show fia driver location <>
lc_exec[$lc_cmd_count]='show controllers fia driver location $location'
lc__ksh[$lc_cmd_count]='fia_driver_show -d d -i 5 -p 16384 '
((lc_cmd_count++))

#Show asic-err nonerror location <>
lc_exec[$lc_cmd_count]='show asic-error fia 0 non location $location'
lc__ksh[$lc_cmd_count]='asic_errors_show -n $fq_nodeid -a 0x2 -i all -e 0x1b -s -t 0x0 -m 0x0'
((lc_cmd_count++))


if [[ $is_ncs5500 == 0 ]]; then
    #show controllers fia voq-usage intf-handle all slice all location <>
    lc_exec[$lc_cmd_count]='show controllers fia voq-usage intf-handle all slice all location $location'
    lc__ksh[$lc_cmd_count]='fia_driver_show -v a -i 5 -p 99999 '
    ((lc_cmd_count++))
fi

#show controllers fia trace all location <>
lc_exec[$lc_cmd_count]='show controllers fia trace all location $location'
lc__ksh[$lc_cmd_count]='fia_show_ltrace -A '
((lc_cmd_count++))

# This is a workaround till all platforms migrate to a specific script based
# implementation (tech_fabric_platform). Once all platforms have their
# tech_fabric_platform separated out, there is no platform based condition
# check required here - unless they cant be sepated out.
is_ncs5500_family
is_ncs5500=$?

lc_exec[$lc_cmd_count]='Platfrom specific commands for $location'
lc__ksh[$lc_cmd_count]='/pkg/bin/tech_fabric_platform "$@"'
((lc_cmd_count++))

lc_exec[$lc_cmd_count]='show processes blocked location $location'
lc__ksh[$lc_cmd_count]='sysmgr_show -o -p blocked'
((lc_cmd_count++))
  
lc_exec[$lc_cmd_count]='show run lspci -vt location $location'
lc__ksh[$lc_cmd_count]='lspci -vt'
((lc_cmd_count++))
  
lc_exec[$lc_cmd_count]='show top -b -n 5 -d 1 location $location'
lc__ksh[$lc_cmd_count]='top -b -n 5 -d 1'
((lc_cmd_count++))

# fia statistics at end
lc_exec[$lc_cmd_count]='show controllers fia statistics detail instance al location $location'
lc__ksh[$lc_cmd_count]='fia_driver_show -s x -b 0x30 -f 0x30 -i 0xff -t n -p 0xffffffff'
((lc_cmd_count++))

is_ncs540l
isncs540l=$?

is_ncs5700
isncs5700=$?

is_ncs5500l
isncs5500l=$?

if [[ $isncs540l == 1 || $isncs5700 == 1 || $isncs5500l == 1 ]]; then
    #show controllers fabric fgid trace all location <>
    lc_exec[$lc_cmd_count]='show controllers fabric fgid trace all location $location'
    lc__ksh[$lc_cmd_count]='fgid_show_trace -E '
    ((lc_cmd_count++))
fi

# Error log file for shell commands run
sh_tech_err_file=/dev/null

copy_reg_dump_files() {
    for i in `ls /var/log/fia_debug_dump_unit_*.txt.gz 2>> $sh_tech_err_file`
    do
        cp $i $__tar_file_directory_on_node 2>> $sh_tech_err_file
    done
}

copy_tcam_reg_dump_files() {
    for i in `ls /var/log/fia_debug_tcam_dump_unit_*.txt.gz 2>> $sh_tech_err_file`
    do
        cp $i $__tar_file_directory_on_node 2>> $sh_tech_err_file
    done
}

copy_hr_reg_dump_files() {
    for i in `ls /root/fia_debug_dump_unit_*.txt.gz 2>> $sh_tech_err_file`
    do
        cp $i $__tar_file_directory_on_node 2>> $sh_tech_err_file
    done
}

cleanup_fia_logs() {
    find /var/log/ -type f -name "fia_debug_dump_unit*" | xargs rm 2>> $sh_tech_err_file
}

cleanup_tcam_logs() {
    find /var/log/ -type f -name "fia_debug_tcam_dump_unit*" | xargs rm 2>> $sh_tech_err_file
}

# Separate folder per node is created as the log files have the same name.
# /misc/scratch files are the ASIC/board reload data files.
copy_fia_driver_logs() {
    ext_node_name=`node_list_generation -f MY`
    ext_node_folder=`node_conversion -N $ext_node_name`_logs
    mkdir -p $__tar_file_directory_on_node/$ext_node_folder 2>> $sh_tech_err_file
    for i in `ls /var/log/fia_driver_restart.log* 2>> $sh_tech_err_file`
    do
        cp $i $__tar_file_directory_on_node/$ext_node_folder 2>> $sh_tech_err_file
    done
    cp /misc/scratch/fia_driver_board_reload.log* $__tar_file_directory_on_node/$ext_node_folder 2>> $sh_tech_err_file
    cp /misc/scratch/fia_driver_kernel.log* $__tar_file_directory_on_node/$ext_node_folder 2>> $sh_tech_err_file
    cp /misc/scratch/fia_debug_tcam_dump_unit_*.txt $__tar_file_directory_on_node/$ext_node_folder 2>> $sh_tech_err_file
    cp /misc/scratch/fia_debug_dump_unit_*.txt $__tar_file_directory_on_node/$ext_node_folder 2>> $sh_tech_err_file
    cp /misc/scratch/kbp_debug.log* $__tar_file_directory_on_node/$ext_node_folder 2>> $sh_tech_err_file
}

copy_debug_dump_files() {
    for i in `ls /misc/scratch/fia_actflow_badparams_dump_* 2>> $sh_tech_err_file`
    do
        mv $i $__tar_file_directory_on_node
    done
}

copy_debug_v1_trace_dump_files() {
    for i in `ls /root/v1_node*`
    do
        mv $i $__tar_file_directory_on_node
    done
}

#if [[ "$check_platform_type" != "SCAPA" && $is_ncs5500 == 0 ]]; then
#    . /pkg/bin/tech_fabric_platform
#fi

copy_old_act_log_files() {
    for i in `ls /disk0:/act_*_log*`
    do
          mv $i $__tar_file_directory_on_node
    done
  }

copy_WB_dump_files() {
    for i in `ls /disk0:/WB_cbuf_dump_*`
    do
          mv $i $__tar_file_directory_on_node
    done

    for i in `ls /disk0:/fia_warmboot_file_*`
    do
          mv $i $__tar_file_directory_on_node
    done
}

do_show_running() {
    if [ $__password = "TRUE" ]; then
        print_heading "show running-config" "-"
        nvgen -c -l 1 -t 1 -i0 -o 1
    elif [ $__sanitized = "TRUE" ]; then
        print_heading "show running-config (sanitized)" "-"
        nvgen -c -l 1 -t 1 -i5 -o 1
    else
        print_heading "show running-config (no password)" "-"
        nvgen -c -l 1 -t 1 -i1 -o 1
    fi
}

do_capture_knet_debugs() {
    if [ -r "/proc/bcm/knet/stats" ]; then
        print_heading "Knet Device Debug Info" "-"
        knet_debug_file="$__tar_file_directory_on_node/knet_debug.txt"
        echo "--------------- START OF KNET DEBUG INFO ----------------" > $knet_debug_file

        echo "--------------- /proc/linux-bcm-knet ----------------" >> $knet_debug_file
        cat /proc/linux-bcm-knet >> $knet_debug_file 2>> $sh_tech_err_file

        echo "--------------- lsmod ----------------" >> $knet_debug_file
        lsmod >> $knet_debug_file 2>> $sh_tech_err_file

        echo "--------------- /proc/linux-kernel-bde ----------------" >> $knet_debug_file
        cat /proc/linux-kernel-bde >> $knet_debug_file 2>> $sh_tech_err_file

        echo "--------------- /proc/linux-user-bde ----------------" >> $knet_debug_file
        cat /proc/linux-user-bde >> $knet_debug_file 2>> $sh_tech_err_file

        echo "--------------- /proc/bcm/knet/link ----------------" >> $knet_debug_file
        cat /proc/bcm/knet/link >> $knet_debug_file 2>> $sh_tech_err_file
        
        echo "--------------- /proc/bcm/knet/rate ----------------" >> $knet_debug_file
        cat /proc/bcm/knet/rate >> $knet_debug_file 2>> $sh_tech_err_file
        
        echo "--------------- /proc/bcm/knet/dma ----------------" >> $knet_debug_file
        cat /proc/bcm/knet/dma >> $knet_debug_file 2>> $sh_tech_err_file
        
        echo "--------------- /proc/bcm/knet/dstats ----------------" >> $knet_debug_file
        cat /proc/bcm/knet/dstats >> $knet_debug_file 2>> $sh_tech_err_file
        
        echo "--------------- /proc/bcm/knet/debug ----------------" >> $knet_debug_file
        cat /proc/bcm/knet/debug >> $knet_debug_file 2>> $sh_tech_err_file

        echo "--------------- /proc/bcm/knet/stats (Run 1) ----------------" >> $knet_debug_file
        cat /proc/bcm/knet/stats >> $knet_debug_file 2>> $sh_tech_err_file
        
        echo "--------------- /proc/bcm/knet/stats (Run 2) ----------------" >> $knet_debug_file
        cat /proc/bcm/knet/stats >> $knet_debug_file 2>> $sh_tech_err_file
        
        echo "--------------- DMESG ----------------" >> $knet_debug_file
        dmesg -T >> $knet_debug_file 2>> $sh_tech_err_file

        echo "--------------- END OF KNET DEBUG INFO ----------------" >> $knet_debug_file
        print_heading "End of Knet Device Debug Info" "-"
    fi
}

display() {
#Print opening heading
      print_main_heading "show tech-support fabric"
      is_ncs540l
      isncs540l=$?
      is_ncs5700
      isncs5700=$?
      is_ncs5500l
      isncs5500l=$?
      #Manoj TODO - Check if 5500l needs to be added in the below case
      if [ "$__cardtype" == "ONESYS" ] && [$isncs540l -eq 0] && [$isncs5700 -eq 0]; then
          echo "one showtech" >> $__tar_file_directory_on_node/admin_flag 
          #checking for chvrf support
          if [[ `which chvrf` != "" ]]; then
              ./pkg/sbin/admin-cli-proxy-xr_static -n -p XR << EOF
              run uname -a;run which scp;run chvrf 0 /opt/cisco/calvados/script/show_tech_fast -r -c fabric -i '$RP_IP' -t /'$__tar_file_directory'/  -m show_fabric
EOF
          else
              /pkg/sbin/admin-cli-proxy-xr_static -n -p XR << EOF
              run uname -a;run which scp;run /opt/cisco/calvados/script/show_tech_fast -r -c fabric -i '$RP_IP' -t /'$__tar_file_directory'/  -m show_fabric
EOF
          fi
      elif [ "$__cardtype" == "SYS" ]; then
         exec_commands sys
      elif [ "$__cardtype" != "RP" ]; then
         exec_commands lc
         if [ "$check_platform_type" != "SCAPA" ]; then
            exec_commands_dshell lc_dshell
         fi
         if [ "$common_only" == "0" ]; then
            if [ "$check_platform_type" != "SCAPA" ]; then
                exec_commands platform
            fi
         fi
     elif [ "$__cardtype" == "RP" ]; then
         exec_commands rp
         if [ "$common_only" == "0" ]; then
            if [ "$check_platform_type" != "SCAPA" ]; then
                exec_commands platform
            fi
         fi
     fi

     #showtech connected
     if [ "$check_platform_type" == "PANINI" ]; then
         enable_techs "" npu
     fi

     # Copy the debug-dump files to show tech-support 
     copy_debug_dump_files
     # copy the issu v1 trace debug files to show tech-support
     copy_debug_v1_trace_dump_files
     #Print closing heading
     copy_old_act_log_files
     # copy WB dump
     copy_WB_dump_files 
     # copy hard reset register dump files
     copy_hr_reg_dump_files
     # Knet Debug Files
     if ! grep -q "PD_CARD_TYPE=27000" /dev/xr_bootstrap; then
         do_capture_knet_debugs
     fi
     # Copy the register dump, other files collected from platform show tech
     # (tech_fabric_platform)
     # Done for ncs5500 only for now. Need to pass tarfile variable to 
     # tech_platform script so that these can be moved to tech_platform for each
     # specific platforms.
     if [[ $__cardtype == "LC" || $__arch == "thinxr" || "$check_platform_type" == "UEA" ]]; then
         copy_reg_dump_files
         copy_tcam_reg_dump_files
         copy_fia_driver_logs
     fi
     
     #cleanup space from fia reg dump files
     if [ "$check_platform_type" == "PANINI" ]; then
         if [[ $__cardtype == "RP" ]]; then
             cleanup_fia_logs
         fi
     fi
     
     if [ "$check_platform_type" == "FRETTA" ]; then
         cleanup_fia_logs
         cleanup_tcam_logs
     fi
          print_main_heading "show tech-support fabric 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

