#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_ether_pd - Shared show tech-support script for ethernet
#
# July 2008, Scott Paulsen
#
# Copyright (c) 2008-2009 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------


# This script fragment contains the code for the following functions
# - print_main_heading
# - print_command_heading
# - run_single_command
# - run_commands
# - run_single_command_on_all_nodes
# - run_commands_on_all_nodes
# - default_parser_function
. /pkg/bin/show_tech_main_fragment

# List each set of show commands to be run. Each set must finish with an empty
# string. Note that it is important to use single quotes rather than double 
# quotes for the strings containing your commands. 

# Command 1 - traces on rsp
command1[0]='show ether-ctrl trace location $location'
command1[1]=''

# Command 2 - Global show commands on rsp
command2[0]='show processes ether_ctrl_mgmt location $location'
command2[1]='show controllers mlan  port-counters  1 location $location'
command2[2]='show controllers mlan  port-counters  2 location $location'
command2[3]='show controllers mlan  port-counters  3 location $location'
command2[4]='show controllers mlan   port-status  1 location $location'
command2[5]='show controllers mlan   port-status  2 location $location'
command2[6]='show controllers mlan   port-status  3 location $location'
command2[7]='show controllers mlan   switch-atu  location $location'
command2[8]='show controllers mlan   switch-status  location $location'
command2[9]=''

# Command 3 - traces on lc
command3[0]='show ether-ctrl trace location $location'
command3[1]='show prm server trace error location $location'
command3[2]='show controllers fca ltrace error location $location'
command3[3]='show controllers fabric fia trace location $location'
command3[4]='show controllers tengig-portctrl trace location $location'
command3[5]='show controllers tengig-phyctrl trace location $location'
command3[6]='show controllers Gig-PortCtrl PortCtrl-trace location $location'
command3[7]='show controllers Gig-PhyCtrl PhyCtrl-trace location $location'
command3[8]='show serdes trace location $location'
command3[9]='show wanphy trace error location $location'
command3[10]='show wanphy trace info location $location'
command3[11]='show controllers tenGig-portctrl   port-stats location $location'
command3[12]='show controllers tenGig-phyctrl port-stats location $location'
command3[13]='show controllers Gig-PortCtrl port-stats location $location'
command3[14]='show controllers Gig-PhyCtrl  port-stats location $location'
command3[15]=''

# Command 4 - Global show commands for lc
command4[0]='show processes ether_ctrl_lc_ge location $location'
command4[1]='show processes ether_ctrl_lc_10ge location $location'
command4[2]='show processes prm_server location $location'
command4[3]='show processes fca_server location $location'
command4[4]=''


# Initialise any variables used.

internal_if="unspecified"
external_if=""
vrf_string=""
trace_only="0"
show_only="0"
append_opt="-a"

# Parse the arguments to the script.
# Usage:
#
# -I  : Run commands for a specific interface
# -V  : Specify a specific VRF
# -T  : Collect trace only
# -S  : Collect show commands only
while [ $# -gt 0 ]; do
  case "$1" in
     -I) internal_if="$2"; shift 2;;
     -V) vrf_string="vrf $2"; shift 2;;
     -T) trace_only="1"; shift 1;;
     -S) show_only="1"; shift 1;;
     *)  default_parser_function "$@"; shift $#;;
  esac
done

# Function: get_node_XXX
# 
# This gets the list of nodes in the format
# r/s/i and stores them in the __location array. 
# It also sets __number_of_nodes to the number of nodes and __nodelist_set
# to "TRUE". 
# 
# It takes no arguments.  
get_node_list_rsp() {
    # Get the list of nodes in the format node_r_s_i by looking at the /net 
    # directory then turn this into an external node name.
    __i=0
    for __node_name in /net/*; do
        __node_names[$__i]="$__node_name"
        __nodeid[$__i]=`node_conversion -i $__node_name`
        if [[ "${__node_names[$__i]}" = /net/node0_RSP* ]]
        then
            __nodeid_valid[$__i]=$?
            if [ "${__nodeid_valid[$__i]}" = "0" ]; then
              __location[$__i]=`node_conversion -e ${__nodeid[__i]}`
              __location_valid[$__i]=$?
              if [ "${__location_valid[$__i]}" != "0" ]; then
                echo "Failed to obtain location for ${__nodeid[__i]}"
              fi
            else
              echo "Failed to obtain location for $__node_name" 
            fi
            __i=$(($__i + 1))
        fi
    done
    __location_valid[$__i]=1
    __nodeid_valid[$__i]=1

    __number_of_nodes=$__i
    __nodelist_set="TRUE"
}

get_node_list_lc() {
    # Get the list of nodes in the format node_r_s_i by looking at the /net 
    # directory then turn this into an external node name.
    __i=0
    for __node_name in /net/*; do
        __node_names[$__i]="$__node_name"
        __nodeid[$__i]=`node_conversion -i $__node_name`
        if [[ "${__node_names[$__i]}" != /net/node0_RSP* ]]   
        then
            __nodeid_valid[$__i]=$?
            if [ "${__nodeid_valid[$__i]}" = "0" ]; then
              __location[$__i]=`node_conversion -e ${__nodeid[__i]}`
              __location_valid[$__i]=$?
              if [ "${__location_valid[$__i]}" != "0" ]; then
                echo "Failed to obtain location for ${__nodeid[__i]}"
              fi
            else
              echo "Failed to obtain location for $__node_name" 
            fi
            __i=$(($__i + 1))
        fi
    done
    __location_valid[$__i]=1
    __nodeid_valid[$__i]=1

    __number_of_nodes=$__i
    __nodelist_set="TRUE"
}

# A function called display() must be provided that calls the functions to 
# run the required show commands. The display() function will be called in 
# the /pkg/bin/show_tech_comp_file_frag

display() {

    # Print the output heading 
    print_main_heading "show tech-support ethernet PD"

    if [ "$show_only" = "0" ]; then
        if [ "$location" = "all" ]; then
            get_node_list_rsp
            run_commands_on_all_nodes 1
            run_commands_on_all_nodes 2

            get_node_list_lc
            run_commands_on_all_nodes 3
            run_commands_on_all_nodes 4
        else
            if [ "$location" = 0/RSP* ]; then
                run_commands 1
                run_commands 2
            else 
                run_commands 3
                run_commands 4
            fi
        fi
    fi

    # Print the closing heading. 
    print_main_heading "show tech-support ethernet PD complete"
}

# Just call the display() function
display

