#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_ls_tm - LS TM show tech command
#
# December 2017, Lightspeed-qos
#
# Copyright (c) 2017-2018 by cisco Systems, Inc.
# All rights reserved.
#----------------------------------------------------------------------
. /pkg/bin/show_tech_main_fragment


__cardtype="unspecified"
internal_if="unspecified"
external_if=""

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

if [ "$internal_if" != "unspecified" ]; then
    external_if=`convert_interface_fmt '-e' $internal_if`
    arg_string="$arg_string -I $internal_if"
fi

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

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

if [ "$__cardtype" == "LC" ]; then
    asic=`qosrm_get_asic -l 1`
fi

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

#############################################################
# Show commands that run once per system
sys_show_exec[1]='show version'
sys_show__ksh[1]='ng_show_version'
sys_show_exec[2]='show logging'
sys_show__ksh[2]='show_logging'
sys_show_exec[3]='show platform'
sys_show__ksh[3]='show_platform_sysdb'
sys_show_exec[4]='show install active'
sys_show__ksh[4]='sdr_instcmd show install active'
sys_show_exec[5]='show interfaces summary'
sys_show__ksh[5]='show_interface -a -o 0x3'
sys_show_exec[6]='show interfaces brief'
sys_show__ksh[6]='show_interface -a -o 0x2'
sys_show_exec[7]='show running-config'
sys_show__ksh[7]='nvgen -c -l 1 -t 1 -o 1'
sys_show_exec[8]='show policymap targets'
sys_show__ksh[8]='policymgr_show -A -B 0x1'
sys_show_exec[9]='show processes policymgr_rp'
sys_show__ksh[9]='sysmgr_show -o -p policymgr_rp'
sys_show_exec[10]='show uidb rm data-dump'
sys_show__ksh[10]='lightspeed_uidb_show data-dump -a'
sys_show_exec[11]='show bundle'
sys_show__ksh[11]='bundlemgr_show -b'
sys_show_exec[12]='show bundle ea members'
sys_show__ksh[12]='bundlemgr_ea_show -m -a'
sys_show_exec[13]='show run policy-map'
sys_show__ksh[13]='nvgen -c -q gl/policymgr/policymap/'
sys_show_exec[14]='show run class-map'
sys_show__ksh[14]='nvgen -c -q gl/policymgr/classmap/'
sys_show_exec[15]='show configuration failed'
sys_show__ksh[15]='cfgmgr_show_failed -c'
#endif

# Trace
sys_trace_exec[1]='show policymgr process trace all location all'
sys_trace__ksh[1]='show_ltrace_policymgr -i all -A'
sys_trace_exec[2]='show policy-lib trace all location all'
sys_trace__ksh[2]='show_ltrace_pllib -i all -A'
sys_trace_exec[3]='show qos-lib trace client qos_ea both location all'
sys_trace__ksh[3]='qos_lib_show_ltrace -i all -C qos_ea -W -U'
sys_trace_exec[4]='show qos-ma trace location all'
sys_trace__ksh[4]='show_qos_ma_ltrace -i all'
sys_trace_exec[5]='show qos-ea trace all location all'
sys_trace__ksh[5]='qos_ea_show_ltrace -i all -A'
#End

#############################################################
# Commands that run on RP and LC nodes

# Show commands

# Show procs
rplc_show_exec[1]='show process blocked'
rplc_show__ksh[1]='sh_proc_ng_blocked'
rplc_show_exec[2]='show processes qos_ma'
rplc_show__ksh[2]='sysmgr_show -o -p qos_ma'
#End

#############################################################
# Commands that run on all LCs
count=1

lc_exec[count]='show qos inconsistency summary location $location'
lc__ksh[count]='qos_ea_show_interface -a inconsistency -l $fq_nodeid'
let count+=1
lc_exec[count]='show qos inconsistency detail 0 location $location'
lc__ksh[count]='qos_ea_show_interface -a inconsistency -v 0x0 -l $fq_nodeid'
let count+=1
lc_exec[count]='show qos summary queue location $location output'
lc__ksh[count]='qos_ea_show_interface -u summary -t queue -d output -l $fq_nodeid'
let count+=1
#lc_exec[count]='show qoshal default-queue interface $external_if'
#lc__ksh[count]='nphal_show_chk default-queue -m $internal_if'
#let count+=1
lc_exec[count]='show controllers np counters all location $location'
lc__ksh[count]='prm_np_show counters -s $fq_nodeid'
let count+=1
lc_exec[count]='show processes qos_ma_ea location $location'
lc__ksh[count]='sysmgr_show -o -p qos_ma_ea -n $fq_nodeid'
let count+=1
lc_exec[count]='show qos-ea ha state location $location'
lc__ksh[count]='qos_ea_show_ha -p $fq_nodeid -S'
let count+=1
lc_exec[count]='show qos-ea ha chkpt all info $location'
lc__ksh[count]='qos_ea_show_ha -p $fq_nodeid -t 0x7 -I' 
let count+=1
lc_exec[count]='show qoshal resource summary location $location'
lc__ksh[count]='nphal_show_chk -p $fq_nodeid resource summary' 
let count+=1
lc_exec[count]='show qoshal trace location $location'
lc__ksh[count]='qoshal_show_ltrace -i $fq_nodeid'
let count+=1
lc_exec[count]='show controllers rm trm table name QOS-INTF-STR npu all detail all-entries location $location'
lc__ksh[count]='trm_show detail -f 0x41 -e 0xff -l $fq_nodeid'
let count+=1
lc_exec[count]='show controllers rm trm table name QOS-INTF-STR npu all summary location $location'
lc__ksh[count]='trm_show summary -f 0x41 -e 0xff -l $fq_nodeid'
let count+=1
lc_exec[count]='show processes qos_ma location $location'
lc__ksh[count]='sysmgr_show -o -p qos_ma -n $fq_nodeid'
let count+=1
# The following command are added again to check the counters update
lc_exec[count]='show qos summary queue location $location output'
lc__ksh[count]='qos_ea_show_interface -u summary -t queue -d output -l $fq_nodeid'
let count+=1
lc_exec[count]='show controllers np counters all location $location'
lc__ksh[count]='prm_np_show counters -s $fq_nodeid'  
let count+=1
lc_exec[count]='show controllers np interrupts all all location $location'
lc__ksh[count]='prm_np_show interrupts -n 999999 -s $fq_nodeid'  
let count+=1

#end
#let count+=1

#############################################################
#
# Commands that must be done on a per-NP basis.  So, we first find
# out what the list of NPs is, then we loop through and do the commands
# once for each NP.
#
per_np_lc_commands() {

    cmd_index=1

    num_nps=`prm_np_show summary -s $fq_nodeid | grep -E "NP[0-9]" | wc -l`

    npid=0; while [ "$npid" -lt "$num_nps" ];do

        ### Dumping TM block register BcmReg 
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance '"$npid"' 40 registers location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i '"$npid"' -s 0x28 -r block -I noitem -L 0x1 -D noitem'
        ((cmd_index++))
        ### Dumping TM block register ScmReg
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 41 registers location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x29 -r block -I noitem -L 0x1 -D noitem'
        ((cmd_index++))
        ### Dumping TM block register TcmReg
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 42 registers location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x2a -r block -I noitem -L 0x1 -D noitem'
        ((cmd_index++))
        ### Dumping TM block register PsmReg
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 43 registers location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x2b -r block -I noitem -L 0x1 -D noitem'
        ((cmd_index++))
        ### Dumping TM block register EqmReg
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 45 registers location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x2d -r block -I noitem -L 0x1 -D noitem'
        ((cmd_index++))
        ### Dumping TM block register WrrReg
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 124 registers location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x7c -r block -I noitem -L 0x1 -D noitem'
        ((cmd_index++))
        ### Dumping TM block register BcmMem
        #per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 457 registers location $location'
        #per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x1c9 -r block -I noitem -L 0x1 -D noitem'
        #((cmd_index++))

        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name bcmMem register-name bcmBudQueueProfile location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n 33568 -a $asic -i  '"$npid"' -s bcmMem -r bcmBudQueueProfile -I 0x0 -L 0x17ff -D noitem -t csrs'
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name bcmMem register-name bcmTailBudMem location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n 33568 -a $asic -i  '"$npid"' -s bcmMem -r bcmTailBudMem -I 0x0 -L 0xbfff -D noitem -t csrs '
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name bcmMem register-name bcmBudMem location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n 33568 -a $asic -i  '"$npid"' -s bcmMem -r bcmBudMem -I 0x0 -L 0x2ffff -D noitem -t csrs'
        ((cmd_index++))

        ### Dumping TM block register ScmMem
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 458 registers location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x1ca -r block -I noitem -L 0x1 -D noitem'
        ((cmd_index++))
        ### Dumping TM block register TcmMem
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 459 registers location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x1cb -r block -I noitem -L 0x1 -D noitem'
        ((cmd_index++))

        ### Dumping TM block register PsmMem
        #per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 460 registers location $location'
        #per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x1cc -r block -I noitem -L 0x1 -D noitem'
        #((cmd_index++))

        ### Dumping TM block register TomMem
        #per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 461 registers location $location'
        #per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x1cd -r block -I noitem -L 0x1 -D noitem'
        #((cmd_index++))

        ### Dumping TM block register WrrMem
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block wrrmem register wrrQueueRecMemInst location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i '"$npid"' -s wrrmem -r wrrQueueRecMemInst -I 0x0 -L 0x2000 -D noitem -t csrs'
        ((cmd_index++))

        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block wrrmem register wrrPortRecMemInst location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i '"$npid"' -s wrrmem -r wrrPortRecMemInst -I 0x0 -L 0x2c -D noitem -t csrs'
        ((cmd_index++))

        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block wrrmem register wrrSubportRecMemInst location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i '"$npid"' -s wrrmem -r wrrSubportRecMemInst -I 0x0 -L 0x3ff -D noitem -t csrs'
        ((cmd_index++))

        ### Dumping L2 register Elf0Reg
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 74 registers location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x4a -r block -I noitem -L 0x1 -D noitem'
        ((cmd_index++))
        ### Dumping EgtReg
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 118 registers location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x76 -r block -I noitem -L 0x1 -D noitem'
        ((cmd_index++))
        ### Dumping EgimReg
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 114 registers location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x72 -r block -I noitem -L 0x1 -D noitem'
        ((cmd_index++))
        ### Dumping IgimReg
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 106 registers location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x6a -r block -I noitem -L 0x1 -D noitem'
        ((cmd_index++))

        ### Dumping IgimMem
        #per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 523 registers location $location'
        #per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x20b -r block -I noitem -L 0x1 -D noitem'
        #((cmd_index++))

        # Dumping elf1Reg 
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' 76 registers location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -n $fq_nodeid -a $asic -i  '"$npid"' -s 0x4c -r block -I noitem -L 0x1 -D noitem'
        ((cmd_index++))

        # Dumping TomReg by Reg-names
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name tomReg register-name tomScratch location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i  '"$npid"' -s tomReg -r tomScratch -I noitem -L 0x1 -D noitem -t csrs'
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name tomReg register-name control location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i  '"$npid"' -s tomReg -r control -I noitem -L 0x1 -D noitem -t csrs'
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name tomReg register-name addr location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i  '"$npid"' -s tomReg -r addr -I noitem -L 0x1 -D noitem -t csrs'
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name tomReg register-name syndrome location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i  '"$npid"' -s tomReg -r syndrome -I noitem -L 0x1 -D noitem -t csrs'
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name tomReg register-name tomException location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i  '"$npid"' -s tomReg -r tomException -I noitem -L 0x1 -D noitem -t csrs'
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name tomReg register-name tomEnCtl location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i  '"$npid"' -s tomReg -r tomEnCtl -I noitem -L 0x1 -D noitem -t csrs'
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name tomReg register-name tomMemInit location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i  '"$npid"' -s tomReg -r tomMemInit -I noitem -L 0x1 -D noitem -t csrs'
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name tomReg register-name tomHbmBypMode location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i  '"$npid"' -s tomReg -r tomHbmBypMode -I noitem -L 0x1 -D noitem -t csrs'
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name tomReg register-name tomSch location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i  '"$npid"' -s tomReg -r tomSch -I noitem -L 0x1 -D noitem -t csrs'
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name tomReg register-name Cntr location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i  '"$npid"' -s tomReg -r Cntr -I noitem -L 0x1 -D noitem -t csrs'
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name tomReg register-name Status location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i  '"$npid"' -s tomReg -r Status -I noitem -L 0x1 -D noitem -t csrs'
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name tomReg register-name tomDbcModSel location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i  '"$npid"' -s tomReg -r tomDbcModSel -I noitem -L 0x1 -D noitem -t csrs'
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name tomReg register-name tomDbcOut 0 4 location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i  '"$npid"' -s tomReg -r tomDbcOut -I 0x0 -L 0x4 -D noitem -t csrs'
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name tomReg register-name tomEco location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i  '"$npid"' -s tomReg -r tomEco -I noitem -L 0x1 -D noitem -t csrs'
        ((cmd_index++))

        # EQM MEM dump with register-name 
        per_np_lc_exec[$cmd_index]='show controllers asic LS-NP instance  '"$npid"' block-name EqmMem register-name eqmQueueEnableMem location $location'
        per_np_lc__ksh[$cmd_index]='gaspp_show -N -n $fq_nodeid -a $asic -i  '"$npid"' -s EqmMem -r eqmQueueEnableMem -I 0x0 -L 0x600 -D noitem -t csrs'
        ((cmd_index++))
        per_np_lc_exec[$cmd_index]="run np_perf -e $npid -q"
        per_np_lc__ksh[$cmd_index]="np_perf -e $npid -q"

        ((cmd_index++))
    
        ((npid++))
    done

    per_np_lc_exec[$cmd_index]=""
    per_np_lc__ksh[$cmd_index]=""
}


#############################################################
# Commands that run on all RPs
#End
rp__ksh[0]=''

#############################################################
# Commands that run on all SCs
sc__ksh[0]=''

#############################################################
# Commands that run on all SPs
sp__ksh[0]=''

#############################################################
# Script

display() {
    print_main_heading "show tech-support ls-tm"
        
    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys_show
	exec_commands sys_trace
    else
        case "$__cardtype" in
        "RP")
            exec_commands rplc_show
            exec_commands rp
            ;;
        "DRP")
            exec_commands rplc_show
            exec_commands rp
            ;;
        "LC")
            per_np_lc_commands
            exec_commands rplc_show
            exec_commands lc
            exec_commands per_np_lc            
            ;;
        esac
    fi

    print_main_heading "show tech-support 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
