#!/pkg/bin/ksh
# --------------------------------------------------------------------------
# dhcpv6d_server_show_tech - Show tech-support script for dhcp ipv6 server
#
# March 2011, Karikolraj Balasubramanian
#
# Copyright (c) 2011-2020 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"
profile_name="unspecified"
vrf_name="unspecified"
interface_id=""
remote_id=""
client_duid=""
location=""
bng=0

# Parse the arguments to the script.
# Usage: 
# 
# -P  : Run command set 2 for profile
# -V  : Run command set 3 for vrf
# Always run         : Run command set 1
while [ $# -gt 0 ]; do
  case "$1" in
     -a) shift 1;;
     -P) profile_name="$2"; shift 2;;
     -V) vrf_name="$2"; shift 2;;
     -N) location="$2"; shift 2;;
     -I) interface_id="$2"; shift 2;;
     -C) client_duid="$2"; shift 2;;
     -B) bng=1; shift 2;;
     -R) remote_id="$2"; shift 2;;
     -t) __cardtype="$2"; 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 cmd
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 [[ "$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 server statistics location $location'
command1__ksh[1]='dhcpv6d_show_server -a -L $fq_nodeid'
command1_exec[2]='show dhcp ipv6 server statistics detail location $location'
command1__ksh[2]='dhcpv6d_show_server -a -E -L $fq_nodeid'
command1_exec[3]='show dhcp ipv6 server binding summary location $location'
command1__ksh[3]='dhcpv6d_show_server -e -S -L $fq_nodeid'
command1_exec[4]='show dhcp ipv6 server binding location $location'
command1__ksh[4]='dhcpv6d_show_server -e -L $fq_nodeid'
command1_exec[5]='show dhcp ipv6 server binding detail location $location'
command1__ksh[5]='dhcpv6d_show_server -e -T -L $fq_nodeid'
command1_exec[6]='show dhcp ipv6 server statistics debug all location $location'
command1__ksh[6]='dhcpv6d_show_server -a -L $fq_nodeid -D -m'
command1_exec[7]='show dhcp ipv6 server disconnect-history detail location $location'
command1__ksh[7]='dhcpv6d_show_server -y -T -L $fq_nodeid'
command1_exec[8]='show dhcp ipv6 trace errors location $location'
command1__ksh[8]='dhcpv6d_show_ltrace -i $fq_nodeid -E'
command1_exec[9]='show dhcp ipv6 trace events location $location'
command1__ksh[9]='dhcpv6d_show_ltrace -i $fq_nodeid -V'
command1_exec[10]='show dhcp ipv6 trace packets location $location'
command1__ksh[10]='dhcpv6d_show_ltrace -i $fq_nodeid -P'
command1_exec[11]='show dhcp ipv6 trace location $location'
command1__ksh[11]='dhcpv6d_show_ltrace -i $fq_nodeid -A'
command1_exec[12]='show ppp disconnect-history unique location $location'
command1__ksh[12]='show_ppp_disc_history -n $fq_nodeid -u'
command1_exec[13]='show dhcp ipv6 database location $location'
command1__ksh[13]='dhcpv6d_show_database -L $fq_nodeid'
command1_exec[14]='show process dhcpv6d location $location'
command1__ksh[14]='sysmgr_show -o -p dhcpv6d -n $fq_nodeid'
command1_exec[15]=''
command1__ksh[15]=''

#commands 2
command2_exec[1]='show dhcp ipv6 server statistics'
command2__ksh[1]='dhcpv6d_show_server -a'
command2_exec[2]='show dhcp ipv6 server statistics detail'
command2__ksh[2]='dhcpv6d_show_server -a -E'
command2_exec[3]='show dhcp ipv6 server binding summary'
command2__ksh[3]='dhcpv6d_show_server -e -S'
command2_exec[4]='show dhcp ipv6 server binding'
command2__ksh[4]='dhcpv6d_show_server -e'
command2_exec[5]='show dhcp ipv6 server statistics debug all'
command2__ksh[5]='dhcpv6d_show_server -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]='show dhcp ipv6 server cdm'
command2__ksh[14]='dhcpv6d_show_server -s'
command2_exec[15]='show dhcp ipv6 server cdm detail'
command2__ksh[15]='dhcpv6d_show_server -s -f'
command2_exec[16]=''
command2__ksh[16]=''

# -P profile
command4_exec[1]='show dhcp ipv6 server profile name $profile_name'
command4__ksh[1]='dhcpv6d_show_server -d $profile_name'
command4_exec[2]=''
command4__ksh[2]=''

command5_exec[1]='show dhcp ipv6 server profile name $profile_name location $location'
command5__ksh[1]='dhcpv6d_show_server -d $profile_name -L $fq_nodeid'
command5_exec[2]=''
command5__ksh[2]=''
# -V vrf_name
command6_exec[1]='show dhcp ipv6 server binding vrf $vrf_name'
command6__ksh[1]='dhcpv6d_show_server -e -V $vrf_name'
command6_exec[2]='show dhcp ipv6 server statistics vrf $vrf_name'
command6__ksh[2]='dhcpv6d_show_server -b $vrf_name'
command6_exec[3]='show ipsubscriber interface vrf $vrf_name brief'
command6__ksh[3]='show_ipsub_ma_intf -b -v $vrf_name'
command6_exec[4]='show running-config pool vrf $vrf_name ipv6'
command6__ksh[4]='nvgen -c -q /cfg/gl/daps/vrf/$vrf_name/ipv6/pool/'
command6_exec[5]=''
command6__ksh[5]=''

command7_exec[1]='show dhcp ipv6 server binding vrf $vrf_name location $location'
command7__ksh[1]=''
command7_exec[2]='show dhcp ipv6 server statistics vrf $vrf_name location $location'
command7__ksh[2]='dhcpv6d_show_server -b $vrf_name -L $fq_nodeid'
command7_exec[3]='show dhcp ipv6 server statistics vrf default location $location'
command7__ksh[3]='dhcpv6d_show_server -b default -L $fq_nodeid'
command7_exec[4]=''
command7__ksh[4]=''

# -I interface_id
command8_exec[1]='show dhcp ipv6 server binding interface-id $interface_id'
command8__ksh[1]='dhcpv6d_show_server -e -F $interface_id'
command8_exec[2]=''
command8__ksh[2]=''

command9_exec[1]='show dhcp ipv6 server binding interface-id $interface_id location $location'
command9__ksh[1]='dhcpv6d_show_server -e -F $interface_id -L $fq_nodeid'
command9_exec[2]=''
command9__ksh[2]=''

# -R remote_id 
command10_exec[1]='show dhcp ipv6 server binding remote-id $remote_id'
command10__ksh[1]='dhcpv6d_show_server -e -R $remote_id'
command10_exec[2]=''
command10__ksh[2]=''

command11_exec[1]='show dhcp ipv6 server binding remote-id $remote_id location $location'
command11__ksh[1]='dhcpv6d_show_server -e -R $remote_id -L $fq_nodeid'
command11_exec[2]=''
command11__ksh[2]=''


# -C duid
command12_exec[1]='show dhcp ipv6 server binding duid $clien_duid'
command12__ksh[1]='dhcpv6d_show_server -e -C $clien_duid'
command12_exec[2]=''
command12__ksh[2]=''

command13_exec[1]='show dhcp ipv6 server binding duid $client_duid location $location'
command13__ksh[1]='dhcpv6d_show_server -e -C $client_duid -L $fq_nodeid'
command13_exec[2]=''
command13__ksh[2]=''


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

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

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

# Commands captured when argument 'bng' is passed
command15_exec[1]='show subscriber session all summary'
command15__ksh[1]='show_iedge_subscriber -c session -w -a'
command15_exec[2]='show udp statistics summary'
command15__ksh[2]='udp_cmd_client show statistics -s'
command15_exec[3]='show ipv6 bundle interfaces'
command15__ksh[3]=''
command15_exec[4]='show subscriber manager disconnect-history unique' 
command15__ksh[4]='show_iedge_stats -l'
command15_exec[5]='show subscriber manager disconnect-history last summary' 
command15__ksh[5]='show_iedge_stats -r'
command15_exec[6]='show subscriber manager disconnect-history last'
command15__ksh[6]='show_iedge_stats -q'
command15_exec[7]='show subscriber manager session-history summary'
command15__ksh[7]='show_iedge_stats -j'
command15_exec[8]='show ppp summary'
command15__ksh[8]='show_ppp_summ -z'
command15_exec[9]='show controllers np counters all'
command15__ksh[9]='prm_np_show counters'
command15_exec[10]='show ipv6 neighbors summary'
command15__ksh[10]='show_ipv6_neighbors -s 0x2'
command15_exec[11]='show route ipv6 summary'
command15__ksh[11]='show_ipv6_rib -X 0x2 -Y 0x1 -Z ________ -V ________ -S ipv6 ________'
command15_exec[12]='show route ipv6 subscriber'
command15__ksh[12]='show_ipv6_rib -X 0x2 -Y 0x1 -Z ________ -V ________ -P subscriber ipv6 ________'
command15_exec[13]='show cef ipv6 summary'
command15__ksh[13]='fib_show_command -S -O 0x1'
command15_exec[14]='show cef ipv6 drops'
command15__ksh[14]='fib_statistics_show_command -P 0x1 -d'
command15_exec[15]='show adjacency ipv6 summary'
command15__ksh[15]='aib_show_command -S -P ipv6'
command15_exec[16]='show checkpoint dynamic process dhcpv6d'
command15__ksh[16]='chkpt_cli -p dhcpv6d'
command15_exec[17]='show pool ipv6'
command15__ksh[17]='daps_show -v all -s'
command15_exec[18]='show pool trace client'
command15__ksh[18]='daps_client_show_ltrace -A'
command15_exec[19]='show ipv6 trace nd aib'
command15__ksh[19]=''
command15_exec[20]='show ipv6 trace nd error'
command15__ksh[20]=''
command15_exec[21]=''
command15__ksh[21]=''

# 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 server"

     if [ "$__cardtype" = "SYS" ]; then
          exec_commands command14
          exec_commands command2

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

          if [ "$profile_name" != "unspecified" ]; then
              exec_commands command4
          fi

          if [ "$interface_id" != "" ]; then
               exec_commands command8
          fi

          if [ "$remote_id" != "" ]; then
               exec_commands command10
          fi

          if [ "$client_duid" != "" ]; then
              exec_commands command12
          fi

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

          exec_commands command1

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

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

          if [ "$interface_id" != "" ]; then
               exec_commands command9
          fi

          if [ "$remote_id" != "" ]; then
               exec_commands command11
          fi

          if [ "$client_duid" != "" ]; then
              exec_commands command13
          fi

          ;;
          esac
     fi

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

