#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_fast_np_ls - Runs show commands for show tech-support np
#                        on Lightspeed based cards using the 'show_tech_fast'
#                        infrastructure.
# 
# Note that while this script is named "show_tech_fast_np_ls", the
# "router_name" field in the stanza for it in comp-mdata.pl causes
# the script to be installed on Lightspeed linecards as "show_tech_np".
#
# See EDCS-631313 for details on show_tech_fast
#
# Feb 2018, Alan Phillips
#
# Copyright (c) 2008-2010, 2012-2019 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------


#
# Get the number of NPs and the state of each.  Note that the state ends up in
# an array called NP_STATE.  The ksh syntax for doing something only if NP1 is
# up would then be:
#
# if [ "${NP_STATE[1]}" == "up" ];then
#     do stuff
# fi
#
get_np_state() {

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

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

        prm_np_show summary -s $fq_nodeid | grep -q -E "NP$npid.*Init Successful$"
        if [ $? == 0 ];then
            NP_STATE[$npid]="up"
        else
            NP_STATE[$npid]="down"
        fi

        ((npid++))

    done
}

#
# Init commands to be executed on both the RP and the LC.
# Note that while this script does not execute on the RP, this
# same function with the same commands is in the PRM component
# show_tech_np script and it does execute on the RP.
#
init_rplc_commands() {

    cmd_index=1
    
    rplc_exec[$cmd_index]='show cef vrf all summary location $ext_node_name'
    rplc__ksh[$cmd_index]='fib_show_command -S -O 0x0 -f all'
    
    ((cmd_index++))

    rplc_exec[$cmd_index]='show cef vrf all ipv6 summary location $ext_node_name'
    rplc__ksh[$cmd_index]='fib_show_command -S -O 0x1 -f all'
    
    ((cmd_index++))
    
    rplc_exec[$cmd_index]='show processes blocked location $ext_node_name'
    if [ "$OS_STRING" == "QNX" ];then
        rplc__ksh[$cmd_index]='show_processes -b'
    else
        rplc__ksh[$cmd_index]='sh_proc_ng_blocked'
    fi
    
    ((cmd_index++))
    
    rplc_exec[$cmd_index]='show memory summary location $ext_node_name'
    if [ "$OS_STRING" == "QNX" ];then
        rplc__ksh[$cmd_index]='show_memory -s'
    else
        rplc__ksh[$cmd_index]='show_memory_ng -s'
    fi

    ((cmd_index++))
    
    rplc_exec[$cmd_index]='show processes memory detail location $ext_node_name'
    if [ "$OS_STRING" == "QNX" ];then
        rplc__ksh[$cmd_index]='show_processes -m -h -t'
    else
        rplc__ksh[$cmd_index]='sh_proc_mem_cli -d'
    fi
    
    ((cmd_index++))

    rplc_exec[$cmd_index]='show watchdog threshold memory default location $ext_node_name'
    rplc__ksh[$cmd_index]='show_watchdog -d'

    ((cmd_index++))

    rplc_exec[$cmd_index]='show watchdog threshold memory configured location $ext_node_name'
    rplc__ksh[$cmd_index]='show_watchdog -c'

    ((cmd_index++))

    rplc_exec[$cmd_index]='show watchdog memory-state location $ext_node_name'
    rplc__ksh[$cmd_index]='show_watchdog'

    ((cmd_index++))

    rplc_exec[$cmd_index]='show memory heap summary all location $ext_node_name'
    if [ "$OS_STRING" == "QNX" ];then
        rplc__ksh[$cmd_index]='malloc_dump -A -s'
    else
        rplc__ksh[$cmd_index]='malloc_dump_wrapper -A -s'
    fi

    ((cmd_index++))

    rplc_exec[$cmd_index]='show shmwin summary location $ext_node_name'
    rplc__ksh[$cmd_index]='shmwin_show -N all -S'

    ((cmd_index++))

    if [ "$OS_STRING" == "Linux" ];then
        rplc_exec[$cmd_index]='run bash /etc/init.d/topproc -c 20'
        rplc__ksh[$cmd_index]='/bin/bash /etc/init.d/topproc -c 20'

        ((cmd_index++))
    fi

    if [ "$OS_STRING" == "QNX" ];then
        rplc_exec[$cmd_index]='run top -d -i 2 -t 2'
        rplc__ksh[$cmd_index]='top -d -i 2 -t 2'
    else
        rplc_exec[$cmd_index]='run top -b -n 2 -d 2'
        rplc__ksh[$cmd_index]='top -b -n 2 -d 2'
    fi
    
    ((cmd_index++))
    
    # MUST be the last command
    rplc_exec[$cmd_index]=''
    rplc__ksh[$cmd_index]=''
}

#
# Init commands related to the NPU server, show_np, etc.
#
init_npu_commands() {

    __npu_server_name="npu_server"
    __npu_server_pid=`sysmgr_show -o -p $__npu_server_name | grep -e PID | cut -c 27-`
    __npu_server_jid=`sysmgr_show -o -p $__npu_server_name | grep -e "Job Id" | cut -c 27-`

    #
    # Use cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #

    cmd_index=1
    
    npu_exec[$cmd_index]='show process $__npu_server_jid location $ext_node_name'
    npu__ksh[$cmd_index]='sysmgr_show -o -p $__npu_server_name -n $fq_nodeid'
    
    ((cmd_index++))
    
    npu_exec[$cmd_index]='follow process $__npu_server_pid iteration 2 location $ext_node_name'
    npu__ksh[$cmd_index]='attach_process -p $__npu_server_pid -i 2 -n $fq_nodeid'

    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np summary all location $ext_node_name'
    npu__ksh[$cmd_index]='prm_np_show summary -s $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='run show_np 0 revision '
    npu__ksh[$cmd_index]='show_np 0 revision '
    
    ((cmd_index++))

    npu_exec[$cmd_index]='run show_np 0 image_version '
    npu__ksh[$cmd_index]='show_np 0 image_version '
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np lightspeed die location $ext_node_name'
    npu__ksh[$cmd_index]='npu_server_ls_show -d -n $fq_nodeid'

    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np lightspeed hbm location $ext_node_name'
    npu__ksh[$cmd_index]='npu_server_ls_show -m -n $fq_nodeid'

    ((cmd_index++))
    
    npu_exec[$cmd_index]='show controllers np counters all location $ext_node_name'
    npu__ksh[$cmd_index]='prm_np_show counters -s $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show drops location $ext_node_name'
    npu__ksh[$cmd_index]='show_drops show -t 0x3 -s $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np fast-drop all location $ext_node_name'
    npu__ksh[$cmd_index]=' prm_np_show fast-drop -s $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np ports all location $ext_node_name'
    npu__ksh[$cmd_index]='prm_np_show ports -s $fq_nodeid'
    
    ((cmd_index++))
    
    npu_exec[$cmd_index]='show controllers np interrupts all all location $ext_node_name'
    npu__ksh[$cmd_index]='prm_np_show interrupts -n 999999 -s $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np load all location $ext_node_name'
    npu__ksh[$cmd_index]='prm_np_show np-load -s $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np server trace init location $ext_node_name'
    npu__ksh[$cmd_index]='show_npu_server_ltrace -N -i $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np server trace errors location $ext_node_name'
    npu__ksh[$cmd_index]='show_npu_server_ltrace -E -i $fq_nodeid'
    
    ((cmd_index++))
    
    npu_exec[$cmd_index]='show controllers np server trace events location $ext_node_name'
    npu__ksh[$cmd_index]='show_npu_server_ltrace -V -i $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np server trace interrupts location $ext_node_name'
    npu__ksh[$cmd_index]='show_npu_server_ltrace -I -i $fq_nodeid'
    
    ((cmd_index++))


    npu_exec[$cmd_index]='show controllers np client trace init location $ext_node_name'
    npu__ksh[$cmd_index]='npu_lib_show_ltrace -N -i $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np client trace errors location $ext_node_name'
    npu__ksh[$cmd_index]='npu_lib_show_ltrace -E -i $fq_nodeid'
    
    ((cmd_index++))
    
    npu_exec[$cmd_index]='show controllers np client trace events location $ext_node_name'
    npu__ksh[$cmd_index]='npu_lib_show_ltrace -V -i $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show pfm location $ext_node_name'
    npu__ksh[$cmd_index]='pfm_node_show -N -L $fq_nodeid'
    
    ((cmd_index++))

    # sbusdriver error traces
    npu_exec[$cmd_index]='show controllers np sbusdriver trace error hbm location $ext_node_name'
    npu__ksh[$cmd_index]='show_np_sbusdriver_ltrace -i $fq_nodeid -T 2 hbm'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np sbusdriver trace error lss location $ext_node_name'
    npu__ksh[$cmd_index]='show_np_sbusdriver_ltrace -i $fq_nodeid -T 2 lss'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np sbusdriver trace error tvs location $ext_node_name'
    npu__ksh[$cmd_index]='show_np_sbusdriver_ltrace -i $fq_nodeid -T 2 tvs'
    
    ((cmd_index++))
    
    # serdes16nm error traces
    npu_exec[$cmd_index]='show controllers np serdes16nm trace error lss location $ext_node_name'
    npu__ksh[$cmd_index]='show_np_avago16nm_ltrace -i $fq_nodeid -T 2 lss'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np serdes16nm trace error hbmi location $ext_node_name'
    npu__ksh[$cmd_index]='show_np_avago16nm_ltrace -i $fq_nodeid -T 2 hbmi'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np serdes16nm trace error hbme location $ext_node_name'
    npu__ksh[$cmd_index]='show_np_avago16nm_ltrace -i $fq_nodeid -T 2 hbme'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np serdes16nm trace error cpllio location $ext_node_name'
    npu__ksh[$cmd_index]='show_np_avago16nm_ltrace -i $fq_nodeid -T 2 cpllio'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers np serdes16nm trace error misio location $ext_node_name'
    npu__ksh[$cmd_index]='show_np_avago16nm_ltrace -i $fq_nodeid -T 2 misio'
    
    ((cmd_index++))

    if [ "$OS_STRING" == "QNX" ];then
        npu_exec[$cmd_index]='admin show logging onboard common location $ext_node_name'
        npu__ksh[$cmd_index]='obfl_exec_command show -A -F genstr -n $fq_nodeid'

        ((cmd_index++))

        npu_exec[$cmd_index]='admin show logging onboard error location $ext_node_name'
        npu__ksh[$cmd_index]='obfl_exec_command show -A -F errmsg -n $fq_nodeid'

        ((cmd_index++))

#   add OBFL support for eXR here 

    fi

    #
    # CCI commands
    #
    npu_exec[$cmd_index]='show controllers cpuctrl trace cih location $ext_node_name'
    npu__ksh[$cmd_index]='show_cci_common_ltrace -C -i $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers cpuctrl trace error location $ext_node_name'
    npu__ksh[$cmd_index]='show_cci_common_ltrace -E -i $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers cpuctrl trace event location $ext_node_name'
    npu__ksh[$cmd_index]='show_cci_common_ltrace -V -i $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers cpuctrl trace init location $ext_node_name'
    npu__ksh[$cmd_index]='show_cci_common_ltrace -N -i $fq_nodeid'
    
    ((cmd_index++))

    # CCI SHIP components are enabled on demand, so they may not be there.
    ship_components=`ship_show -i | grep cci`
    for one_comp in $ship_components;do

       npu_exec[$cmd_index]="run ship_show -c $one_comp"
       npu__ksh[$cmd_index]="ship_show -c $one_comp"

       ((cmd_index++))

    done

    #
    # RDM commands
    #
    npu_exec[$cmd_index]='show controllers rdm trace error location $ext_node_name'
    npu__ksh[$cmd_index]='show_rdm_ltrace -E -i $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers rdm trace event location $ext_node_name'
    npu__ksh[$cmd_index]='show_rdm_ltrace -V -i $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='show controllers rdm trace init location $ext_node_name'
    npu__ksh[$cmd_index]='show_rdm_ltrace -N -i $fq_nodeid'
    
    ((cmd_index++))

    npu_exec[$cmd_index]='run ship_show -c rdm'
    npu__ksh[$cmd_index]='ship_show -c rdm'
    
    ((cmd_index++))

    # MUST be the last command
    npu_exec[$cmd_index]=''
    npu__ksh[$cmd_index]=''
}

#
# Init commands to be executed for UIDB RM
#
init_uidb_rm_commands() {

    __uidb_server_name="uidb_server"
    __uidb_server_pid=`sysmgr_show -o -p $__uidb_server_name | grep -e PID | cut -c 27-`
    __uidb_server_jid=`sysmgr_show -o -p $__uidb_server_name | grep -e "Job Id" | cut -c 27-`

    #
    # Use cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #

    cmd_index=1
     
    uidb_rm_exec[$cmd_index]='show process $__uidb_server_jid location $ext_node_name'
    uidb_rm__ksh[$cmd_index]='sysmgr_show -o -p $__uidb_server_name -n $fq_nodeid'
    
    ((cmd_index++))

    uidb_rm_exec[$cmd_index]='follow process $__uidb_server_pid iteration 2 location $ext_node_name'
    uidb_rm__ksh[$cmd_index]='attach_process -p $__uidb_server_pid -i 2 -n $fq_nodeid'

   ((cmd_index++))
    
    uidb_rm_exec[$cmd_index]='show controllers rm uidb trace errors location $ext_node_name'
    uidb_rm__ksh[$cmd_index]='lightspeed_uidb_show_ltrace -R -i $fq_nodeid'
    
    ((cmd_index++))

    uidb_rm_exec[$cmd_index]='show controllers rm uidb trace events location $ext_node_name'
    uidb_rm__ksh[$cmd_index]='lightspeed_uidb_show_ltrace -E -i $fq_nodeid'
    
    ((cmd_index++))

    uidb_rm_exec[$cmd_index]='show controllers rm uidb trace init location $ext_node_name'
    uidb_rm__ksh[$cmd_index]='lightspeed_uidb_show_ltrace -I -i $fq_nodeid'
    
    ((cmd_index++))

    uidb_rm_exec[$cmd_index]='show controllers rm uidb trace pidb-trace debug location $ext_node_name'
    uidb_rm__ksh[$cmd_index]='lightspeed_pidb_show_ltrace -T 2 -i $fq_nodeid'
    
    ((cmd_index++))

    uidb_rm_exec[$cmd_index]='show controllers rm uidb trace pidb-trace errors location $ext_node_name'
    uidb_rm__ksh[$cmd_index]='lightspeed_pidb_show_ltrace -T 1 -i $fq_nodeid'
    
    ((cmd_index++))

    uidb_rm_exec[$cmd_index]='show controllers rm uidb trace server-errors location $ext_node_name'
    uidb_rm__ksh[$cmd_index]='lightspeed_uidb_show_ltrace -T -i $fq_nodeid'
    
    ((cmd_index++))

    uidb_rm_exec[$cmd_index]='show controllers rm uidb trace server-events location $ext_node_name'
    uidb_rm__ksh[$cmd_index]='lightspeed_uidb_show_ltrace -S -i $fq_nodeid'
    
    ((cmd_index++))

    uidb_rm_exec[$cmd_index]='show uidb index location $ext_node_name'
    uidb_rm__ksh[$cmd_index]='uidb_show index -s $fq_nodeid'
    
    ((cmd_index++))
    
    # MUST be the last command
    uidb_rm_exec[$cmd_index]=''
    uidb_rm__ksh[$cmd_index]=''
}

#
# Init commands to be executed for stats RM
#
init_stats_rm_commands() {

    __stats_server_name="ls_stats_svr"
    __stats_server_pid=`sysmgr_show -o -p $__stats_server_name | grep -e PID | cut -c 27-`
    __stats_server_jid=`sysmgr_show -o -p $__stats_server_name | grep -e "Job Id" | cut -c 27-`

    #
    # Use cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #

    cmd_index=1
     
    stats_rm_exec[$cmd_index]='show process $__stats_server_jid location $ext_node_name'
    stats_rm__ksh[$cmd_index]='sysmgr_show -o -p $__stats_server_name -n $fq_nodeid'
    
    ((cmd_index++))
    
    stats_rm_exec[$cmd_index]='follow process $__stats_server_pid iteration 2 location $ext_node_name'
    stats_rm__ksh[$cmd_index]='attach_process -p $__stats_server_pid -i 2 -n $fq_nodeid'

   ((cmd_index++))
     
    stats_rm_exec[$cmd_index]='show controllers rm stats trace client all error location $ext_node_name'
    stats_rm__ksh[$cmd_index]='ls_stats_show_ltrace -Call -E -i $fq_nodeid'
    
    ((cmd_index++))

    stats_rm_exec[$cmd_index]='show controllers rm stats trace client all info location $ext_node_name'
    stats_rm__ksh[$cmd_index]='ls_stats_show_ltrace -Call -I -i $fq_nodeid'
    
    ((cmd_index++))
     
    stats_rm_exec[$cmd_index]='show controllers rm stats trace server error location $ext_node_name'
    stats_rm__ksh[$cmd_index]='ls_stats_show_ltrace -S -E -i $fq_nodeid'
    
    ((cmd_index++))

    stats_rm_exec[$cmd_index]='show controllers rm stats trace server info location $ext_node_name'
    stats_rm__ksh[$cmd_index]='ls_stats_show_ltrace -S -I -i $fq_nodeid'
    
    ((cmd_index++))

    stats_rm_exec[$cmd_index]='show controllers rm stats client all location $ext_node_name'
    stats_rm__ksh[$cmd_index]='ls_stats_svr_show -c 0x100 -n $fq_nodeid'
    
    ((cmd_index++))

    stats_rm_exec[$cmd_index]='show controllers rm stats table all location $ext_node_name'
    stats_rm__ksh[$cmd_index]='ls_stats_svr_show -C 0x100 -n $fq_nodeid'
    
    ((cmd_index++))

    stats_rm_exec[$cmd_index]='show controllers rm stats resources npu all location $ext_node_name'
    stats_rm__ksh[$cmd_index]='ls_stats_svr_show -r -p 0x11 -n $fq_nodeid'
    
    ((cmd_index++))

    stats_rm_exec[$cmd_index]='run ls_stats_test metadata'
    stats_rm__ksh[$cmd_index]='ls_stats_test metadata'
    
    ((cmd_index++))
   
    # MUST be the last command
    stats_rm_exec[$cmd_index]=''
    stats_rm__ksh[$cmd_index]=''
}

#
# Init commands to be executed for L2RM
#
init_l2rm_commands() {

    __l2_server_name="ls_l2rm_svr"
    __l2_server_pid=`sysmgr_show -o -p $__l2_server_name | grep -e PID | cut -c 27-`
    __l2_server_jid=`sysmgr_show -o -p $__l2_server_name | grep -e "Job Id" | cut -c 27-`

    #
    # Use cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #

    cmd_index=1
     
    l2rm_exec[$cmd_index]='show process $__l2_server_jid location $ext_node_name'
    l2rm__ksh[$cmd_index]='sysmgr_show -o -p $__l2_server_name -n $fq_nodeid'
    
    ((cmd_index++))
    
    l2rm_exec[$cmd_index]='follow process $__l2_server_pid iteration 2 location $ext_node_name'
    l2rm__ksh[$cmd_index]='attach_process -p $__l2_server_pid -i 2 -n $fq_nodeid'

   ((cmd_index++))
    
    l2rm_exec[$cmd_index]='show controllers rm l2rm trace errors location $ext_node_name'
    l2rm__ksh[$cmd_index]='ls_l2rm_ltrace_show -E -i $fq_nodeid'
    
    ((cmd_index++))
     
    l2rm_exec[$cmd_index]='show controllers rm l2rm trace events location $ext_node_name'
    l2rm__ksh[$cmd_index]='ls_l2rm_ltrace_show -V -i $fq_nodeid'
    
    ((cmd_index++))

    l2rm_exec[$cmd_index]='sh controllers rm l2rm classify trace errors location $ext_node_name'
    l2rm__ksh[$cmd_index]='ls_l2rm_uidb_class_show_ltrace -E -i $fq_nodeid'

    ((cmd_index++))

    l2rm_exec[$cmd_index]='sh controllers rm l2rm classify trace events location $ext_node_name'
    l2rm__ksh[$cmd_index]='ls_l2rm_uidb_class_show_ltrace -V -i $fq_nodeid'

    ((cmd_index++))
     
    # MUST be the last command
    l2rm_exec[$cmd_index]=''
    l2rm__ksh[$cmd_index]=''
}

#
# Init commands to be executed for TRM
#
init_trm_commands() {

    __trm_server_name="trm_helper"
    __trm_server_pid=`sysmgr_show -o -p $__trm_server_name | grep -e PID | cut -c 27-`
    __trm_server_jid=`sysmgr_show -o -p $__trm_server_name | grep -e "Job Id" | cut -c 27-`

    #
    # Use cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #

    cmd_index=1
     
    trm_exec[$cmd_index]='show process $__trm_server_jid location $ext_node_name'
    trm__ksh[$cmd_index]='sysmgr_show -o -p $__trm_server_name -n $fq_nodeid'
    
    ((cmd_index++))
    
    trm_exec[$cmd_index]='follow process $__trm_server_pid iteration 2 location $ext_node_name'
    trm__ksh[$cmd_index]='attach_process -p $__trm_server_pid -i 2 -n $fq_nodeid'

   ((cmd_index++))
    
    trm_exec[$cmd_index]='show controllers rm trm trace api location $ext_node_name'
    trm__ksh[$cmd_index]='trm_show_ltrace -A -i $fq_nodeid'
    
    ((cmd_index++))
     
    trm_exec[$cmd_index]='show controllers rm trm trace errors location $ext_node_name'
    trm__ksh[$cmd_index]='trm_show_ltrace -E -i $fq_nodeid'
    
    ((cmd_index++))

    trm_exec[$cmd_index]='show controllers rm trm trace info location $ext_node_name'
    trm__ksh[$cmd_index]='trm_show_ltrace -I -i $fq_nodeid'
    
    ((cmd_index++))

    trm_exec[$cmd_index]='show controllers rm trm trace init location $ext_node_name'
    trm__ksh[$cmd_index]='trm_show_ltrace -N -i $fq_nodeid'
    
    ((cmd_index++))
     
    #
    # The following 2 commands do not work.
    #

    # trm_exec[$cmd_index]='show controllers rm trm trace server-errors location $ext_node_name'
    # trm__ksh[$cmd_index]='trm_show_ltrace -R -i $fq_nodeid'
    
    # ((cmd_index++))

    # trm_exec[$cmd_index]='show controllers rm trm trace server-info location $ext_node_name'
    # trm__ksh[$cmd_index]='trm_show_ltrace -S -i $fq_nodeid'
    
    # ((cmd_index++))

    trm_exec[$cmd_index]='show controllers rm trm trace warning location $ext_node_name'
    trm__ksh[$cmd_index]='trm_show_ltrace -W -i $fq_nodeid'
    
    ((cmd_index++))
     
    # MUST be the last command
    trm_exec[$cmd_index]=''
    trm__ksh[$cmd_index]=''
}

#
# Init commands to be executed for TCAM RM
#
init_tcam_rm_commands() {

    __tcam_rm_server_name="ls_tcam_svr"
    __tcam_rm_server_pid=`sysmgr_show -o -p $__tcam_rm_server_name | grep -e PID | cut -c 27-`
    __tcam_rm_server_jid=`sysmgr_show -o -p $__tcam_rm_server_name | grep -e "Job Id" | cut -c 27-`

    #
    # Use cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #

    cmd_index=1
     
    tcam_rm_exec[$cmd_index]='show process $__tcam_rm_server_jid location $ext_node_name'
    tcam_rm__ksh[$cmd_index]='sysmgr_show -o -p $__tcam_rm_server_name -n $fq_nodeid'
    
    ((cmd_index++))
    
    tcam_rm_exec[$cmd_index]='follow process $__tcam_rm_server_pid iteration 2 location $ext_node_name'
    tcam_rm__ksh[$cmd_index]='attach_process -p $__tcam_rm_server_pid -i 2 -n $fq_nodeid'

   ((cmd_index++))

    tcam_rm_exec[$cmd_index]='show controllers rm tcam sum all all np all location $ext_node_name'
    tcam_rm__ksh[$cmd_index]='ls_tcam_svr_show tcam -S -l 4 -a 22 -e 17 -s $fq_nodeid'
    ((cmd_index++))
    
    tcam_rm_exec[$cmd_index]='show controllers rm tcam trace error location $ext_node_name'
    tcam_rm__ksh[$cmd_index]='ls_tcam_show_ltrace -E -i $fq_nodeid'
    
    ((cmd_index++))

    tcam_rm_exec[$cmd_index]='show controllers rm tcam trace event location $ext_node_name'
    tcam_rm__ksh[$cmd_index]='ls_tcam_show_ltrace -V -i $fq_nodeid'
    
    ((cmd_index++))

    tcam_rm_exec[$cmd_index]='show controllers rm tcam trace init location $ext_node_name'
    tcam_rm__ksh[$cmd_index]='ls_tcam_show_ltrace -N -i $fq_nodeid'
    
    ((cmd_index++))

    tcam_rm_exec[$cmd_index]='show controllers rm tcam trace internal location $ext_node_name'
    tcam_rm__ksh[$cmd_index]='ls_tcam_show_ltrace -B -i $fq_nodeid'
    
    ((cmd_index++))
     
    # MUST be the last command
    tcam_rm_exec[$cmd_index]=''
    tcam_rm__ksh[$cmd_index]=''
}

#
# Init commands to be executed for PLU RM
#
init_plu_rm_commands() {

    __plu_rm_server_name="plu_bkg_main"
    __plu_rm_server_pid=`sysmgr_show -o -p $__plu_rm_server_name | grep -e PID | cut -c 27-`
    __plu_rm_server_jid=`sysmgr_show -o -p $__plu_rm_server_name | grep -e "Job Id" | cut -c 27-`

    #
    # Use cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #

    cmd_index=1
     
    plu_rm_exec[$cmd_index]='show process $__plu_rm_server_jid location $ext_node_name'
    plu_rm__ksh[$cmd_index]='sysmgr_show -o -p $__plu_rm_server_name -n $fq_nodeid'
    
    ((cmd_index++))
    
    plu_rm_exec[$cmd_index]='follow process $__plu_rm_server_pid iteration 2 location $ext_node_name'
    plu_rm__ksh[$cmd_index]='attach_process -p $__plu_rm_server_pid -i 2 -n $fq_nodeid'

   ((cmd_index++))

    plu_rm_exec[$cmd_index]='show controllers rm plu trace detail location $ext_node_name'
    plu_rm__ksh[$cmd_index]='show_ltrace_plu -D -i $fq_nodeid'
    
    ((cmd_index++))

    plu_rm_exec[$cmd_index]='show controllers rm plu trace error location $ext_node_name'
    plu_rm__ksh[$cmd_index]='show_ltrace_plu -R -i $fq_nodeid'
    
    ((cmd_index++))

    plu_rm_exec[$cmd_index]='show controllers rm plu trace event location $ext_node_name'
    plu_rm__ksh[$cmd_index]='show_ltrace_plu -E -i $fq_nodeid'
    
    ((cmd_index++))

    plu_rm_exec[$cmd_index]='show controllers rm plu trace lookup location $ext_node_name'
    plu_rm__ksh[$cmd_index]='show_ltrace_plu -L -i $fq_nodeid'
    
    ((cmd_index++))

    plu_rm_exec[$cmd_index]='show controllers rm plu counters location $ext_node_name'
    plu_rm__ksh[$cmd_index]='plu_show -d -n $fq_nodeid'
    
    ((cmd_index++))

    plu_rm_exec[$cmd_index]='show controllers rm plu counters hardware all location $ext_node_name'
    plu_rm__ksh[$cmd_index]='plu_show -d 1 PLUHWCNT 17 pluHWCNTALL -n $fq_nodeid'
    
    ((cmd_index++))
     
    # MUST be the last command
    plu_rm_exec[$cmd_index]=''
    plu_rm__ksh[$cmd_index]=''
}

#
# Init commands to be executed for EDRM
#
init_edrm_commands() {

    __edrm_server_name="edrm_svr"
    __edrm_server_pid=`sysmgr_show -o -p $__edrm_server_name | grep -e PID | cut -c 27-`
    __edrm_server_jid=`sysmgr_show -o -p $__edrm_server_name | grep -e "Job Id" | cut -c 27-`

    #
    # Use cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #

    cmd_index=1
     
    edrm_exec[$cmd_index]='show process $__edrm_server_jid location $ext_node_name'
    edrm__ksh[$cmd_index]='sysmgr_show -o -p $__edrm_server_name -n $fq_nodeid'
    
    ((cmd_index++))
    
    edrm_exec[$cmd_index]='follow process $__edrm_server_pid iteration 2 location $ext_node_name'
    edrm__ksh[$cmd_index]='attach_process -p $__edrm_server_pid -i 2 -n $fq_nodeid'

   ((cmd_index++))

    edrm_exec[$cmd_index]='show controllers rm edrm trace server error location $ext_node_name'
    edrm__ksh[$cmd_index]='edrm_show_ltrace -S 0 -E -i $fq_nodeid'
    
    ((cmd_index++))

    edrm_exec[$cmd_index]='show controllers rm edrm trace server info location $ext_node_name'
    edrm__ksh[$cmd_index]='edrm_show_ltrace -S 0 -I -i $fq_nodeid'
    
    ((cmd_index++))

    edrm_exec[$cmd_index]='show controllers rm edrm trace server init location $ext_node_name'
    edrm__ksh[$cmd_index]='edrm_show_ltrace -S 0 -N -i $fq_nodeid'
    
    ((cmd_index++))

    edrm_exec[$cmd_index]='show controllers rm edrm trace server mem location $ext_node_name'
    edrm__ksh[$cmd_index]='edrm_show_ltrace -S 0 -M -i $fq_nodeid'
    
    ((cmd_index++))

    edrm_exec[$cmd_index]='show controllers rm edrm trace server oir location $ext_node_name'
    edrm__ksh[$cmd_index]='edrm_show_ltrace -S 0 -O -i $fq_nodeid'
    
    ((cmd_index++))

    edrm_exec[$cmd_index]='show controllers rm edrm trace client group 1 member all error location $ext_node_name'
    edrm__ksh[$cmd_index]='edrm_show_ltrace -C -G 0x1 -B 0x100 -E -i $fq_nodeid'

    ((cmd_index++))
     
    # MUST be the last command
    edrm_exec[$cmd_index]=''
    edrm__ksh[$cmd_index]=''
}

#
# Init commands to be executed for UDMA RM
#
init_udma_rm_commands() {

    #
    # Use cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #

    cmd_index=1

    udma_rm_exec[$cmd_index]='show controllers rm udma trace error location $ext_node_name'
    udma_rm__ksh[$cmd_index]='show_udma_common_ltrace -E -i $fq_nodeid'
    
    ((cmd_index++))

    udma_rm_exec[$cmd_index]='show controllers rm udma trace event location $ext_node_name'
    udma_rm__ksh[$cmd_index]='show_udma_common_ltrace -V -i $fq_nodeid'
    
    ((cmd_index++))

    udma_rm_exec[$cmd_index]='show controllers rm udma trace init location $ext_node_name'
    udma_rm__ksh[$cmd_index]='show_udma_common_ltrace -N -i $fq_nodeid'
    
    ((cmd_index++))
     
    # MUST be the last command
    udma_rm_exec[$cmd_index]=''
    udma_rm__ksh[$cmd_index]=''
}

#
# Init commands to be executed for TBPG RM
#
init_tbpg_rm_commands() {

    #
    # Use cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #

    cmd_index=1

    tbpg_rm_exec[$cmd_index]='show controllers rm tbpg trace error location $ext_node_name'
    tbpg_rm__ksh[$cmd_index]='show_tbpg_common_ltrace -E -i $fq_nodeid'
    
    ((cmd_index++))

    tbpg_rm_exec[$cmd_index]='show controllers rm tbpg trace event location $ext_node_name'
    tbpg_rm__ksh[$cmd_index]='show_tbpg_common_ltrace -V -i $fq_nodeid'
    
    ((cmd_index++))

    tbpg_rm_exec[$cmd_index]='show controllers rm tbpg trace init location $ext_node_name'
    tbpg_rm__ksh[$cmd_index]='show_tbpg_common_ltrace -N -i $fq_nodeid'
    
    ((cmd_index++))
     
    # MUST be the last command
    tbpg_rm_exec[$cmd_index]=''
    tbpg_rm__ksh[$cmd_index]=''
}

#
# Init commands to be executed for ARL RM
#
init_arl_rm_commands() {

    #
    # Use cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #

    cmd_index=1

    arl_rm_exec[$cmd_index]='show controllers rm arl trace error location $ext_node_name'
    arl_rm__ksh[$cmd_index]='ls_arl_show_ltrace -E -i $fq_nodeid'
    
    ((cmd_index++))

    arl_rm_exec[$cmd_index]='show controllers rm arl trace info location $ext_node_name'
    arl_rm__ksh[$cmd_index]='ls_arl_show_ltrace -I -i $fq_nodeid'
    
    ((cmd_index++))

    # MUST be the last command
    arl_rm_exec[$cmd_index]=''
    arl_rm__ksh[$cmd_index]=''
}

#
# Init commands to be executed for QoS RM
#
init_qos_rm_commands() {

    #
    # Use cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #

    cmd_index=1

    qos_rm_exec[$cmd_index]='show qoshal trace location $ext_node_name'
    qos_rm__ksh[$cmd_index]='qoshal_show_ltrace -i $fq_nodeid'
    
    ((cmd_index++))

    qos_rm_exec[$cmd_index]='show qoshal resource summary location $ext_node_name'
    qos_rm__ksh[$cmd_index]='nphal_show_chk -p $fq_nodeid resource summary'
    
    ((cmd_index++))

    qos_rm_exec[$cmd_index]='show controllers np tm counters all location $ext_node_name'
    qos_rm__ksh[$cmd_index]='prm_np_show TM counters -s $fq_nodeid'
    
    ((cmd_index++))

    # MUST be the last command
    qos_rm_exec[$cmd_index]=''
    qos_rm__ksh[$cmd_index]=''
}

#
# This function executes commands that dump large volumes of data to 
# files.  Those files are deposited in the temporary working show tech
# directory (__tar_file_directory_on_node).   The show tech infra
# automatically sweeps up files left there as part of the tar file
# it creates.
#
# An example of something that we would dump to a file is a large
# quantity of register values.
#
dump_data_to_files() {

    if [ ! -d $__tar_file_directory_on_node ];then
        # Shouldn't happen, but just return if it does.
        return
    fi

    this_node=$(node_conversion -n $fq_nodeid | cut -c5-)
    qos_regs_file="qos_regs_$this_node$(date +__%Y:%m:%d_%H:%M:%S).txt"
    asic_errs_file="asic_errors_$this_node$(date +__%Y:%m:%d_%H:%M:%S).txt"

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

        if [ "${NP_STATE[$npid]}" != "up" ];then
            ((npid++))
            continue
        fi

        #
        # NPU server
        #
        # Dump the output of:
        #   show asic-errors np <npid> all location <>
        # to a file.
        #
        echo "NP$npid asic-errors information" >>  $__tar_file_directory_on_node/$asic_errs_file
        echo >> $__tar_file_directory_on_node/$asic_errs_file
        asic_errors_show -n $fq_nodeid -a 0x4 -i $npid -e 0x7 -s -t 0x0 -m 0x0 >> $__tar_file_directory_on_node/$asic_errs_file

        #
        # QoS RM
        #
        # Dump the output of:
        #   show controllers asic LS-NP instance <npid> 541 registers location <>
        # to a file.  This dumps about 60K lines per NP.
        #
        echo "NP$npid WRR Memory Registers" >>  $__tar_file_directory_on_node/$qos_regs_file
        echo >> $__tar_file_directory_on_node/$qos_regs_file
        gaspp_show -N -n $fq_nodeid -a $asic -i $npid -s wrrmem -r wrrQueueRecMemInst -I 0x0 -L 0x2000 -D noitem -t csrs >> $__tar_file_directory_on_node/$qos_regs_file
        gaspp_show -N -n $fq_nodeid -a $asic -i $npid -s wrrmem -r wrrPortRecMemInst -I 0x0 -L 0x2c -D noitem -t csrs >> $__tar_file_directory_on_node/$qos_regs_file
        gaspp_show -N -n $fq_nodeid -a $asic -i $npid -s wrrmem -r wrrSubportRecMemInst -I 0x0 -L 0x3ff -D noitem -t csrs >> $__tar_file_directory_on_node/$qos_regs_file

        ((npid++))

    done

    # NPU server
    if [ -f $__tar_file_directory_on_node/$asic_errs_file ];then
        gzip $__tar_file_directory_on_node/$asic_errs_file
    fi

    # QoS RM
    if [ -f $__tar_file_directory_on_node/$qos_regs_file ];then
        gzip $__tar_file_directory_on_node/$qos_regs_file
    fi

    #
    # ARL RM
    #
    # Dump the output of:
    #   show controllers rm arl table all npu all location <>
    # to a file.  The size of the file will depend on the config.  It
    # will be zero length if there are no table entries.
    #
    arl_tab_file="arl_table_$this_node$(date +__%Y:%m:%d_%H:%M:%S).txt"
    ls_arl_svr_show -p 0x8 -t 0x4 -n $fq_nodeid > $__tar_file_directory_on_node/$arl_tab_file
    gzip $__tar_file_directory_on_node/$arl_tab_file
}

#
# This function is executed on each linecard.  It collects the files from
# NP_DIR_ON_LC with filenames that include the linecard location on which
# the script is executing.
#
bundle_saved_np_files() {

    if [ ! -d $__tar_file_directory_on_node ];then
        # Shouldn't happen, but just return if it does.
        return
    fi

    if [ ! -d $NP_DIR_ON_LC ];then
        # If the NP directory doesn't exist, do nothing
        return
    fi

    this_node=$(node_conversion -n $fq_nodeid | cut -c5-)

    print_heading "Compressing and archiving the following files from $NP_DIR_ON_LC:"

    cd $NP_DIR_ON_LC
    file_cnt=0

    ls -lt *.txt *.log

    txt_file_list=`ls *.txt *.log`
    txt_gz_file_list=""
    for onefile in $txt_file_list;do
        gzip -c $onefile > $__tar_file_directory_on_node/$onefile.gz
	txt_gz_file_list="$txt_gz_file_list $onefile.gz"
        ((file_cnt++))
    done

    zip_file_list=`ls *.txt.gz *.log.gz`
    for onefile in $zip_file_list;do
        cp $onefile $__tar_file_directory_on_node/$onefile
        ((file_cnt++))
    done

    if [ $file_cnt == 0 ];then
        # If we found no files, just return
        return
    fi

    cd $__tar_file_directory_on_node
    tar cf np_dir_files-${this_node}.tar $txt_gz_file_list $zip_file_list
    for onefile in $txt_file_list;do
        rm $onefile.gz
    done
    for onefile in $zip_file_list;do
        rm $onefile
    done

    print_heading "Archive file np_dir_files-$this_node created"
}

#
# Init commands that must be done on a per-NP basis.  We skip NPs
# that are not up.
#
init_per_npu_commands() {

    #
    # Use cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #

    cmd_index=1

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

        if [ "${NP_STATE[$npid]}" != "up" ];then
            ((npid++))
            continue
        fi

        # per_npu_exec[$cmd_index]="run show_np -e $npid -d hist=60"
        # per_npu__ksh[$cmd_index]="show_np -e $npid -d hist=60"

        # ((cmd_index++))

        per_npu_exec[$cmd_index]="run np_perf -e $npid -l"
        per_npu__ksh[$cmd_index]="np_perf -e $npid -l"

        ((cmd_index++))

        per_npu_exec[$cmd_index]="run np_perf -e $npid -q"
        per_npu__ksh[$cmd_index]="np_perf -e $npid -q"

        ((cmd_index++))

        #
	# sbusdriver serdes dump all lines on the NP
	# *** These commands take a long time to complete
	# *** un-comment if much more detail is required
	#
        #per_npu_exec[$cmd_index]="run sbus_client_test -n $npid -r 5 -t 0"
        #per_npu__ksh[$cmd_index]="sbus_client_test -n $npid -r 5 -t 0"
	
        #((cmd_index++))
	
        #per_npu_exec[$cmd_index]="run cat /tmp/srds_$npid.dmp"
        #per_npu__ksh[$cmd_index]="cat /tmp/srds_$npid.dmp"
    
        #((cmd_index++))

        #per_npu_exec[$cmd_index]="run rm /tmp/srds_$npid.dmp"
        #per_npu__ksh[$cmd_index]="rm /tmp/srds_$npid.dmp"
    
        #((cmd_index++))
	
        # Next NP
        ((npid++))

    done

    per_npu_exec[$cmd_index]=""
    per_npu__ksh[$cmd_index]=""
}

# ********************************
#  Main part of script starts here 
# ********************************

#
# show_tech_main_fragment is part of the show tech infrastructure
# and contains a number of functions and ksh variables that are used
# within this script.  We execute show_tech_main_fragment to
# essentially "#include" those functions.
#
. /pkg/bin/show_tech_main_fragment

# Note that showtech_node_name comes from show_tech_main_fragment
int_node_name=$showtech_node_name
ext_node_name=`node_conversion -E $int_node_name`
fq_nodeid=`node_conversion -i $int_node_name`


#Get the asic id for LS asic
asic=`qosrm_get_asic -l 1`

OS_STRING=`uname -s`

# In Classic XR, the NP logs are stored in a disk on the RP that is
# accessible from both the LC and RP. In FleXR, the NP logs are stored
# on a disk that is on the LC and only accessible to the RP through a
# mountpoint.  
NP_DIR_ON_LC="/misc/scratch/np"

BIN_DIR=/pkg/bin
MCAST_PROC_NAME=ipv4_mfwd_partner

__cardtype="unspecified"

# 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

# The following function gets the number of NPs in a variable called NUM_NPS
# and the up/down state of each NP in an array called NP_STATE.  There
# is a comment above this function showing how to use the NP_STATE array.

get_np_state

#
# display() is the function called by the show tech infrastructure
# to collect the collect/display the component specific info.
#
# exec_commands # is a function provided by the show tech infrastructure.
#
display() {
    
    #
    # This script should only be executed on the LC.  It is only installed
    # on the LC.  The 'show tech np' script that runs on the RP is the
    # one from the platforms/viking/lc/prm component.
    #
    if [ "$__cardtype" == "LC" ]; then

        init_npu_commands

        init_per_npu_commands

        init_uidb_rm_commands

        init_stats_rm_commands

        init_l2rm_commands

        init_trm_commands

        init_tcam_rm_commands

        init_plu_rm_commands

        init_edrm_commands

        init_udma_rm_commands

        init_tbpg_rm_commands

        init_arl_rm_commands

        init_qos_rm_commands

        init_rplc_commands

        exec_commands npu

        exec_commands per_npu

        exec_commands uidb_rm

        exec_commands stats_rm

        exec_commands l2rm

        exec_commands trm

        exec_commands tcam_rm

        exec_commands plu_rm

        exec_commands edrm

        exec_commands udma_rm

        exec_commands tbpg_rm

        exec_commands arl_rm

        exec_commands qos_rm

        exec_commands rplc 

        dump_data_to_files

        bundle_saved_np_files
    fi
}


#
# show_tech_file_fragment is part of the show tech infrastructure and
# is the script that will call our display() function.  It also collects
# the output and writes it to a file if that file is specified.  It
# then compresses the file.
#
. /pkg/bin/show_tech_file_fragment

