#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_ip_infra - Show tech-support script for Ip-infra
#
# August 2012, Raghu Mohanraj
#
# Copyright (c) 2012-2019 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

# Initialise any variables used. 
                                                       
ipv4=0
ipv6=0
__cardtype="unspecified"

# Parse the arguments to the script.
# Usage: 
# 
# Run commands that display ip-infra information
while [ $# -gt 0 ]; do
  case "$1" in
     -B) ipv4=1; shift 1;;
     -C) ipv6=1; shift 1;;
     -t) __cardtype="$2"; shift 2;;
     *)  default_parser_function "$@"; shift $#;;
  esac
done

int_node_name=$showtech_node_name
ext_node_name=`node_conversion -E $int_node_name`
fq_nodeid=`node_conversion -i $int_node_name`
location=$ext_node_name

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

OS_STRING=`uname -s`

# 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  - show global
basic_exec[1]="show running-config"
basic__ksh[1]="nvgen -c -l 1 -t 1 -o 1"

basic_exec[2]="show logging"
basic__ksh[2]="show_logging"

# Command - block
# assumes "platform" is set for us by show_tech_main_fragment
if [ "$platform" = "panini" ]
then
    basic_exec[3]='show platform'
    basic__ksh[3]='show_platform_sysdb'
else
    basic_exec[3]='show platform'
    basic__ksh[3]='show_platform'
fi

basic_exec[4]="show redundancy"
basic__ksh[4]="redcon_show"

basic_exec[5]="show version"
if [ "$platform" = "panini" ]
then
    basic__ksh[5]='ng_show_version'
else
    basic__ksh[5]='show_version'
fi

basic_exec[6]='show cli history detail' 
basic__ksh[6]="show_parser_history -h 0x2" 

basic_exec[7]=""
basic__ksh[7]=""

# Command - block
# assumes "platform" is set for us by show_tech_main_fragment
if [ "$platform" = "panini" ]
then
    block_exec[1]='show process blocked'
    block__ksh[1]='sh_proc_ng_blocked'
else
    block_exec[1]='show process blocked'
    block__ksh[1]='show_processes -b'
fi

block_exec[2]=""
block__ksh[2]=""

#Command - local
local_exec[1]="show im database detail view owner location $location"
local__ksh[1]="im_show database -v 0x0 -l 0x3 -h $fq_nodeid"
local_exec[2]=""
local__ksh[2]=""

# get_pid function. Given the process name, it will
# echo the pid for that process
# it will have code to take care of the host OS.
OS_STRING=`uname -s`
function get_pid {
    if [[ ${OS_STRING} = "QNX" ]]; then
        pid=`pidin | grep $1 | cut -c1-8 | head -n 1`
    else
        pid=`ps -ef | grep $1 | grep -v grep | awk '{print $2}' | head -n 1`
    fi
    echo ${pid}
}

#Find the PIDs of ipinfra processes.
ipv4_ma_pid=`get_pid ipv4_ma`
ipv4_io_pid=`get_pid ipv4_io`
ipv6_ma_pid=`get_pid ipv6_ma`
ipv6_io_pid=`get_pid ipv6_io`
ipv6_ea_pid=`get_pid ipv6_ea`
ipv4_arm_pid=`get_pid ipv4_arm`
ipv6_arm_pid=`get_pid ipv6_arm`

#Attach procs
rpv4_attach_procs="$ipv4_arm_pid"
rpv6_attach_procs="$ipv6_arm_pid"
v4_attach_procs="$ipv4_ma_pid $ipv4_io_pid"
v6_attach_procs="$ipv6_ma_pid $ipv6_io_pid $ipv6_ea_pid"

attach_index=1
for attach_pid in $rpv4_attach_procs; do
    rpv4_attach_exec[$attach_index]="follow process $attach_pid iteration 1 verbose"
    rpv4_attach__ksh[$attach_index]="attach_process -p $attach_pid -i 1"
    (( attach_index =  $attach_index + 1))
done

attach_index=1
for attach_pid in $rpv6_attach_procs; do
    rpv6_attach_exec[$attach_index]="follow process $attach_pid iteration 1 verbose"
    rpv6_attach__ksh[$attach_index]="attach_process -p $attach_pid -i 1"
    (( attach_index =  $attach_index + 1))
done

attach_index=1
for attach_pid in $v4_attach_procs; do
    v4_attach_exec[$attach_index]="follow process $attach_pid iteration 1 verbose"
    v4_attach__ksh[$attach_index]="attach_process -p $attach_pid -i 1"
    (( attach_index =  $attach_index + 1))
done

attach_index=1
for attach_pid in $v6_attach_procs; do
    v6_attach_exec[$attach_index]="follow process $attach_pid iteration 1 verbose"
    v6_attach__ksh[$attach_index]="attach_process -p $attach_pid -i 1"
    (( attach_index =  $attach_index + 1))
done

#Command - RPv4 only
rpv4_exec[1]="show processes ipv4_arm"
rpv4__ksh[1]="sysmgr_show -o -p ipv4_arm"

rpv4_exec[2]="show arm ipv4 vrf all location all"
rpv4__ksh[2]="ip_arm_show -v 4 -f all -l all -j"

rpv4_exec[3]="show arm ipv4 conflicts"
rpv4__ksh[3]="ip_arm_show -v 4 -f default -l -1 -c"

rpv4_exec[4]="show arm ipv4 summary location all"
rpv4__ksh[4]="ip_arm_show -v 4 -f default -l all -s"

rpv4_exec[5]="show arm ipv4 vrf all database location all"
rpv4__ksh[5]="ip_arm_show -v 4 -f all -l all -d"

rpv4_exec[6]="show ipv4 vrf all interface internal"
rpv4__ksh[6]="show_ip_interface -t -v all"

rpv4_exec[7]="show ipv4 vrf all interface events"
rpv4__ksh[7]="show_ip_interface -u -v all"

rpv4_exec[8]="show smiap trace"
rpv4__ksh[8]="smiap_show_ltrace"

rpv4_exec[9]=""
rpv4__ksh[9]=""

#Command - RPv6 only
rpv6_exec[1]="show processes ipv6_arm"
rpv6__ksh[1]="sysmgr_show -o -p ipv6_arm"

rpv6_exec[2]="show arm ipv6 vrf all database location all"
rpv6__ksh[2]="ip_arm_show -v 6 -f all -l all -d"

rpv6_exec[3]="show arm ipv6 summary location all"
rpv6__ksh[3]="ip_arm_show -v 6 -f default -l all -s"

rpv6_exec[4]="show arm ipv6 conflicts"
rpv6__ksh[4]="ip_arm_show -v 6 -f default -l -1 -c"

rpv6_exec[5]="show arm ipv6 vrf all location all"
rpv6__ksh[5]="ip_arm_show -v 6 -f all -l all-j"

rpv6_exec[6]="show ipv6 vrf all interface internal"
rpv6__ksh[6]="show_ipv6_interface -t -v all"

rpv6_exec[7]="show ipv6 vrf all interface events"
rpv6__ksh[7]="show_ipv6_interface -u -v all"

rpv6_exec[8]="show ipv6 router global-address"
rpv6__ksh[8]="show_ipv6_global_addr -a"

rpv6_exec[9]="show ipv6 router global-address clients"
rpv6__ksh[9]="show_ipv6_global_addr -c"

rpv6_exec[10]=""
rpv6__ksh[10]=""

# Command  - show ipv4 
ipv4_exec[1]="show ipv4 ma trace location $location"
ipv4__ksh[1]="show_ipv4_ma_ltrace -i $fq_nodeid"

ipv4_exec[2]="show ipv4 ma trace error location $location"
ipv4__ksh[2]="show_ipv4_ma_ltrace -i $fq_nodeid -E"

ipv4_exec[3]="show ipv4 trace location $location"
ipv4__ksh[3]="show_ipv4_ltrace -i $fq_nodeid"

ipv4_exec[4]="show ipv4 trace error location $location"
ipv4__ksh[4]="show_ipv4_ltrace -i $fq_nodeid -E"

cmd="ls /pkg/sbin/ip_arm_show_ltrace 2>/dev/null"
plat=$?
if [[ $plat -eq 0 ]]
then
    ipv4_exec[5]="show arm ipv4 trace location $location"
    ipv4__ksh[5]="ip_arm_show_ltrace -V 4 -i $fq_nodeid"
fi

ipv4_exec[6]="show processes ipv4_ma"
ipv4__ksh[6]="sysmgr_show -o -p ipv4_ma"

ipv4_exec[7]="show processes ipv4_io"
ipv4__ksh[7]="sysmgr_show -o -p ipv4_io"

ipv4_exec[8]="show system statistics component ipv4-ma"
ipv4__ksh[8]="ship_show -c ipv4-ma"

ipv4_exec[9]="show system statistics component ipv4-io"
ipv4__ksh[9]="ship_show -c ipv4-io"

cmd="ls /pkg/bin/show_ip_traffic 2>/dev/null"
plat=$?
if [[ $plat -eq 0 ]]
then
   ipv4_exec[10]="show ipv4 traffic brief location $location"
   ipv4__ksh[10]="show_ip_traffic -b -l $fq_nodeid"
fi

ipv4_exec[11]="show ipv4 ma event-history location $location"
ipv4__ksh[11]="show_ip_ma_evts"

ipv4_exec[12]="show ipv4 ma owner-channel client location $location"
ipv4__ksh[12]="show_ipv4_oc_info -c -n $fq_nodeid"

ipv4_exec[13]="show ipv4 ma owner-channel client all interface all location $location" 
ipv4__ksh[13]="show_ipv4_oc_info -s all -n $fq_nodeid -i all"

ipv4_exec[14]="show ipv4 ea interface all location $location"
ipv4__ksh[14]="show_ipv4_ea_interface -l $fq_nodeid -a"

cmd="ls /pkg/sbin/rsi_show_ltrace 2>/dev/null"
ipv4_exec[15]="show rsi trace location $location"
ipv4__ksh[15]="rsi_show_ltrace -i $fq_nodeid"

ipv4_exec[16]=""
ipv4__ksh[16]=""

# Command  - show ipv6
ipv6_exec[1]="show ipv6 trace io location $location"
ipv6__ksh[1]="show_ltrace_io"

ipv6_exec[2]="show ipv6 trace ea location $location"
ipv6__ksh[2]="ipv6_ea_pi_show_ltrace -i $fq_nodeid"

ipv6_exec[3]="show ipv6 trace ma location $location"
ipv6__ksh[3]="show_ltrace_ma -i $fq_nodeid"

ipv6_exec[4]="show system statistics component ipv6-ma"
ipv6__ksh[4]="ship_show -c ipv6-ma"

ipv6_exec[5]="show system statistics component ipv6-ea"
ipv6__ksh[5]="ship_show -c ipv6-ea"

cmd="ls /pkg/sbin/ip_arm_show_ltrace 2>/dev/null"
plat=$?
if [[ $plat -eq 0 ]]
then
    ipv6_exec[6]="show arm ipv6 trace location $location"
    ipv6__ksh[6]="ip_arm_show_ltrace -V 6 -i $fq_nodeid"
fi

ipv6_exec[7]="show processes ipv6_ma"
ipv6__ksh[7]="sysmgr_show -o -p ipv6_ma"

ipv6_exec[8]="show processes ipv6_io"
ipv6__ksh[8]="sysmgr_show -o -p ipv6_io"

ipv6_exec[9]="show processes ipv6_ea"
ipv6__ksh[9]="sysmgr_show -o -p ipv6_ea"

ipv6_exec[10]="show ipv6 traffic brief location $location"
ipv6__ksh[10]="show_ipv6_traffic -b -l $fq_nodeid"

ipv6_exec[11]="show ipv6 ma event-history location $location"
ipv6__ksh[11]="show_ipv6_ma_evts"

ipv6_exec[12]="show ipv6 ma owner-channel client location $location" 
ipv6__ksh[12]="show_ipv6_oc_info -c -n $fq_nodeid"

ipv6_exec[13]="show ipv6 ma owner-channel client all interface all location $location" 
ipv6__ksh[13]="show_ipv6_oc_info -s all -c -n $fq_nodeid -i all"

ipv6_exec[14]="show ipv6 ea interface all location $location"
ipv6__ksh[14]="show_ipv6_ea_interface -l $fq_nodeid -a"

ipv6_exec[15]=""
ipv6__ksh[15]=""

# 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 ip-infra"

    if [ "$__cardtype" == "SYS" ]; then
        exec_commands basic
    else 
        case "$__cardtype" in
        "RP")
            if [ $ipv4 -eq  1 ]; then
                exec_commands rpv4
                exec_commands rpv4_attach
            fi
            if [ $ipv6 -eq  1 ]; then
                exec_commands rpv6
                exec_commands rpv6_attach
            fi
            if [ $ipv4 -eq  0 ] && [ $ipv6 -eq  0 ]; then
                exec_commands rpv4
                exec_commands rpv6
                exec_commands rpv4_attach
                exec_commands rpv6_attach
            fi
            ;;
        esac

        if [ $ipv4 -eq  1 ]; then
            exec_commands ipv4
            exec_commands block
            exec_commands v4_attach
        fi
        if [ $ipv6 -eq  1 ]; then
            exec_commands ipv6
            exec_commands block
            exec_commands v6_attach
        fi
        if [ $ipv4 -eq  0 ] && [ $ipv6 -eq  0 ]; then
            exec_commands ipv4
            exec_commands ipv6
            exec_commands block
            exec_commands v4_attach
            exec_commands v6_attach
        fi
        exec_commands local
    fi
 
    # Print the closing heading. 
    print_main_heading "show tech-support ip-infra 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

