#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_aib - Show tech-support script for AIB
#
# July 2017, Jiajian Yang
#
# Copyright (c) 2017, 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. 
                                                       
__cardtype="unspecified"

# Parse the arguments to the script.
# Usage: 
# 
# Run commands that display ip-infra information
while [ $# -gt 0 ]; do
  case "$1" in
     -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 version"
if [[ "$platform" = "panini" ]]; then
    basic__ksh[4]='ng_show_version'
else
    basic__ksh[4]='show_version'
fi

basic_exec[5]='show install active'
if [[ "$platform" = "panini" ]]; then
    basic__ksh[5]='sdr_instcmd show install active'
else
    basic__ksh[5]='instcmd show install active'
fi

basic_exec[6]='show logging'
basic__ksh[6]='show_logging'

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.
aib_pid=`get_pid aib`

#Attach procs
aib_attach_procs="$aib_pid"

attach_index=1
for attach_pid in $aib_attach_procs; do
    aib_attach_exec[$attach_index]="follow process $attach_pid iteration 5 verbose"
    aib_attach__ksh[$attach_index]="attach_process -p $attach_pid -i 5"
    (( attach_index =  $attach_index + 1))
done


#Command - RP only
rp_exec[1]="show adjacency summary"
rp__ksh[1]="aib_show_command -S"

rp_exec[2]=""
rp__ksh[2]=""

# Command  - Generic to RP/LC
rplc_exec[1]="show processes aib location $location"
rplc__ksh[1]="sysmgr_show -o -p aib"

rplc_exec[2]="show adjacency internal detail location $location"
rplc__ksh[2]="aib_show_command -E -D -N $fq_nodeid"

rplc_exec[3]="show adjacency remote internal detail location $location"
rplc__ksh[3]="aib_show_command -E -D -N $fq_nodeid"

rplc_exec[4]="show adjacency producer detail location $location"
rplc__ksh[4]="aib_show_command -M -D -N $fq_nodeid"

rplc_exec[5]="show adjacency consumer detail location $location"
rplc__ksh[5]="aib_show_command -C -D -N $fq_nodeid"

rplc_exec[6]="show adjacency internal idb detail location $location"
rplc__ksh[6]="aib_show_command -B -D -N $fq_nodeid"

rplc_exec[7]="show adjacency internal hash detail location $location"
rplc__ksh[7]="aib_show_command -T -D -N $fq_nodeid"

rplc_exec[8]="show adjacency quarantine-list detail location $location"
rplc__ksh[8]="aib_show_command -D -N $fq_nodeid -K"

rplc_exec[9]="show adjacency quarantine-list remote detail location $location"
rplc__ksh[9]="aib_show_command -R -D -N $fq_nodeid -K"

rplc_exec[10]="show adjacency quarantine-list client-info detail location $location"
rplc__ksh[10]="aib_show_command -D -N $fq_nodeid -K"

rplc_exec[11]="show adjacency quarantine-list remote client-info detail location $location"
rplc__ksh[11]="aib_show_command -R -D -N $fq_nodeid -K"

rplc_exec[12]="show adjacency producer-list detail location $location"
rplc__ksh[12]="aib_show_command -L -D -N $fq_nodeid"

rplc_exec[13]="show adjacency internal idb location $location"
rplc__ksh[13]="aib_show_command -B -N $fq_nodeid"

rplc_exec[14]="show adjacency trace all location $location"
rplc__ksh[14]="show_ltrace_aib -i $fq_nodeid -S -F"

rplc_exec[15]=""
rplc__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 aib"

    if [ "$__cardtype" == "SYS" ]; then
        exec_commands basic
    else 
        case "$__cardtype" in
        "RP")
            exec_commands rp
        esac
        exec_commands block
        exec_commands aib_attach
        exec_commands rplc
        exec_commands local
    fi
 
    # Print the closing heading. 
    print_main_heading "show tech-support aib 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

