#! /pkg/bin/ksh
# ----------------------------------------------------------------------
# show_tech_ipt_fast -- show tech-support tunnel-template on fast path
#
# October 2008, Haihua Luo
#
# Copyright (c) 2008-2009, 2017-2018 by cisco Systems, Inc.
# All rights reserved.
#-----------------------------------------------------------------------

. /pkg/bin/show_tech_main_fragment

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

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

# ******************************************************************
#  Commands that run on SYS node (i.e. the node where the show tech
#  command is issued from)
# ******************************************************************

ipt_show_sys_exec[1]='show version'
if [[ "$platform" == "panini" ]]; then
    ipt_show_sys__ksh[1]='ng_show_version'
else
    ipt_show_sys__ksh[1]='show_version'
fi

if [[ "$platform" != "panini" ]]; then
    ipt_show_sys_exec[2]='show install which component tunnel-gre'
    ipt_show_sys__ksh[2]='instcmd show install which component tunnel-gre detail'
fi

if [[ "$platform" != "panini" ]]; then
    ipt_show_sys__ksh[3]=''
else
    ipt_show_sys__ksh[2]=''
fi
# ******************************************************************
#  Commands that run on all (D)RPs in a multi-chassis system
# ******************************************************************

ipt_show_rp_exec[1]='show running-config tunnel-template'
ipt_show_rp__ksh[1]='nvgen -c -q gl/tunnel/template/'

ipt_show_rp_exec[2]='show tunnel-template'
ipt_show_rp__ksh[2]='ipt_bis_show -a'

ipt_show_rp_exec[3]='run ipt_bis_show -i'
ipt_show_rp__ksh[3]='ipt_bis_show -i'

ipt_show_rp_exec[4]='run ipt_bis_show -a -d'
ipt_show_rp__ksh[4]='ipt_bis_show -a -d'

ipt_show_rp_exec[5]='show processes ip_tunnel_ma'
ipt_show_rp__ksh[5]='sysmgr_show -o -p ip_tunnel_ma'

ipt_show_rp_exec[6]='show tunnel-ip trace ma all'
ipt_show_rp__ksh[6]='ip_tunnel_ma_show_ltrace -A'

ipt_show_rp_exec[7]='show cef recursive-nexthop'
ipt_show_rp__ksh[7]='fib_show_command -O 0x0 -D'

# Biscuit issue can be caused by BGP
ipt_show_rp_exec[8]='show bgp ipv4 tunnel'
ipt_show_rp__ksh[8]='bgp_show -V default -A 0x1 -W 0x40 -t'

ipt_show_rp_exec[9]='show tunnel ip trace bgp'
ipt_show_rp__ksh[9]='ip_tunnel_client_show_ltrace bgp'

ipt_show_rp_exec[10]='show tunnel-ip trace ea all location $location'
ipt_show_rp__ksh[10]='ip_tunnel_ea_show_ltrace -A'

ipt_show_rp__ksh[11]=''

# ******************************************************************
#  Commands that run on all RPs (in multi-chassis system) and LCs
# ******************************************************************
ipt_show_rplc_exec[1]='show processes blocked'
if [[ "$platform" == "panini" ]]; then
    ipt_show_rplc__ksh[1]='sh_proc_ng_blocked'
else
    ipt_show_rplc__ksh[1]='show_processes -b'
fi

# Need to change executable name to ip_tunnel_ea in 3.9
ipt_show_rplc_exec[2]='show processes gre_ipip_ea'
ipt_show_rplc__ksh[2]='sysmgr_show -o -p gre_ipip_ea'

ipt_show_rplc_exec[3]='show processes netio'
ipt_show_rplc__ksh[3]='sysmgr_show -o -p netio'

ipt_show_rplc_exec[4]='show processes ifmgr'
ipt_show_rplc__ksh[4]='sysmgr_show -o -p ifmgr'

ipt_show_rplc__ksh[5]=''

# ******************************************************************
#  Commands that run on SC nodes
# ******************************************************************
sc__ksh[0]=''

# ******************************************************************
#  Commands that run on SP nodes
# ******************************************************************
sp__ksh[0]=''

# This is good for tracing whether Biscuit changes are sent down to FIB
# Unfortunately, the fib show ltrace client runs only on RP. So we can't
# make it run in parallel from all nodes. Luckily they do offer an 
# "location all" option
#
# The reason to make it a function is that the pipe is not well supported
# in array definitions
check_fib() {
    print_heading "show cef trace event wrapping location all | include src_addr_pool_len" "-"
    show_fib_trace -i all -P 0x0 -T 0x0 | grep -E src_addr_pool_len
}

# Tracing Biscuit MA/EA interactions. IM traces wrap too quickly
# so we do this ourselves
check_ma_ea_interaction () {
    print_heading "show im trace op all | include TUNNEL" "-"
    im_show_trace -C -P -A -D -R -G | grep -E TUNNEL
}

# Find out what the MA is up to at this moment ...
follow_ma() {
    ma_pid=`sysmgr_show -o -p ip_tunnel_ma | grep -e PID | cut -c 27-`

    print_heading "follow process $ma_pid all Verbose Iteration 1" "-"
    attach_process -p $ma_pid -A -i 1 -v
}

# Find out what the EAs are up to at this moment ...
follow_ea() {
    ea_pid=`sysmgr_show -o -p gre_ipip_ea | grep -e PID | cut -c 27-`

    print_heading "follow process $ea_pid all Verbose Iteration 1" "-"
    attach_process -p $ea_pid -A -i 1 -v
}

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

display() {
    print_main_heading "show tech-support tunnel-template"

    if [ "$__cardtype" == "SYS" ]; then
        exec_commands ipt_show_sys
    else
        case "$__cardtype" in
        "DRP")
            exec_commands ipt_show_rp
            exec_commands ipt_show_rplc
            follow_ma
            check_fib
            check_ma_ea_interaction
            ;;
        "RP")
            exec_commands ipt_show_rp
            exec_commands ipt_show_rplc
            follow_ma
            check_fib
            check_ma_ea_interaction
            ;;
        "LC")
            exec_commands ipt_show_rplc
            follow_ea
            check_ma_ea_interaction
            ;;
        esac
    fi

    print_main_heading "show tech-support tunnel-template completed"
}
# 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

