#!/pkg/bin/ksh
# -----------------------------------------------------------------------------
# show_tech_fast_bundles - Show tech-support fast script for bundles
#
# July 2008, Anthony Toole
#
# Copyright (c) 2008-2016 by cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------------------

#
# OVERVIEW OF BUNDLE SHOW TECH FAST
#
# 'show_tech_fast' script is executed by parser, being passed whatever script
# we want to run as an argument - in this case 'show_tech_fast_bundles' (or the 
# ether/pos equivalents when they are written).  The end result is that the 
# 'show_tech_fast_bundles' script (or ether/pos equivalents) will be run on 
# each and every node in parallel - with arguments being setup to show which 
# card type it is being run on.
#
# The 'show_tech_fast_bundles' script will then call the _common and _only 
# scripts on all cards, and the sub-script will take whatever action it needs 
# to based on card type.
#
# Regarding card type, "SYS" corresponds to the commands that should be run 
# once per-system (as this is run within LR, on the dLRSC), and "RP", "LC", 
# etc. correspond to any card of that type in the chassis.  We'll get both SYS
# and RP callbacks on the dLRSC. 
#
# - 'show_tech_fast_bundles' is the main script for running parallel (aka. 
# fast) show tech bundles.  This will only be run directly by show tech infra.
#
# - 'show_tech_fast_bundle_common' is the script that will be called by both 
# 'show_tech_fast_bundles' and any other subsystem that needs it (so in due 
# course, at least the "fast" versions of the ether/pos show tech scripts.
#
# - 'show_tech_fast_bundle_only' is the script that will be called only by 
# 'show_tech_fast_bundles'
#
# At the time of writing, ether/pos do not support show-tech fast - and so this
# script has to work around that.  For now the non-fast scripts that ether/pos
# infra provide will be called once per system by this script (and so we lose
# some of the parallel benefit).  In reverse, the ether/pos equivalents of 
# 'show_tech_fast_bundles' will call the "slow" versions of this script, which
# are maintained in parallel.
#
# Once ether/pos have migrated this script can be changed to instead call the 
# ether/pos scripts on every node - in much the same was as the 
# 'show_tech_fast_common' and 'show_tech_fast_only' commands are called - and 
# will then decide what action to take based on the card type it is run on, and
# the interface filter.
#

#
# 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"
internal_if="unspecified"
external_if=""
arg_string=""
showtech_argv=""
showtech_argc="0"

while [ "$#" -gt "0" ]; do
    case "$1" in
        # Have ?? here to remove first two characters of variable
        -IBundle-*) internal_if="${1#??}"; shift 1;;
        -t) __cardtype="$2"; shift 2;;
        *) showtech_argv="$showtech_argv $1"; (( showtech_argc+=1 )); shift 1;;
    esac
done

#
# Call the showtech infra default parser - using set to recreate the $#, $@, 
# etc. - "--" is necessary so set doesn't try to interpret any of the 
# arguments we are passing through!
#
if [[ $showtech_argc -ne 0 ]]; then
    set -- $showtech_argv
    default_parser_function $@
fi

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 "$0: output file not specified"
    exit
fi

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

arg_string="$arg_string -t $__cardtype"

#
# 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[1]='show running-config'
sys__ksh[1]='nvgen -c -i1 -l 1 -t 1 -o 1'

# RP only, so use "location all"
sys_exec[2]='show redundancy location all'
sys__ksh[2]='redcon_show -n all'

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

sys_exec[4]='show interface summary'
sys__ksh[4]='show_interface -a -o 0x3'

sys_exec[5]='show install active'
# assumes "platform" is set for us by show_tech_main_fragment
if [ "$platform" = "panini" ]
then
    sys__ksh[5]='sdr_instcmd show install active'
else
    sys__ksh[5]='instcmd show install active'
fi

sys_exec[6]='show_tech_config_history script'
sys__ksh[6]='show_tech_config_history'

sys_exec[7]='show interface Bundle-*'
sys__ksh[7]='show_interface -i Bundle-*'

sys_exec[8]='show bundle status detail'
sys__ksh[8]='bundlemgr_hc -O 3'

sys_exec[9]=''
sys__ksh[9]=''

###############################################################################
# Interface specific show commands that run once per LR                       #
# (ie. global commands or those only runnable on RP)                          #
###############################################################################
sys_intf_exec[1]='show interfaces $external_if'
sys_intf__ksh[1]='show_interface -i $internal_if'

# BROKEN: this doesn't have "location all" option, nor can it be run on LC
sys_intf_exec[2]='show adjacency $external_if location all'
sys_intf__ksh[2]='aib_show_command -I $internal_if -A'

sys_intf_exec[3]='show imds interface $external_if'
sys_intf__ksh[3]='show_interface -i $internal_if'

# BROKEN: this doesn't have "location all" option, nor can it be run on LC
sys_intf_exec[4]='show cef $external_if'
sys_intf__ksh[4]='fib_show_command -t -O 0x0 -i $internal_if'

sys_intf_exec[5]=''
sys_intf__ksh[5]=''

###############################################################################
# Show commands that run on all RP or LC                                      #
###############################################################################
# assumes "platform" is set for us by show_tech_main_fragment
if [ "$platform" = "panini" ]
then
    rplc_exec[1]='show process blocked'
    rplc__ksh[1]='sh_proc_ng_blocked'
    # specify context 11 to display dumper context for last 10 crashes (not in CLI)
    rplc_exec[2]='show context'
    rplc__ksh[2]='corehelper_context -c 0x1'
else
    rplc_exec[1]='show process blocked'
    rplc__ksh[1]='show_processes -b'
    # specify context 11 to display dumper context for last 10 crashes (not in CLI)
    rplc_exec[2]='show context'
    rplc__ksh[2]='dumper_context -c 0xb'
fi

rplc_exec[3]=''
rplc__ksh[3]=''

###############################################################################
# Interface specific show commands that run on all RP or LC                   #
###############################################################################
rplc_intf_exec[1]='show im database verbose interface $external_if'
rplc_intf__ksh[1]='im_show database -n $internal_if -l 0x3'

rplc_intf_exec[2]='show netio chains $external_if'
rplc_intf__ksh[2]='netio_show -C -N $internal_if'

rplc_intf_exec[3]=''
rplc_intf__ksh[3]=''

#
# 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 bundles"
        
    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
        if [ "$internal_if" != "unspecified" ]; then
            exec_commands sys_intf
        fi
    else
        case "$__cardtype" in
        "DRP")
            exec_commands rplc
            
            if [ "$internal_if" != "unspecified" ]; then
                exec_commands rplc_intf
            fi
            ;;
        "RP")
            exec_commands rplc
            
            if [ "$internal_if" != "unspecified" ]; then
                exec_commands rplc_intf
            fi
            ;;
        "LC")
            exec_commands rplc
            
            if [ "$internal_if" != "unspecified" ]; then
                exec_commands rplc_intf
            fi
            ;;
        esac
    fi
    
    #
    # Run the other modules on all nodes - each can decide whether any
    # action is needed or not based on card type and interface filter.
    #
    run_module show_tech_fast_bundle_common "$arg_string"
    run_module show_tech_fast_ether_common "$arg_string" 
   
    ## show_tech_fast_chdlc is not supported on panini
    ## introduce a is_script_existing check and skip
    cmd="ls /pkg/bin/show_tech_fast_chdlc 2>/dev/null"
    eval $cmd
    plat=$?
    if [[ $plat -eq 0 ]]
    then
        run_module show_tech_fast_chdlc "$arg_string"
    fi

    run_module show_tech_fast_pos_common "$arg_string"
    run_module show_tech_ema_common "$arg_string"
     
    print_main_heading "show tech-support bundles complete"
}

#
# This function calls the display() function and sends the output to file if
# the file option has been set. 
#
. /pkg/bin/show_tech_file_fragment
