#!/pkg/bin/ksh
# -----------------------------------------------------------------------------
# show_tech_fast_bundle_common - Shared show tech-support fast script 
#                                for bundles
#
# July 2008, Anthony Toole
#
# Copyright (c) 2008-2019, 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 - card type and interface filter are the 
# only support options currently.  No need to use the default parser function,
# as this has been done by caller.
#
__cardtype="unspecified"
internal_if="unspecified"
external_if=""
ether_arg_string=""
bundle_only_arg_string=""

while [ "$#" -gt "0" ]; do
    case "$1" in
        -I) internal_if="$2"; shift 2;;
        -t) __cardtype="$2"; shift 2;;
        *) shift;;
    esac
done

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

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

node_name_internal=$showtech_node_name
node_number=`node_conversion -i $node_name_internal`


#
# List of commands to be run.  East 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 that run once per LR
###############################################################################
sys_exec[2]='show bundle'
sys__ksh[2]='bundlemgr_show -b'

sys_exec[3]='show bundle scheduled-actions'
sys__ksh[3]='bundlemgr_show -h'

sys_exec[1]='show bundle infrastructure ea'
sys__ksh[1]='bundlemgr_ea_show -b -a'

sys_exec[4]='show lacp'
sys__ksh[4]='bundlemgr_show -l'

sys_exec[5]='show lacp counters'
sys__ksh[5]='bundlemgr_show -c'

sys_exec[6]='show bundle infrastructure mac-allocation'
sys__ksh[6]='bundlemgr_show -d'

sys_exec[7]='show bundle infrastructure lacp-io'
sys__ksh[7]='bundlemgr_show -t'

sys_exec[8]='show lacp system-id all'
sys__ksh[8]='bundlemgr_show -s -G all'

sys_exec[9]='show bundle load-balancing brief'
sys__ksh[9]='bundlemgr_adj_show -b'

sys_exec[10]='show bundle infrastructure event'
sys__ksh[10]='bundlemgr_show -e'

sys_exec[11]='show mlacp verbose'
sys__ksh[11]='bundlemgr_show -m -O 4'

#
# 'show mlacp counters' executes 3 separate commands which we cannot do in 
# the script, so instead we will run each command individually
#
sys_exec[12]='show mlacp counters mbr-info'
sys__ksh[12]='bundlemgr_show -u -T member -G all'

sys_exec[13]='show mlacp counters bdl-info'
sys__ksh[13]='bundlemgr_show -u -T bundle -G all'

sys_exec[14]='show mlacp counters ig-info'
sys__ksh[14]='bundlemgr_show -u -T rg -G all'

sys_exec[15]='show bundle protect'
sys__ksh[15]='bundlemgr_show -n'

sys_exec[16]=''
sys__ksh[16]=''


###############################################################################
# Show commands that run on all RP or LC                                      #
###############################################################################
rplc_exec[1]='show bundle trace all'
rplc__ksh[1]='bundlemgr_distrib_show_ltrace -Y'

rplc_exec[2]='show processes bundlemgr_adj'
rplc__ksh[2]='sysmgr_show -o -p bundlemgr_adj'

rplc_exec[3]='show processes bundlemgr_local'
rplc__ksh[3]='sysmgr_show -o -p bundlemgr_local'

rplc_exec[4]=''
rplc__ksh[4]=''

###############################################################################
# Show commands that run on all RP only                                       #
###############################################################################

#
# RP only processes
#
rp_exec[1]='show processes bundlemgr_distrib'
rp__ksh[1]='sysmgr_show -o -p bundlemgr_distrib'

# assumes "platform" is set for us by show_tech_main_fragment
if [ "$platform" = "panini" ]
then
    rp_exec[2]='show processes ema_server_sdr'
    rp__ksh[2]='sysmgr_show -o -p ema_server_sdr'
else
    rp_exec[2]='show processes ema_server'
    rp__ksh[2]='sysmgr_show -o -p ema_server'
fi

rp_exec[3]='show bundle load-balancing detail'
rp__ksh[3]='bundlemgr_adj_show -d -h $node_number'

rp_exec[4]='show bundle infrastructure database ma'
rp__ksh[4]='bundlemgr_csl_show -d -P 0x0 -N $node_number -L 0x5'

rp_exec[5]='show bundle infrastructure in-progress ma'
rp__ksh[5]='bundlemgr_csl_show -o -P 0x0 -N $node_number -L 0x5'

#rp_exec[6]=''
#rp__ksh[6]=''
              
###############################################################################
# Show commands that run on all LC only                                       #
###############################################################################
lc_exec[1]='show processes bundlemgr_checker'
lc__ksh[1]='sysmgr_show -o -p bundlemgr_checker'

lc_exec[2]='show processes bundlemgr_ea'
lc__ksh[2]='sysmgr_show -o -p bundlemgr_ea'

lc_exec[3]='show bundle infrastructure database ea'
lc__ksh[3]='bundlemgr_csl_show -d -P 0x1 -N $node_number -L 0x5'

lc_exec[4]='show bundle infrastructure in-progress ea'
lc__ksh[4]='bundlemgr_csl_show -o -P 0x1 -N $node_number -L 0x5'

lc_exec[5]='show controllers np configSram'
lc__ksh[5]='show_config_sram $node_number'

lc_exec[6]=''
lc__ksh[6]=''

#
# The display() function is the one that does all the work - called by us as 
# this is a worker script.
#
display() {
    print_main_heading "show tech-support bundle common"
        
    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
    else
        case "$__cardtype" in
        "RP")
       
            #Build the array fo number of LCs.    
            if [ "$platform" = "panini" ]
            then

                num_lcs=$(show_platform_sysdb | grep "[0-9]*\/[0-9]*\/[0-9]* " -v | grep "[0-9]*\/[0-9]* " | cut -f 1 -d " " | awk '{printf "%s/CPU0\n", $0}'| wc -l)
                 lc_arr=$(show_platform_sysdb | grep "[0-9]*\/[0-9]*\/[0-9]* " -v | grep "[0-9]*\/[0-9]* " | cut -f 1 -d " " | awk '{printf "%s/CPU0\n", $0}')
            else
                num_lcs=$(shelfmgr_show_hfr -e | grep "[0-9]\/[0-9]" | cut -f 1 -d " " | grep "CPU0" | wc -l)
                 lc_arr=$(shelfmgr_show_hfr -e | grep "[0-9]\/[0-9]" | cut -f 1 -d " " | grep "CPU0")
            fi

            # Build exec array on rp 
            # Get current count of rp_exec array.
            let count=${#rp_exec[@]}

            #Get the bundle ids of the all the bundle in the system.
            bundle_ids=$(bundlemgr_show -b all | grep -F Bundle-)
            
            for bundle_id in ${bundle_ids[@]}
            do
                for loc in ${lc_arr[@]}
                do

                    node_string=`node_conversion -N $loc`
                    node_number=`node_conversion -i $node_string`
                
                    rp_exec[${count}]="show bundle load-balancing $bundle_id location $loc"
                    rp__ksh[${count}]="bundlemgr_adj_show -B $bundle_id -h $node_number"
                    let count=$((count + 1))

                    # assumes "platform" is set for us by show_tech_main_fragment
                    if [ "$platform" = "panini" ]
                    then
                        rp_exec[${count}]="show cef adjacency $bundle_id hardware egress location $loc"
                        rp__ksh[${count}]="fib_show_command -Z -O 0x0 -9 0x0 -i $bundle_id -H egress -N $node_number"
                        let count=$((count + 1))
                        rp_exec[${count}]="show cef adjacency $bundle_id hardware ingress location $loc"
                        rp__ksh[${count}]="fib_show_command -Z -O 0x0 -9 0x0 -i $bundle_id -H ingress -N $node_number"
                        let count=$((count + 1))
                    else
                        rp_exec[${count}]="show cef adjacency $bundle_id hardware egress location $loc"
                        rp__ksh[${count}]="fib_show_command -Z -O 0x0 -i $bundle_id -H egress -N $node_number"
                        let count=$((count + 1))
                        rp_exec[${count}]="show cef adjacency $bundle_id hardware ingress location $loc"
                        rp__ksh[${count}]="fib_show_command -Z -O 0x0 -i $bundle_id -H ingress -N $node_number"
                        let count=$((count + 1))
                    fi

                done
             done
      
            rp_exec[${count}]=''
            rp__ksh[${count}]=''
            
            exec_commands rplc
            exec_commands rp
            ;;
        "DRP")
            exec_commands rplc
            exec_commands rp
            ;;
        "LC")
            exec_commands rplc
            exec_commands lc
            ;;
        esac
    fi
    
    print_main_heading "show tech-support bundle common complete"
}

display
