#!/pkg/bin/ksh
# --------------------------------------------------------------------------
# dhcpv6d_relay_show_tech - Show tech-support script for dhcp ipv6 relay
#
# Nov 2013, Siddharth Sharma
#
# 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. 
                                                       
__cardtype="unspecified"
vrf_name="unspecified"
location=""
rib=0

# Parse the arguments to the script.
# Usage: 
# 
# -V  : Run command set 3 for vrf
# Always run         : Run command set 1
while [ $# -gt 0 ]; do
  case "$1" in
     -V) vrf_name="$2"; shift 2;;
     -t) __cardtype="$2"; shift 2;;
     -l) location="$2"; shift 2;;
     -t) __cardtype="$2"; shift 2;;
     -B) rib=1; shift 2;;
     *)  default_parser_function "$@"; shift $#;;
  esac
done

__parm1=`sysmgr_show -o -p exec | grep PID`
__parm2=`echo ${__parm1#*PID:}`
export EXEC_PID=$__parm2

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

#Platform specific commands
# Detect on which platform are we goin to exec cm
uname -a | grep hfr > /dev/null 2>&1;  RC=$?
uname -a | grep prp > /dev/null 2>&1;  RC1=$?
uname -a | grep enxr > /dev/null 2>&1; RC2=$?
uname -a | grep asr9k > /dev/null 2>&1; RC3=$?
uname -a | grep xrvr > /dev/null 2>&1; RC4=$?

PLATFORM_TYPE=`uname -m`

if [[ "$RC" -eq "0" ]]; then
    platform="hfr"
elif [[ "$RC1" -eq "0" ]]; then
    platform="prp"
elif [[ "$RC2" -eq "0" ]]; then
    platform="enxr"
elif [[ "$RC3" -eq "0" ]]; then
    platform="viking"
elif [[ "$RC4" -eq "0" ]]; then
    platform="xrvr"
else
    platform="panini"
fi

# 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. 

# -N location
command1_exec[1]='show dhcp ipv6 relay statistics location $location'
command1__ksh[1]='dhcpv6d_show_relay -a -L $fq_nodeid'
command1_exec[2]='show dhcp ipv6 relay statistics detail location $location'
command1__ksh[2]='dhcpv6d_show_relay -a -E -L $fq_nodeid'
command1_exec[3]='show dhcp ipv6 relay binding summary location $location'
command1__ksh[3]='dhcpv6d_show_relay -e -S -L $fq_nodeid'
command1_exec[4]='show dhcp ipv6 relay binding location $location'
command1__ksh[4]='dhcpv6d_show_relay -e -L $fq_nodeid'
command1_exec[5]='show dhcp ipv6 relay binding detail location $location'
command1__ksh[5]='dhcpv6d_show_relay -e -T -L $fq_nodeid'
command1_exec[6]='show dhcp ipv6 relay statistics debug all location $location'
command1__ksh[6]='dhcpv6d_show_relay -a -L $fq_nodeid -D -m'
command1_exec[7]='show dhcp ipv6 trace errors location $location'
command1__ksh[7]='dhcpv6d_show_ltrace -i $fq_nodeid -E'
command1_exec[8]='show dhcp ipv6 trace events location $location'
command1__ksh[8]='dhcpv6d_show_ltrace -i $fq_nodeid -V'
command1_exec[9]='show dhcp ipv6 trace packets location $location'
command1__ksh[9]='dhcpv6d_show_ltrace -i $fq_nodeid -P'
command1_exec[10]='show dhcp ipv6 trace location $location'
command1__ksh[10]='dhcpv6d_show_ltrace -i $fq_nodeid -A'
command1_exec[11]='show dhcp ipv6 database location $location'
command1__ksh[11]='dhcpv6d_show_database -L $fq_nodeid'
command1_exec[12]='show process dhcpv6d location $location'
command1__ksh[12]='sysmgr_show -o -p dhcpv6d -n $fq_nodeid'
command1_exec[13]=''
command1__ksh[13]=''

#commands 2
command2_exec[1]='show dhcp ipv6 relay statistics'
command2__ksh[1]='dhcpv6d_show_relay -a'
command2_exec[2]='show dhcp ipv6 relay statistics detail'
command2__ksh[2]='dhcpv6d_show_relay -a -E'
command2_exec[3]='show dhcp ipv6 relay binding summary'
command2__ksh[3]='dhcpv6d_show_relay -e -S'
command2_exec[4]='show dhcp ipv6 relay binding'
command2__ksh[4]='dhcpv6d_show_relay -e'
command2_exec[5]='show dhcp ipv6 relay statistics debug all'
command2__ksh[5]='dhcpv6d_show_relay -a -D -m'
command2_exec[6]='show dhcp ipv6 trace errors'
command2__ksh[6]='dhcpv6d_show_ltrace -E -i all'
command2_exec[7]='show dhcp ipv6 trace events'
command2__ksh[7]='dhcpv6d_show_ltrace -V -i all'
command2_exec[8]='show dhcp ipv6 trace packets'
command2__ksh[8]='dhcpv6d_show_ltrace -P -i all'
command2_exec[9]='show dhcp ipv6 trace'
command2__ksh[9]='dhcpv6d_show_ltrace -A -i all'
command2_exec[10]='show dhcp ipv6 database'
command2__ksh[10]='dhcpv6d_show_database'
command2_exec[11]='show dhcp ipv6 issu'
command2__ksh[11]='dhcpv6d_show_issu_status'
command2_exec[12]='show dhcp ipv6 issu status' 
command2__ksh[12]='dhcpv6d_show_issu_status -L'
command2_exec[13]='show cli history detail'
command2__ksh[13]='show_parser_history -h 2'
command2_exec[14]=''
command2__ksh[14]=''

# -V vrf_name
command4_exec[1]='show dhcp ipv6 relay binding vrf $vrf_name'
command4__ksh[1]='dhcpv6d_show_relay -e -V $vrf_name'
command4_exec[2]='show dhcp ipv6 relay statistics vrf $vrf_name'
command4__ksh[2]='dhcpv6d_show_relay -b $vrf_name'
command4_exec[3]=''
command4__ksh[3]=''


command5_exec[1]='show dhcp ipv6 relay binding vrf $vrf_name location $location'
command5__ksh[1]=''
command5_exec[2]='show dhcp ipv6 relay statistics vrf $vrf_name location $location'
command5__ksh[2]='dhcpv6d_show_relay -b $vrf_name -L $fq_nodeid'
command5_exec[3]='show dhcp ipv6 relay statistics vrf default location $location'
command5__ksh[3]='dhcpv6d_show_relay -b default -L $fq_nodeid'
command5_exec[4]='' 
command5__ksh[4]=''

# Default commands
command6_exec[1]='show dhcp ipv6 base context'
command6__ksh[1]='dhcpv6d_show_base -C'

command6_exec[2]='show process blocked all'
if [[ "${PLATFORM_TYPE}" == "x86_64" ]]; then
    command6__ksh[2]='sh_proc_ng_blocked all'
else
	command6__ksh[2]='show_processes -b -n all'
fi


command6_exec[3]='show redundancy'
command6__ksh[3]='redcon_show'
command6_exec[4]='show running-config'
command6__ksh[4]='nvgen -c -q /'
command6_exec[5]='show logging'
command6__ksh[5]='show_logging'
command6_exec[6]=''
command6__ksh[6]=''

# Commands captured when argument 'rib' is passed
command7_exec[1]='show route ipv6 summary'
command7__ksh[1]='show_ipv6_rib -X 0x2 -Y 0x1 -Z ________ -V ________ -S ipv6 ________'
command7_exec[2]='show route ipv6 subscriber'
command7__ksh[2]='show_ipv6_rib -X 0x2 -Y 0x1 -Z ________ -V ________ -P subscriber ipv6 ________'
command7_exec[3]=''
command7__ksh[3]=''

# Pull all the log files like CPE-tracking etc
cp="cp -f"
rm="rm -f"
mkdir="mkdir"
touch="touch"
node_name=`uname -n`
node_subdir=$__tar_file_directory_on_node/$node_name
if [ ! -d "$node_subdir" ]; then
   $mkdir $node_subdir
fi
error_file="/tmp/dhcpd_sh_tech_errors.txt"
$rm $error_file
nodename_chk=`echo $node_name | grep '^node'`
if [ -z "$nodename_chk" ] ; then
    nodename=`echo $node_name | cut -d '_' -f2-`
else
    nodename="$node_name";
fi
a="/disk0:/dhcpv6_track_dump.log.$nodename"
if [ -e "$a" ]; then
   $cp "/disk0:/dhcpv6_track_dump.log.$nodename" $node_subdir  2>>$error_file
fi
b="/tmp/dhcpv6_track_dump"
if [ -e "$b" ]; then
   $cp /tmp/dhcpv6_track_dump $node_subdir 2>>$error_file
fi
c="/tmp/dhcpd_sh_tech_errors.txt"
if [ -e "$c" ] ; then
    $cp /tmp/dhcpd_sh_tech_errors.txt $node_subdir
fi

# 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 ipv6 dhcpd relay"

     if [ "$__cardtype" == "SYS" ]; then
         exec_commands command6
         exec_commands command2
         if [ "$vrf_name" != "unspecified" ]; then
              exec_commands command4
         fi

         if [ $rib -eq 1 ]; then
            exec_commands command7 
         fi
     else
         case "$__cardtype" in
         "DRP"|"RP"|"LC")

         exec_commands command1

         if [ "$vrf_name" != "unspecified" ]; then
              exec_commands command5
         fi
         ;;
         esac
     fi

     # Print the closing heading. 
     print_main_heading "show tech-support ipv6 dhcpd relay 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

