#!/pkg/bin/ksh
# --------------------------------------------------------------------------
# dhcpd_ipv4_relay_show_tech - Show tech-support script for dhcp ipv4 relay
#
# March 2008, Michael G. McMenemy
#
# Oct 2011, Chandra Tangi
#
# Copyright (c) 2008-2011, 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. 
                                                       
profile_name="unspecified"
vrf_name="unspecified"
__cardtype="unspecified"

# 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;;
     -l) 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

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

# -l location
show_dhcpv4_relay_rplc_exec[1]='show dhcp ipv4 relay statistics location $location'
show_dhcpv4_relay_rplc__ksh[1]='dhcpd_show_relay -a -L $fq_nodeid'

show_dhcpv4_relay_rplc_exec[2]='show dhcp ipv4 relay statistics detail location $location'
show_dhcpv4_relay_rplc__ksh[2]='dhcpd_show_relay -a -O -L $fq_nodeid'

show_dhcpv4_relay_rplc_exec[3]='show dhcp ipv4 relay statistics raw all location $location'
show_dhcpv4_relay_rplc__ksh[3]='dhcpd_show_relay -a -L $fq_nodeid -D -m'

show_dhcpv4_relay_rplc_exec[4]='show dhcp ipv4 trace errors location $location'
show_dhcpv4_relay_rplc__ksh[4]='dhcpd_show_ltrace -i $fq_nodeid -E'

show_dhcpv4_relay_rplc_exec[5]='show dhcp ipv4 trace events location $location'
show_dhcpv4_relay_rplc__ksh[5]='dhcpd_show_ltrace -i $fq_nodeid -V'

show_dhcpv4_relay_rplc_exec[6]='show dhcp ipv4 trace packets location $location'
show_dhcpv4_relay_rplc__ksh[6]='dhcpd_show_ltrace -i $fq_nodeid -P'

show_dhcpv4_relay_rplc_exec[7]='show dhcp ipv4 trace location $location'
show_dhcpv4_relay_rplc__ksh[7]='dhcpd_show_ltrace -i $fq_nodeid -A'

show_dhcpv4_relay_rplc_exec[8]=''
show_dhcpv4_relay_rplc__ksh[8]=''

# -P profile
show_dhcpv4_relay_profile_exec[1]='show dhcp ipv4 relay profile name $profile_name'
show_dhcpv4_relay_profile__ksh[1]='dhcpd_show_relay -d $profile_name'

show_dhcpv4_relay_profile_exec[2]=''
show_dhcpv4_relay_profile__ksh[2]=''

show_dhcpv4_relay_profile_rplc_exec[1]='show dhcp ipv4 relay profile name $profile_name location $location'
show_dhcpv4_relay_profile_rplc__ksh[1]='dhcpd_show_relay -d $profile_name -L $fq_nodeid'

show_dhcpv4_relay_profile_rplc_exec[2]=''
show_dhcpv4_relay_profile_rplc__ksh[2]='' 

# -V vrf
show_dhcpv4_relay_vrf_exec[1]='show dhcp ipv4 relay statistics vrf $vrf_name'
show_dhcpv4_relay_vrf__ksh[1]='dhcpd_show_relay -b $vrf_name'

show_dhcpv4_relay_vrf_exec[2]=''
show_dhcpv4_relay_vrf__ksh[2]=''

######################################################################
##                       Show dhcpv4 relay                          ##
######################################################################
show_dhcpv4_relay_exec[1]='show process dhcpd'
show_dhcpv4_relay__ksh[1]='sysmgr_show -o -p dhcpd'

show_dhcpv4_relay_exec[2]='show running-config'
show_dhcpv4_relay__ksh[2]='nvgen -c -q /'

show_dhcpv4_relay_exec[3]='show dhcp ipv4 relay statistics'
show_dhcpv4_relay__ksh[3]='dhcpd_show_relay -a'

show_dhcpv4_relay_exec[4]='show dhcp ipv4 relay statistics detail'
show_dhcpv4_relay__ksh[4]='dhcpd_show_relay -a -O'

show_dhcpv4_relay_exec[5]='show dhcp ipv4 relay statistics raw all'
show_dhcpv4_relay__ksh[5]='dhcpd_show_relay -a -D -m'

show_dhcpv4_relay_exec[6]='show dhcp ipv4 trace errors'
show_dhcpv4_relay__ksh[6]='dhcpd_show_ltrace -E -i all'
  
show_dhcpv4_relay_exec[7]='show dhcp ipv4 trace events'
show_dhcpv4_relay__ksh[7]='dhcpd_show_ltrace -V -i all'

show_dhcpv4_relay_exec[8]='show dhcp ipv4 trace packets'
show_dhcpv4_relay__ksh[8]='dhcpd_show_ltrace -P -i all'

show_dhcpv4_relay_exec[9]='show dhcp ipv4 trace'
show_dhcpv4_relay__ksh[9]='dhcpd_show_ltrace -A -i all'

show_dhcpv4_relay_exec[10]='show dhcp ipv4 issu'
show_dhcpv4_relay__ksh[10]='dhc4_issu_status_show'

show_dhcpv4_relay_exec[11]='show dhcp ipv4 issu status'
show_dhcpv4_relay__ksh[11]='dhc4_issu_status_show -i'

show_dhcpv4_relay_exec[12]='show cli history detail'
show_dhcpv4_relay__ksh[12]='show_parser_history -h 2'

show_dhcpv4_relay_exec[13]=''
show_dhcpv4_relay__ksh[13]=''

# 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:/dhcpv4_track_dump.log.$nodename"
if [ -e "$a" ]; then
   $cp "/disk0:/dhcpv4_track_dump.log.$nodename" $node_subdir  2>>$error_file
fi
b="/tmp/dhcpv4_track_dump"
if [ -e "$b" ]; then
   $cp /tmp/dhcpv4_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 ipv4 dhcpd relay"

     if [ "$__cardtype" = "SYS" ]; then
          exec_commands show_dhcpv4_relay
          
          if [ "$profile_name" != "unspecified" ]; then
              exec_commands show_dhcpv4_relay_profile 
          fi

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

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

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

          ;;
        esac
     fi

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

