#!/pkg/bin/ksh
# --------------------------------------------------------------------------
# dhcpv6d_base_show_tech - Show tech-support script for dhcp ipv6 base
#
# April 2017, Akash Pathak
#
# 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"
vrf_name="unspecified"
location=""
bng=0

# Parse the arguments to the script.
# Usage: 
# 
# -V  : Run command set 2 for vrf
# Always run         : Run command set 1
while [ $# -gt 0 ]; do
  case "$1" in
     -a) shift 1;;
     -V) vrf_name="$2"; shift 2;;
     -N) location="$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 base statistics location $location'
command1__ksh[1]='dhcpv6d_show_base -a -L $fq_nodeid'
command1_exec[2]='show dhcp ipv6 base statistics detail location $location'
command1__ksh[2]='dhcpv6d_show_base -a -E -L $fq_nodeid'
command1_exec[3]='show dhcp ipv6 proxy statistics location $location'
command1__ksh[3]='dhcpv6d_show_proxy -a -L $fq_nodeid'
command1_exec[4]='show dhcp ipv6 proxy statistics detail location $location'
command1__ksh[4]='dhcpv6d_show_proxy -a -E -L $fq_nodeid'
command1_exec[5]='show dhcp ipv6 proxy binding summary location $location'
command1__ksh[5]='dhcpv6d_show_proxy -e -S -L $fq_nodeid'
command1_exec[6]='show dhcp ipv6 proxy binding location $location'
command1__ksh[6]='dhcpv6d_show_proxy -e -L $fq_nodeid'
command1_exec[7]='show dhcp ipv6 proxy binding detail location $location'
command1__ksh[7]='dhcpv6d_show_proxy -e -T -L $fq_nodeid'
command1_exec[8]='show dhcp ipv6 server statistics location $location'
command1__ksh[8]='dhcpv6d_show_server -a -L $fq_nodeid'
command1_exec[9]='show dhcp ipv6 server statistics detail location $location'
command1__ksh[9]='dhcpv6d_show_server -a -E -L $fq_nodeid'
command1_exec[10]='show dhcp ipv6 server binding summary location $location'
command1__ksh[10]='dhcpv6d_show_server -e -S -L $fq_nodeid'
command1_exec[11]='show dhcp ipv6 server binding location $location'
command1__ksh[11]='dhcpv6d_show_server -e -L $fq_nodeid'
command1_exec[12]='show dhcp ipv6 server binding detail location $location'
command1__ksh[12]='dhcpv6d_show_server -e -T -L $fq_nodeid'
command1_exec[13]='show dhcp ipv6 base statistics debug all location $location'
command1__ksh[13]='dhcpv6d_show_base -a -L $fq_nodeid -D -m'
command1_exec[14]='show dhcp ipv6 proxy statistics debug all location $location'
command1__ksh[14]='dhcpv6d_show_proxy -a -L $fq_nodeid -D -m'
command1_exec[15]='show dhcp ipv6 server statistics debug all location $location'
command1__ksh[15]='dhcpv6d_show_server -a -L $fq_nodeid -D -m'
command1_exec[16]='show dhcp ipv6 proxy disconnect-history detail location $location'
command1__ksh[16]='dhcpv6d_show_proxy -y -T -L $fq_nodeid'
command1_exec[17]='show dhcp ipv6 server disconnect-history detail location $location'
command1__ksh[17]='dhcpv6d_show_server -y -T -L $fq_nodeid'
command1_exec[18]='show dhcp ipv6 trace errors location $location'
command1__ksh[18]='dhcpv6d_show_ltrace -i $fq_nodeid -E'
command1_exec[19]='show dhcp ipv6 trace events location $location'
command1__ksh[19]='dhcpv6d_show_ltrace -i $fq_nodeid -V'
command1_exec[20]='show dhcp ipv6 trace packets location $location'
command1__ksh[20]='dhcpv6d_show_ltrace -i $fq_nodeid -P'
command1_exec[21]='show dhcp ipv6 trace location $location'
command1__ksh[21]='dhcpv6d_show_ltrace -i $fq_nodeid -A'
command1_exec[22]='show dhcp ipv6 database location $location'
command1__ksh[22]='dhcpv6d_show_database -L $fq_nodeid'
command1_exec[23]='show process dhcpv6d location $location'
command1__ksh[23]='sysmgr_show -o -p dhcpv6d -n $fq_nodeid'
command1_exec[24]=''
command1__ksh[24]=''

#commands 2
command2_exec[1]='show dhcp ipv6 base statistics'
command2__ksh[1]='dhcpv6d_show_base -a'
command2_exec[2]='show dhcp ipv6 base statistics detail'
command2__ksh[2]='dhcpv6d_show_base -a -E'
command2_exec[3]='show dhcp ipv6 proxy statistics'
command2__ksh[3]='dhcpv6d_show_proxy -a'
command2_exec[4]='show dhcp ipv6 proxy statistics detail'
command2__ksh[4]='dhcpv6d_show_proxy -a -E'
command2_exec[5]='show dhcp ipv6 proxy binding summary'
command2__ksh[5]='dhcpv6d_show_proxy -e -S'
command2_exec[6]='show dhcp ipv6 proxy binding'
command2__ksh[6]='dhcpv6d_show_proxy -e'
command2_exec[7]='show dhcp ipv6 server statistics'
command2__ksh[7]='dhcpv6d_show_server -a'
command2_exec[8]='show dhcp ipv6 server statistics detail'
command2__ksh[8]='dhcpv6d_show_server -a -E'
command2_exec[9]='show dhcp ipv6 server binding summary'
command2__ksh[9]='dhcpv6d_show_server -e -S'
command2_exec[10]='show dhcp ipv6 server binding'
command2__ksh[10]='dhcpv6d_show_server -e'
command2_exec[11]='show dhcp ipv6 base statistics debug all'
command2__ksh[11]='dhcpv6d_show_base -a -D -m'
command2_exec[12]='show dhcp ipv6 proxy statistics debug all'
command2__ksh[12]='dhcpv6d_show_proxy -a -D -m'
command2_exec[13]='show dhcp ipv6 server statistics debug all'
command2__ksh[13]='dhcpv6d_show_server -a -D -m'
command2_exec[14]='show dhcp ipv6 trace errors'
command2__ksh[14]='dhcpv6d_show_ltrace -E -i all'
command2_exec[15]='show dhcp ipv6 trace events'
command2__ksh[15]='dhcpv6d_show_ltrace -V -i all'
command2_exec[16]='show dhcp ipv6 trace packets'
command2__ksh[16]='dhcpv6d_show_ltrace -P -i all'
command2_exec[17]='show dhcp ipv6 trace'
command2__ksh[17]='dhcpv6d_show_ltrace -A -i all'
command2_exec[18]='show dhcp ipv6 issu'
command2__ksh[18]='dhcpv6d_show_issu_status'
command2_exec[19]='show dhcp ipv6 issu status' 
command2__ksh[19]='dhcpv6d_show_issu_status -L'
command2_exec[20]='show dhcp ipv6 database'
command2__ksh[20]='dhcpv6d_show_database'
command2_exec[21]='show cli history detail'
command2__ksh[21]='show_parser_history -h 2'
command2_exec[22]=''
command2__ksh[22]=''

# -V vrf_name
command3_exec[1]='show running-config pool vrf $vrf_name ipv6'
command3__ksh[1]='nvgen -c -q /cfg/gl/daps/vrf/$vrf_name/ipv6/pool/'

# Default commands
command4_exec[1]='show process blocked all'
if [[ "${PLATFORM_TYPE}" == "x86_64" ]]; then
      command4__ksh[1]='sh_proc_ng_blocked all'
else
      command4__ksh[1]='show_processes -b -n all'
fi

command4_exec[2]='show redundancy'
command4__ksh[2]='redcon_show'
command4_exec[3]='show running-config'
command4__ksh[3]='nvgen -c -q /'
command4_exec[4]='show logging'
command4__ksh[4]='show_logging'
command4_exec[5]=''
command4__ksh[5]=''

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

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

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

     else
          case "$__cardtype" in
          "DRP"|"RP"|"LC")

          exec_commands command1

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

          ;;
          esac
     fi

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

