#!/pkg/bin/ksh
# --------------------------------------------------------------------------
# dhcpd_ipv4_snoop_show_tech - Show tech-support script for dhcp ipv4 snoop
#
# March 2008, Michael G. McMenemy
#
# Sep 2011, Chandra Tangi
#
# Copyright (c) 2008-2011, 2014, 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

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

# Initialise any variables used. 
                                                       
profile_name="unspecified"
vrf_name="unspecified"
bridge_domain_name="unspecified"
__cardtype="unspecified"

# Parse the arguments to the script.
# Usage: 
# 
# -P  : Run command set 2 for profile
# -B  : Run command set 3 for bridge-domain
# Always run         : Run command set 1
while [ $# -gt 0 ]; do
  case "$1" in
     -a) shift 1;;
     -P) profile_name="$2"; shift 2;;
     -B) bridge_domain_name="$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=$?

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

# Show dhcpv4 snoop without location
dhcp_show_snoop_exec[1]='show dhcp ipv4 snoop statistics'
dhcp_show_snoop__ksh[1]='dhcpd_show_snoop -a'

dhcp_show_snoop_exec[2]='show dhcp ipv4 snoop statistics detail'
dhcp_show_snoop__ksh[2]='dhcpd_show_snoop -a -O'

dhcp_show_snoop_exec[3]='show dhcp ipv4 snoop binding summary'
dhcp_show_snoop__ksh[3]='dhcpd_show_snoop -g'

dhcp_show_snoop_exec[4]='show dhcp ipv4 snoop binding'
dhcp_show_snoop__ksh[4]='dhcpd_show_snoop -e'

dhcp_show_snoop_exec[5]='show dhcp ipv4 snoop binding detail'
dhcp_show_snoop__ksh[5]='dhcpd_show_snoop -i'

dhcp_show_snoop_exec[6]='show dhcp ipv4 snoop profile'
dhcp_show_snoop__ksh[6]='dhcpd_show_snoop -c'

dhcp_show_snoop_exec[7]='show dhcp ipv4 snoop statistics raw all'
dhcp_show_snoop__ksh[7]='dhcpd_show_snoop -a -h -m'

dhcp_show_snoop_exec[8]='show dhcp ipv4 trace errors'
dhcp_show_snoop__ksh[8]='dhcpd_show_ltrace -E -i all'

dhcp_show_snoop_exec[9]='show dhcp ipv4 trace events'
dhcp_show_snoop__ksh[9]='dhcpd_show_ltrace -V -i all'

dhcp_show_snoop_exec[10]='show dhcp ipv4 trace packets'
dhcp_show_snoop__ksh[10]='dhcpd_show_ltrace -P -i all'

dhcp_show_snoop_exec[11]='show dhcp ipv4 trace'
dhcp_show_snoop__ksh[11]='dhcpd_show_ltrace -A -i all'

dhcp_show_snoop_exec[12]='show cli history detail'
dhcp_show_snoop__ksh[12]='show_parser_history -h 2'

dhcp_show_snoop_exec[13]=''
dhcp_show_snoop__ksh[13]=''

# -P profile
dhcp_show_snoop_profile_exec[1]='show dhcp ipv4 snoop profile name $profile_name'
dhcp_show_snoop_profile__ksh[1]='dhcpd_show_snoop -d $profile_name'

dhcp_show_snoop_profile_exec[2]=''
dhcp_show_snoop_profile__ksh[2]=''

# -B bridge-domain
dhcp_show_snoop_bd_exec[1]='show dhcp ipv4 snoop statistics bridge-domain $bridge_domain_name'
dhcp_show_snoop_bd__ksh[1]='dhcpd_show_snoop -b $bridge_domain_name'

dhcp_show_snoop_bd_exec[2]=''
dhcp_show_snoop_bd__ksh[2]=''

#########################################################################
##    Basic snoop debug commands.                                      ##
#########################################################################

Common1_exec[1]='show process dhcpd'
Common1__ksh[1]='sysmgr_show -o -p dhcpd'

Common1_exec[2]='show running-config'
Common1__ksh[2]='nvgen -c -q /'

Common1_exec[3]='show interface brief'
Common1__ksh[3]='show_interface -a -o 0x2'
Common1_exec[4]='show l2vpn bridge-domain summary'
Common1__ksh[4]='l2vpn_show 0xa'

if [[ "$platform" = "panini" ]]; then

    Common1_exec[5]='show processes blocked location all'
    Common1__ksh[5]='sh_proc_ng_blocked all'
else
    Common1_exec[5]='show processes blocked location all'
    Common1__ksh[5]='show_processes -b -n all'
fi

Common1_exec[6]=''
Common1__ksh[6]=''

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

     if [ "$__cardtype" = "SYS" ]; then
          exec_commands Common1
     else
          case "$__cardtype" in
          "RP") 
               exec_commands dhcp_show_snoop 

               if [ "$bridge_domain_name" != "unspecified" ]; then
                    exec_commands dhcp_show_snoop_bd
               fi

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

               ;;
          esac
     fi 

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

