#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# Optics_snapshot - show commands for show tech-support optics
#
# Oct 2015, Alok Kumar Sinha

# Copyright (c) 2015-2021 by cisco Systems, Inc.
# All rights reserved.
# --------------------------------------------------------------------



. /pkg/bin/show_tech_main_fragment

PLAT=`/pkg/bin/get_platform_type`

if [ $PLAT != 8000 ]
then
    source /pkg/bin/tech_fabric_common
fi

__cardtype="unspecified"
__iswhitebox=0

# 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 [ "$__cardtype" == "unspecified" ]; then
    __cardtype=`node_type`
fi

__isciscohw=$(xr_sysctl -e -n cisco.ios.xr.portmap.is_cisco_hw)
: ${__isciscohw:=1}
if [ "$__isciscohw" == "0" ]; then
    __npufamily=`xr_sysctl --values cisco.ios.xr.portmap.npu_family`
    if [ "$__npufamily" == "WHITE_BOX" ]; then
        __iswhitebox="1"
    fi
fi

#save the ctrace for the led for a given LC node process to the outfile argument passed
led_save_ctrace () {
    outfile=$1/led

    /pkg/bin/ctracedec -gktTdslAwp optics_led >> $outfile
}

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

#
# SYS commands 
#
### General information
sys_exec[1]='show version'
sys__ksh[1]='ng_show_version'

sys_exec[2]='show platform'
sys__ksh[2]='show_platform_sysdb'

sys_exec[3]='show logging'
sys__ksh[3]='show_logging'

sys_exec[4]='show running-config'
sys__ksh[4]='nvgen -c -l 1 -t 1 -o 1'

sys_exec[5]='show processes blocked'
sys__ksh[5]='sh_proc_ng_blocked -l all'

# Commands that run on all LCs
#show optics-driver trace all location
lc_exec[1]='show optics-driver trace all location $location'
lc__ksh[1]='optics_icpe_show_ltrace -A -i location $location'

#show optics trace 
lc_exec[2]='show optics trace ext location $location'
lc__ksh[2]='optics_show_ltrace -X'

lc_exec[3]='show optics trace init location $location'
lc__ksh[3]='optics_show_ltrace -Z'

lc_exec[4]='show optics trace event location $location'
lc__ksh[4]='optics_show_ltrace -H'

lc_exec[5]='show optics-driver trace driver all location $location'
lc__ksh[5]='optics_icpe_show_ltrace -C -i $fq_nodeid'

#show vether-ea trace all location
lc_exec[6]='show vether-ea trace all location $location'
lc__ksh[6]='dpa_vether_ea_show_ltrace -i $fq_nodeid -E -V'

#show ether-ea trace all location
lc_exec[7]='show ether-ea trace all location $location'
lc__ksh[7]='dpa_ether_ea_show_ltrace -i $fq_nodeid -E -V'

#show eapd optics trace all location
lc_exec[8]='show eapd optics trace all location $location'
lc__ksh[8]='optics_eapd_show_ltrace -A -i $fq_nodeid'

#show ofa trace location
lc_exec[16]='show ofa trace location $location'
lc__ksh[16]='ofa_show_ltrace -i $fq_nodeid'

#show dpa trace location
lc_exec[9]='show dpa trace location $location'
lc__ksh[9]='dpa_show_ltrace -i $fq_nodeid'

#show optics-driver trace icpe all location
lc_exec[10]='show optics-driver trace icpe all location $location'
lc__ksh[10]='optics_icpe_show_ltrace -A -i $fq_nodeid'

lc_exec[19]='show optics-driver debug all location $location'
lc__ksh[19]='optics_debug_show -A -i $fq_nodeid'

#show controllers fia trace all location
lc_exec[11]='show controllers fia trace all location $location'
lc__ksh[11]='fia_show_ltrace -A -i $fq_nodeid'

#show ethernet v-ether trace location
lc_exec[12]='show ethernet v-ether trace location $location'
lc__ksh[12]='eth_intf_show_trace -i $fq_nodeid'

#show qos ea trace all location
lc_exec[13]='show qos ea trace all location $location'
lc__ksh[13]='qos_ea_show_ltrace -i $fq_nodeid -A'

#show vether-driver server trace
lc_exec[14]='show vether-driver server event location $location'
lc__ksh[14]='easy_debug_ltrace -i $fq_nodeid -W 0 -X 0x1 -Y 0x0 -Z libvether_server'

#show vether-driver server error trace
lc_exec[15]='show vether-driver server error location $location'
lc__ksh[15]='easy_debug_ltrace -i $fq_nodeid -W 0 -X 0x0 -Y 0x0 -Z libvether_server'

lc_exec[17]='sfpga ltrace'
lc__ksh[17]='optics_sfpga_show_ltrace -A $fq_nodeid'

#show controller fia diagshell 
lc_exec[18]='show controller fia diagshell'
lc__ksh[18]='cat /tmp/diagshell_lc'

#show port and group db dumps
lc_exec[21]='show port DB'
lc__ksh[21]='vether_test ddb -t 0'

if [ "$platform_variant" == "fretta" ]; then
    #show vether ea events
    lc_exec[22]='show vether ea event'
    lc__ksh[22]='/pkg/bin/fretta_vether_ea_show_ltrace -V'

    #show vether ea error
    lc_exec[23]='show vether ea error'
    lc__ksh[23]='/pkg/bin/fretta_vether_ea_show_ltrace -E'
fi

# Command that run on all RPs 
rp_exec[1]='show version'
rp__ksh[1]='ng_show_version'

rp_exec[2]='show platform'
rp__ksh[2]='show_platform_sysdb'

rp_exec[3]='show dpa trace location $location'
rp__ksh[3]='dpa_show_ltrace -i $fq_nodeid'

rp_exec[4]='show inventory details'
rp__ksh[4]='show_inventory -e -x'

rp_exec[5]='show alarms brief'
rp__ksh[5]='show_operational -a Alarms Brief'

rp_exec[6]='show inventory vendor-type'
rp__ksh[6]='show_inventory -e -o'

rp_exec[7]='show ipv4 interface brief'
rp__ksh[7]='show_ip_interface -b -v default'

#show controller optics trace
rp_exec[8]='show controllers optics *'
rp__ksh[8]='show_controller_optics -a Optics.*'

cmd_index=0
((cmd_index++))
sflc_exec[$cmd_index]='show optics-driver trace driver all location $location'
sflc__ksh[$cmd_index]='optics_icpe_show_ltrace -C -i $fq_nodeid'

((cmd_index++))
sflc_exec[$cmd_index]='show optics-driver debug all location $location'
sflc__ksh[$cmd_index]='optics_debug_show -A -i $fq_nodeid'

((cmd_index++))
sflc_exec[$cmd_index]='show optics trace ext location $location'
sflc__ksh[$cmd_index]='optics_show_ltrace -X'

((cmd_index++))
sflc_exec[$cmd_index]='show optics trace init location $location'
sflc__ksh[$cmd_index]='optics_show_ltrace -Z'

((cmd_index++))
sflc_exec[$cmd_index]='show optics trace event location $location'
sflc__ksh[$cmd_index]='optics_show_ltrace -H'

#show optics-driver trace icpe all location
((cmd_index++))
sflc_exec[$cmd_index]='show optics-driver trace icpe all location $location'
sflc__ksh[$cmd_index]='optics_icpe_show_ltrace -A -i $fq_nodeid'

#show vether-ea trace all location
((cmd_index++))
sflc_exec[$cmd_index]='show vether-ea trace all location $location'
sflc__ksh[$cmd_index]='ofa_vether_ea_show_ltrace -i $fq_nodeid -E -V'

((cmd_index++))
sflc_exec[$cmd_index]='show vether-ea platform trace all location $location'
sflc__ksh[$cmd_index]='show_vether_platform_trace -A -i $fq_nodeid'

#show ether-ea trace all location
((cmd_index++))
sflc_exec[$cmd_index]='show ether-ea trace all location $location'
sflc__ksh[$cmd_index]='ofa_ether_ea_show_ltrace -i $fq_nodeid -E -V'

#show eapd optics trace all location
((cmd_index++))
sflc_exec[$cmd_index]='show eapd optics trace all location $location'
sflc__ksh[$cmd_index]='optics_eapd_show_ltrace -A -i $fq_nodeid'

((cmd_index++))
sflc_exec[$cmd_index]='show controllers npu trace all location $location'
sflc__ksh[$cmd_index]='npu_show_ctrace -A -h $fq_nodeid'

#show ethernet v-ether trace location
((cmd_index++))
sflc_exec[$cmd_index]='show ethernet v-ether trace location $location'
sflc__ksh[$cmd_index]='eth_intf_show_trace -i $fq_nodeid'

#show qos ea trace all location
((cmd_index++))
sflc_exec[$cmd_index]='show qos ea trace all location $location'
sflc__ksh[$cmd_index]='qos_ea_show_ltrace -i $fq_nodeid -A'

#show vether-driver server trace
((cmd_index++))
sflc_exec[$cmd_index]='show vether-driver server event location $location'
sflc__ksh[$cmd_index]='easy_debug_ltrace -i $fq_nodeid -W 0 -X 0x1 -Y 0x0 -Z libvether_server'

#show vether-driver server error trace
((cmd_index++))
sflc_exec[$cmd_index]='show vether-driver server error location $location'
sflc__ksh[$cmd_index]='easy_debug_ltrace -i $fq_nodeid -W 0 -X 0x0 -Y 0x0 -Z libvether_server'

((cmd_index++))
sflc_exec[$cmd_index]='show vether-driver fsm event location $location'
sflc__ksh[$cmd_index]='easy_debug_ltrace -i $fq_nodeid -W 0 -X 0x3 -Y 0x0 -Z libvether_server'

((cmd_index++))
sflc_exec[$cmd_index]='show vether-driver fsm error location $location'
sflc__ksh[$cmd_index]='easy_debug_ltrace -i $fq_nodeid -W 0 -X 0x2 -Y 0x0 -Z libvether_server'
 
#show ofa trace location
((cmd_index++))
sflc_exec[$cmd_index]='show ofa trace location $location'
sflc__ksh[$cmd_index]='ofa_show_ltrace -i $fq_nodeid'

((cmd_index++))
sflc_exec[$cmd_index]='utility tail -n +1 /var/log/ofa_la.log'
sflc__ksh[$cmd_index]='cat /var/log/ofa_la.log'

#show icpe-internal local controller optics trace location
((cmd_index++))
sflc_exec[$cmd_index]='show icpe-internal local controller optics trace location $location'
sflc__ksh[$cmd_index]='icpe_local_show_trace -C 0x0 -i $fq_nodeid'

#show icpe-internal local controller optics db all global all location
((cmd_index++))
sflc_exec[$cmd_index]='show icpe-internal local controller optics db all global all location $location'
sflc__ksh[$cmd_index]='icpe_local_show_db -C 0x0 -P 0x0 -G $fq_nodeid -F 0x0'

#show icpe-internal local controller optics db all interface
((cmd_index++))
sflc_exec[$cmd_index]='show icpe-internal local controller optics db all interface'
sflc__ksh[$cmd_index]='icpe_local_show_db -C 0x0 -P 0x0 -A'

#show icpe-internal local controller optics api-stats all location
((cmd_index++))
sflc_exec[$cmd_index]='show icpe-internal local controller optics api-stats all location $location'
sflc__ksh[$cmd_index]='icpe_local_show_api_stats -P 0x0 -g 0x0 -S -N $fq_nodeid'

#show icpe-internal local controller optics api-stats all detail location
((cmd_index++))
sflc_exec[$cmd_index]='show icpe-internal local controller optics api-stats all detail location $location'
sflc__ksh[$cmd_index]='icpe_local_show_api_stats -P 0x0 -g 0x0 -S -d -N $fq_nodeid'

#show icpe-internal local controller optics status
((cmd_index++))
sflc_exec[$cmd_index]='show icpe-internal local controller optics status'
sflc__ksh[$cmd_index]='icpe_local_show_status -C 0x0'

#show icpe-internal local controller optics status detail
((cmd_index++))
sflc_exec[$cmd_index]='show icpe-internal local controller optics status detail'
sflc__ksh[$cmd_index]='icpe_local_show_status -C 0x0 -D'

#show optics trace all location
((cmd_index++))
sflc_exec[$cmd_index]='show optics trace all location $location'
sflc__ksh[$cmd_index]='optics_show_ltrace -A -i $fq_nodeid'

((cmd_index++))
sflc_exec[$cmd_index]='show im trace location $location'
sflc__ksh[$cmd_index]='im_show_trace -i $fq_nodeid'

# Command that run on all RPs 
cmd_index=0
# specify context 11 to display dumper context for last 10 crashes (not in CLI)
((cmd_index++))
sfrp_exec[$cmd_index]='show context'
sfrp__ksh[$cmd_index]='corehelper_context -c 0xb -n all'

((cmd_index++))
sfrp_exec[$cmd_index]='show epm trace boot location all'
sfrp__ksh[$cmd_index]='epm_show_ltrace -i all -T 0x1'

((cmd_index++))
sfrp_exec[$cmd_index]='show controllers optics'
sfrp__ksh[$cmd_index]='show_controller_optics -a Optics.*'

((cmd_index++))
sfrp_exec[$cmd_index]='show ofa trace location $location'
sfrp__ksh[$cmd_index]='ofa_show_ltrace -i $fq_nodeid'

((cmd_index++))
sfrp_exec[$cmd_index]='show inventory details'
sfrp__ksh[$cmd_index]='show_inventory -e -x'

((cmd_index++))
sfrp_exec[$cmd_index]='show controllers fabric plane all'
sfrp__ksh[$cmd_index]='fsdbagg_show -C P -p all -s NA -o NA'

# Commands for ncs5k-skywarp platform collected in node.tech
# show version,platform,logging & running-config already collected in system.tech

skp_exec[1]='show inventory details'
skp__ksh[1]='show_inventory -e -x'

skp_exec[2]='show controllers optics *'
skp__ksh[2]='show_controller_optics -a Optics.*'

skp_exec[3]='show alarms brief'
skp__ksh[3]='show_operational -a Alarms Brief'

skp_exec[4]='show interfaces location $location'
skp__ksh[4]='show_interface -a -p 0x2a001000'

skp_exec[5]='show ipv4 interface brief'
skp__ksh[5]='show_ip_interface -b -v default'

skp_exec[6]='show im trace wrapping location $location'
skp__ksh[6]='im_show_trace -w -i 4096'

skp_exec[7]='show vether-ea trace all wrapping location $location'
skp__ksh[7]='skp_vether_ea_show_ltrace -w -i 4096 -E -V'

skp_exec[8]='show prm server error wrapping location $location'
skp__ksh[8]='easy_debug_ltrace -w -i 4096 -W 0 -X 0x0 -Y 0x0 -Z prm_server'

skp_exec[9]='show prm server event wrapping location $location'
skp__ksh[9]='easy_debug_ltrace -w -i 4096 -W 0 -X 0x1 -Y 0x0 -Z prm_server'

skp_exec[10]='show prm server uidb all software'
skp__ksh[10]='prm_show -s -uidb -all'

skp_exec[11]='show prm server port all software'
skp__ksh[11]='prm_show -s -port -all'

skp_exec[12]='show prm uidb error wrapping location $location'
skp__ksh[12]='easy_debug_ltrace -w -i 4096 -W 0 -X 0x6 -Y 0x0 -Z prm_server'

skp_exec[13]='show prm uidb event wrapping location $location'
skp__ksh[13]='easy_debug_ltrace -w -i 4096 -W 0 -X 0x7 -Y 0x0 -Z prm_server'

skp_exec[14]='show pm trace wrapping location $location'
skp__ksh[14]='sysmgr_show_ltrace -w -i 4096'

skp_exec[15]='show ethernet v-ether trace wrapping location $location'
skp__ksh[15]='eth_intf_show_trace -w -i 4096'

skp_exec[16]='show ethernet v-ether db all global all location $location'
skp__ksh[16]='eth_intf_show_db -P 0x0 -G 4096 -F 0x0'

skp_exec[17]='show ethernet v-ether db all interface all'
skp__ksh[17]='eth_intf_show_db -P 0x0 -A'

skp_exec[18]='show ethernet driver trace wrapping location $location'
skp__ksh[18]='ethernet_trace_show -w -i 4096'

skp_exec[19]='show ethernet infra trace wrapping location $location'
skp__ksh[19]='ether_show_trace -w -i 4096'

skp_exec[20]='show vether-driver server error wrapping location $location'
skp__ksh[20]='easy_debug_ltrace -w -i 4096 -W 0 -X 0x0 -Y 0x0 -Z libvether_server'

skp_exec[21]='show vether-driver server event wrapping location $location'
skp__ksh[21]='easy_debug_ltrace -w -i 4096 -W 0 -X 0x1 -Y 0x0 -Z libvether_server'

skp_exec[22]='show vether-driver fsm error wrapping location $location'
skp__ksh[22]='easy_debug_ltrace -w -i 4096 -W 0 -X 0x2 -Y 0x0 -Z libvether_server'

skp_exec[23]='show vether-driver fsm event wrapping location $location'
skp__ksh[23]='easy_debug_ltrace -w -i 4096 -W 0 -X 0x3 -Y 0x0 -Z libvether_server'

skp_exec[24]='show optics trace all wrapping location $location'
skp__ksh[24]='optics_show_ltrace -A -w -i 4096'

skp_exec[25]='show ethernet controller trace wrapping location $location'
skp__ksh[25]='icpe_eth_ctrlr_show_trace -w -i 4096'

skp_exec[26]='show optics-driver trace driver all wrapping location $location'
skp__ksh[26]='optics_icpe_show_ltrace -C -w -i 4096'

skp_exec[27]='show optics-driver debug all location $location'
skp__ksh[27]='optics_debug_show -A -i 4096'

skp_exec[28]='show optics-driver trace icpe all wrapping location $location'
skp__ksh[28]='optics_icpe_show_ltrace -A -w -i 4096'

skp_exec[29]='show eapd optics trace all wrapping location $location'
skp__ksh[29]='optics_eapd_show_ltrace -A -w -i 4096'

skp_exec[30]='show vether-client client error wrapping location $location'
skp__ksh[30]='easy_debug_ltrace -w -i 4096 -W 0 -X 0x1 -Y 0x0 -Z libvether_client'

skp_exec[31]='show vether-client client event wrapping location $location'
skp__ksh[31]='easy_debug_ltrace -w -i 4096 -W 0 -X 0x0 -Y 0x0 -Z libvether_client'

skp_exec[32]='show vether-stats error wrapping location $location'
skp__ksh[32]='easy_debug_ltrace -w -i 4096 -W 0 -X 0x0 -Y 0x0 -Z libvether_stats_skp'

skp_exec[33]='show vether-stats event wrapping location $location'
skp__ksh[33]='easy_debug_ltrace -w -i 4096 -W 0 -X 0x1 -Y 0x0 -Z libvether_stats_skp'

skp_exec[34]='show controllers pm trace creation wrapping location $location'
skp__ksh[34]='show_skp_pm_ltrace -w -i 4096 -C'

skp_exec[35]='show controllers pm trace error wrapping location $location'
skp__ksh[35]='show_skp_pm_ltrace -w -i 4096 -R'

skp_exec[36]='show prm drv diagshell "version"'
skp__ksh[36]='prm_drv_show -c version'

skp_exec[37]='show prm drv diagshell "ps"'
skp__ksh[37]='prm_drv_show -c ps'

skp_exec[38]='show prm drv diagshell "phy info"'
skp__ksh[38]='prm_drv_show -c "phy info"'

skp_exec[39]='show prm drv diagshell "link"'
skp__ksh[39]='prm_drv_show -c link'

ncs540lrp_exec[1]='python /pkg/bin/phy_info_dump.py'
ncs540lrp__ksh[1]='python /pkg/bin/phy_info_dump.py'

show_controllers_optics(){
for j in `seq 0 7`;
    do
        /pkg/bin/show_sdr_sysdb -n | grep 0/$j/CPU0
        if [ $? -eq 0 ] 
            then
                echo "===================== Found LC$j ======================";
            echo
          # show controller optics command
                echo
                echo "================ Start of LC$j Optics ================="; 
                echo
                for i in `seq 0 35`;
                    do
                        echo "Optics Port Number = $i"
                        echo "----------------------"
                        echo "Running : show controller optics Optics0/$j/0/$i";
                        echo
                        /pkg/sbin/show_controller_optics -a 'Optics0_'$j'_0_'$i
                    done   
        fi
    done

    # show interface brief command
        echo
            echo "============ Start of show interface brief ============";
            echo
        /pkg/bin/show_interface -a -o 0x2;

    # show interface command
        echo
            echo "============ Start of show interface ============";
            echo
        /pkg/bin/show_interface -a;
}

show_diagshell_lc(){
echo "" > /tmp/diagshell_lc
for k in `seq 0 5`;
    do
       echo >> /tmp/diagshell_lc
          echo "Running : show controller fia diagshell $k ps location " >> /tmp/diagshell_lc
          /pkg/bin/fia_driver_show -c ps -u 0x$k -n $fq_nodeid >> /tmp/diagshell_lc

       echo >> /tmp/diagshell_lc
          echo "Running : show controller fia diagshell $k "diag nif" location" >> /tmp/diagshell_lc
          exec_diagnif_per_port_cmd 0x$k /tmp/diagshell_lc
    done
}

display() {
#Print opening heading
    print_main_heading "show tech-support optics"
    if [ "$__cardtype" == "SYS" ]; then
            exec_commands sys
    else

       TMP_PLAT=`/pkg/bin/get_platform_type`
       case "$__cardtype" in
            "RP")

               if [ $TMP_PLAT == 8000 ]
               then   
                   exec_commands sfrp
               else
                  #Platform check, returns 0 for NCS-5500/Fretta 
                  /pkg/bin/show_sdr_sysdb -n | grep "NCS-55\|NC55\|N540"
                  if [ $? -eq 0 ] || [ "$__iswhitebox" == "1" ] 
                      then
                          exec_commands rp
                  else 
                  #NCS-5000/Skywarp case
                      exec_commands skp 
                  fi  
               fi    
               
               if [ $TMP_PLAT == "NCS540L" ]
               then   
                   exec_commands ncs540lrp
               fi    
           ;;

           "LC")

               if [ $TMP_PLAT == 8000 ]
               then    
                   exec_commands sflc
               else
                  #LC only for NCS-5500/Fretta ; to skip for NCS-5000/Skywarp
                  /pkg/bin/show_sdr_sysdb -n | grep "NCS-55\|NC55\|N540\|NC57"
                  if [ $? -eq 0 ] || [ "$__iswhitebox" == "1" ]
                      then
                          show_diagshell_lc;
                          exec_commands lc
                          node_subdir=$__tar_file_directory_on_node/$node_name
                          if [ ! -d "$node_subdir" ]; then
                              /bin/mkdir -p $node_subdir
                          fi
                          led_save_ctrace $node_subdir
                  fi
               fi
           ;;
       esac
    fi

    print_main_heading "show tech-support optics complete"
}

. /pkg/bin/show_tech_file_fragment

