#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_pfc - Show tech-support fast script for pfc for NCS5500
#
# Mar 2020 - vvijayal
#
# Copyright (c) 2020-2021 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------

#
# Load the script provided by show-tech infra, which provides worker functions
#
. /pkg/bin/show_tech_main_fragment

#
# Parse the arguments to the script - in particular, to work out what node type
# we are running on and any interface filter.
#
# The args passed to this script seem to get easily mixed in with the show tech
# arguments, so make no assumption about ordering of arguments here - and pass
# anything unhandled over to showtech infra.
#
__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 [ "$__filename" = "unspecified" ]; then
    echo "show_tech_pfc: output file not specified"
    exit
fi

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

#
# List of commands to be run.  Each set must finish with a pair of empty
# strings.  Note that it is important to use single quotes rather than double
# quotes for the strings containing your commands.
#
# For all of these the __ksh variable is the process that will actually be
# spawned, the _exec variable is just a string that is printed in the output to
# describe it.
#

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

#############################################################
# Show commands that run once per system
#############################################################

cmd_index=1
sys_exec[$cmd_index]='show version'
sys__ksh[$cmd_index]='ng_show_version'

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

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

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

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

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

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

sys_exec[$cmd_index]='show redundancy location all'
sys__ksh[$cmd_index]='redcon_show -n all'
((cmd_index++))

# Interface
sys_exec[$cmd_index]='show im status summary'
sys__ksh[$cmd_index]='im_show status -l 0x0'
((cmd_index++))

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

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

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

sys_exec[$cmd_index]='show controllers intf priority-flow-control'
sys__ksh[$cmd_index]='ethernet_show_controller -A -s pfc'
((cmd_index++))

sys_exec[$cmd_index]='show policy-map interface all'
sys__ksh[$cmd_index]='qos_ma_show_stats -x -p 0x1 -q 0x2'
((cmd_index++))

sys_exec[$cmd_index]='show policy-map targets'
sys__ksh[$cmd_index]='policymgr_show -A -B 0x1'
((cmd_index++))

sys_exec[$cmd_index]='show configuration failed'
sys__ksh[$cmd_index]='cfgmgr_show_failed -c'
((cmd_index++))

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

sys_exec[$cmd_index]='show controller npu stats voq ingress interface all instance all location all'
sys__ksh[$cmd_index]='ofa_npu_stats_show -v a -i 0x10 -n A -t n -p A -s 0x0 -d 0x0'
((cmd_index++))

sys_exec[$cmd_index]=''
sys__ksh[$cmd_index]=''

#############################################################
# Commands that run on RP and LC nodes
cmd_index=1

rplc_exec[$cmd_index]='show controllers npu voq-usage interface all instance all location $location'
rplc__ksh[$cmd_index]='dpa_qosea_voq_show -v x -i 0x10 -n $fq_nodeid -t n -p 0x0'
((cmd_index++))

rplc_exec[$cmd_index]='show im database brief location $location'
rplc__ksh[$cmd_index]='im_show database -l 0x1 -h $fq_nodeid'
((cmd_index++))

rplc_exec[$cmd_index]='show process blocked'
rplc__ksh[$cmd_index]='show_processes -b'
((cmd_index++))

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

rplc_exec[$cmd_index]='show controllers npu priority-flow-control location $location'
rplc__ksh[$cmd_index]='dpa_qosea_pfc_show -n $fq_nodeid'
((cmd_index++))

rplc_exec[$cmd_index]=''
rplc__ksh[$cmd_index]=''

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

lc_exec[$lc_cmd_index]='show controllers fia trace all location $location'
lc__ksh[$lc_cmd_index]='fia_show_ltrace -A -i $fq_nodeid'
((lc_cmd_index++))

lc_exec[$lc_cmd_index]='show vether-ea trace all location $location'
lc__ksh[$lc_cmd_index]='dpa_vether_ea_show_ltrace -i $fq_nodeid -E -V'
((lc_cmd_index++))

lc_exec[$lc_cmd_index]='show qos ea trace all location $location'
lc__ksh[$lc_cmd_index]='qos_ea_show_ltrace -i $fq_nodeid -A'
((lc_cmd_index++))

lc_exec[$lc_cmd_index]='show processes qos_ea location $location'
lc__ksh[$lc_cmd_index]='sysmgr_show -o -p qos_ea -n $fq_nodeid'
((lc_cmd_index++))

lc_exec[$lc_cmd_index]='show processes qos_ma location $location'
lc__ksh[$lc_cmd_index]='sysmgr_show -o -p qos_ma -n $fq_nodeid'
((lc_cmd_index++))

lc_exec[$lc_cmd_index]=''
lc__ksh[$lc_cmd_index]=''

#
# The display() function is the one called by the show-tech infra to actually
# do the work of running the commands (it is run by show_tech_file_fragment, 
# called below).
#
# NOTE: our "sys" commands ideally want to be run once per LR.  But seems there
# is currently no way to detect this, so for now we duplicate the SYS handling
# on all RP cards - this means for each RP type node in an LR we'll duplicate 
# the information (not ideal, but better than losing data).
#

display() {
    print_main_heading "show tech-support platform-pfc"

    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
    else
        case "$__cardtype" in
        "RP")
            exec_commands rplc
            ;;
        "DRP")
            exec_commands rplc
            ;;
        "LC")
            exec_commands rplc
            exec_commands lc
            ;;
        esac
    fi

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