#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# dot1x_show_techsupport - Show tech-support script for dot1x
#
# Dec 2016, Lalitesh M
#
# Copyright (c) 2014-2024 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.

__cardtype="unspecified"

# Set the default values of the file name for which show tech-support macsec 
# is to run and the file it is to write to.
# Read in the arguments to the script, setting node_required and filename
# according to these arguments.
# Note that it is important for security reasons that users can only enter
# alphanumeric filenames and nodes and that anywhere calling this script must
# enforce this.
while [ "$#" -gt "0" ]; do
    case "$1" in
        -t) __cardtype="$2"; shift 2;;
        *)  default_parser_function "$@"; shift $#;;
    esac
done

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

 # global commands on RP


###############################################################################
# Show commands that run once per LR                                          #
###############################################################################


sys_exec[1]='show install active'
if [[ "$platform" != "viking" ]]; then
sys__ksh[1]='sdr_instcmd show install active'
else
sys__ksh[1]='instcmd show install active'
fi

if [[ "$platform" != "viking" ]]; then
sys_exec[2]='show install repository'
sys__ksh[2]='sdr_instcmd show install repository'
else
sys_exec[2]='show install inactive'
sys__ksh[2]='instcmd show install inactive'
fi

sys_exec[3]='show version'
if [[ "$platform" != "viking" ]]; then
sys__ksh[3]='ng_show_version'
else
sys__ksh[3]='show_version'
fi

sys_exec[4]='show platform'
if [[ "$platform" != "viking" ]]; then
sys__ksh[4]='show_platform_sysdb'
else
sys__ksh[4]='show_platform_vkg'
fi

sys_exec[5]=''
sys__ksh[5]=''


###############################################################################
# Show commands that run on every RP                                          #
###############################################################################


rp_exec[1]='show logging'
rp__ksh[1]='show_logging'

rp_exec[2]='show redundancy location all'
rp__ksh[2]='redcon_show -n all'

rp_exec[3]='show context location all'
if [[ "$platform" == "panini" ]]; then 
    rp__ksh[3]='corehelper_context -c 0x1 -n all'
else
    rp__ksh[3]='dumper_context -c 0x1 -n all'
fi

rp_exec[4]='show running-config'
rp__ksh[4]='nvgen -c -l 1 -t 1 -o 1'

rp_exec[5]='show ip int brief'
rp__ksh[5]='show_ip_interface -b -v all'

rp_exec[6]='show process dot1x location all'
rp__ksh[6]='sysmgr_show -o -p dot1x -n all'

rp_exec[7]='show dot1x detail'
rp__ksh[7]='show_dot1x -d detail-all'

rp_exec[8]='show dot1x statistics'
rp__ksh[8]='show_dot1x -t all_stats'

rp_exec[9]='show dot1x trace all location $location'
rp__ksh[9]='show_dot1x_ltrace -i $fq_nodeid -A'

rp_exec[10]='show crypto ca certificates'
rp__ksh[10]='pki_cmd show certificates brief'

rp_exec[11]='show crypto ca certificates detail'
rp__ksh[11]='pki_cmd show certificates detail'

rp_exec[12]='show dot1x summary'
rp__ksh[12]='show_dot1x -m'

rp_exec[13]='sh operational Dot1x Statistics InterfaceStatisticsTable'
rp__ksh[13]='show_operational "Dot1x" "Statistics" "InterfaceStatisticsTable" "_EOT_" "Dot1x" "Statistics" "InterfaceStatisticsTable"'

rp_exec[14]='show mab summary'
rp__ksh[14]='show_mab "-s" "-n" "-o"'

rp_exec[15]='show mab detail'
rp__ksh[15]='show_mab "-s" "-n" "-d" "-o"'

rp_exec[16]='sh operational MAB NodeTable'
rp__ksh[16]='show_operational "MAB" "NodeTable" "_EOT_" "MAB" "NodeTable"'

rp_exec[17]='sh operational MAB Interfaces'
rp__ksh[17]='show_operational "MAB" "Interfaces" "_EOT_" "MAB" "Interfaces"'

rp_exec[18]='show crypto key mypubkey rsa'
rp__ksh[18]='ce_cmd show key myrsa nooption'

rp_exec[19]='show im database brief location all'
rp__ksh[19]='im_show database -l 0x1 -a'

rp_exec[20]='show mab statistics location $location'
rp__ksh[20]='show_mab -s -f -n -l $fq_nodeid'

rp_exec[21]=''
rp__ksh[21]=''

##############################################################################
# Show commands of macsec stats that run on every LC                       #
###############################################################################

lc_exec[1]='show dot1x trace all location $location'
lc__ksh[1]='show_dot1x_ltrace -i $fq_nodeid -A'

lc_exec[2]='show dot1x statistics location $location'
lc__ksh[2]='show_dot1x -t loc -l $fq_nodeid'

lc_exec[3]='show mab statistics location $location'
lc__ksh[3]='show_mab -s -f -n -l $fq_nodeid'

lc_exec[4]=''
lc__ksh[4]=''

###############################################################################
# Show commands for collecting dot1x interface stats                          #
###############################################################################
i=1

# find fq_nodeid for the node
# QNX does not provide node_list_generation but 'uname -n' provides the node name
# Linux supports node_list_generation and node_conversion to get nodename
# QNX is used by classic xr and most of other platforms is using Linux
if type node_list_generation >/dev/null 2>&1; then
    node_name=$(node_conversion -N $(node_list_generation -f MY))
else
    node_name=$(uname -n)
fi

fq_nodeid=`node_conversion -i $node_name`

# Get the list of interfaces registered in this node from IM.
# Using the, show  im database location <> view owner, cmd.
#
# Grep the lines starting with, Interface <interface name in given format>.
# Remove useless stuff
#
# store list of such interfaces obtained in intf_list_arr, 
# echo $intf_list_arr, for node_0_1_CPU0 may give something like.. /
# GigabitEthernet0/1/0/0 
# GigabitEthernet0/1/0/1 
# GigabitEthernet0/1/0/2
# GigabitEthernet0/1/0/3
# GigabitEthernet0/1/0/4
intf_list_arr=$(im_show "database" "-v" "0x0" "-l" "0x2" "-h" $fq_nodeid | more "-f" |\
    grep -E "^Interface (Ten|Twenty|Forty|Hundred|Gigabit|Four|Fifty|Two|TwentyFive).*([0-9]+(/[0-9]+)+)" |\
    sed 's/,//g' | cut -d ' ' -f2)

intf_list_arr=$(echo $intf_list_arr |tr '\n' ' '|sed 's/ /#\n/g')
	
# Get the list of interfaces in which we have dot1x running.
# ie, show dot1x interfaces
#
# These cannot be used directly for passing to client processes.
# Store the interface in the form R/S/I/P  only.
#
# They will be compared with interfaces on this node obtained from IM
# echo $dot1x_interfaces, may give something like..
# 0/1/0/0
# 0/1/0/3
# 0/1/0/4
# 0/2/0/0
dot1x_interfaces=$(show_dot1x "-i" "all" |\
	grep -E  "^Dot1x info for (Ten|Twenty|Forty|Hundred|Gigabit|Four|Fifty|Two|TwentyFive).*([0-9]+(/[0-9]+)+)" |\
	cut -d ' ' -f4 | sed "s/^[a-zA-Z]*//")

# Iterate dot1x list of interfaces and compare with IM interface 
for dot1x_if in ${dot1x_interfaces[@]}; do
    dot1x_if_str=$(echo $intf_list_arr| grep -o "\S*$dot1x_if#" | sed 's/#//')

	if [[ $dot1x_if_str ]]; then
		dot1x_if_str_internal=`convert_interface_fmt '-i' $dot1x_if_str`
        if_stats_exec[i]="show dot1x statistics interface $dot1x_if_str"
        if_stats__ksh[i]="show_dot1x -t intf -f  $dot1x_if_str_internal"
	    i=$(($i+1))
    fi
done

mab_interfaces=$(show_mab -s -n -d -o -l $fq_nodeid |\
    grep -E  "^MAB info for (Ten|Twenty|Forty|Hundred|Gigabit|Four|Fifty|Two|TwentyFive).*([0-9]+(/[0-9]+)+)" |\
    cut -d ' ' -f4 | sed "s/^[a-zA-Z]*//")

# Iterate dot1x list of interfaces and compare with IM interface
for mab_if in ${mab_interfaces[@]}; do
    mab_if_str=$(echo $intf_list_arr| grep -o "\S*$mab_if#" | sed 's/#//')
    echo "Value of i: $i"
    if [[ $mab_if_str ]]; then
        mab_if_str_internal=`convert_interface_fmt '-i' $mab_if_str`
        if_stats_exec[i]="show mab statistics interface $mab_if_str"
        if_stats__ksh[i]="show_mab -s -f -e -i  $mab_if_str_internal"
        i=$(($i+1))
    fi
done

if_stats_exec[i]=''
if_stats__ksh[i]=''

pd_exec[1]=''
pd__ksh[1]=''
if [ -f /pkg/bin/ofa_dot1x_caps_show_ltrace ]; then
    pd_exec[1]='show dot1x platform trace all location $location'
    pd__ksh[1]='ofa_dot1x_caps_show_ltrace -i $fq_nodeid'
elif [ -f /pkg/bin/dot1x_caps_show_ltrace ]; then
    pd_exec[1]='show dot1x platform trace all location $location'
    pd__ksh[1]='dot1x_caps_show_ltrace -i $fq_nodeid'
fi

pd_exec[2]=''
pd__ksh[2]=''

# Parse the arguments to the script.
# Usage:
#
#sys.exit()
# A function called display() must be provided that calls the functions to
# Print the output heading
do_show_command() {
     print_main_heading "show tech-support dot1x"

     if [ "$__cardtype" == "SYS" ]; then
         exec_commands sys
     else
         case "$__cardtype" in
         "RP"|"DRP")
             exec_commands rp
             exec_commands if_stats
             exec_commands pd
             ;;
         "LC")
             if [ -f /pkg/bin/dot1x ]
             then
                 exec_commands lc
                 exec_commands if_stats
                 exec_commands pd
             fi
             ;;
         esac
     fi

     # Print the closing heading.
     print_main_heading "show tech-support dot1x complete"
}
# The display function.
display() {
        do_show_command
}

  # 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
