#!/pkg/bin/ksh
# --------------------------------------------------------------------------
# dhcpd_ipv4_proxy_show_tech - Show tech-support script for dhcp ipv4 proxy
#
# March 2008, Suresh R Thesayi
#
# 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

# Initialise any variables used. 
                                                       
profile_name="unspecified"
vrf_name="unspecified"
__cardtype="unspecified"
circuit_id=""
remote_id=""
location=""

# 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;;
     -C) circuit_id="$2"; 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=$?

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.
#
# For all of these the __ksh variable is the process that will actually be
# spawned, the _exec variable is just a string that is printed in the output to
# describe it.
#

#######################################################################
###                   Show dhcpv4 proxy                             ###
#######################################################################
show_dhcpv4_proxy_exec[1]='show process dhcpd'
show_dhcpv4_proxy__ksh[1]='sysmgr_show -o -p dhcpd'

show_dhcpv4_proxy_exec[2]='show running-config'
show_dhcpv4_proxy__ksh[2]='nvgen -c -q /'

show_dhcpv4_proxy_exec[3]='show dhcp ipv4 proxy statistics'
show_dhcpv4_proxy__ksh[3]='dhcpd_show_proxy -a'

show_dhcpv4_proxy_exec[4]='show dhcp ipv4 proxy statistics detail'
show_dhcpv4_proxy__ksh[4]='dhcpd_show_proxy -a -O'

show_dhcpv4_proxy_exec[5]='show dhcp ipv4 proxy binding summary'
show_dhcpv4_proxy__ksh[5]='dhcpd_show_proxy -e -S'

show_dhcpv4_proxy_exec[6]='show dhcp ipv4 proxy binding'
show_dhcpv4_proxy__ksh[6]='dhcpd_show_proxy -e'

show_dhcpv4_proxy_exec[7]='show dhcp ipv4 proxy binding detail'
show_dhcpv4_proxy__ksh[7]='dhcpd_show_proxy -e -T'

show_dhcpv4_proxy_exec[8]='show dhcp ipv4 proxy statistics raw all'
show_dhcpv4_proxy__ksh[8]='dhcpd_show_proxy -a -D -m'

show_dhcpv4_proxy_exec[9]='show dhcp ipv4 proxy disconnect-history'
show_dhcpv4_proxy__ksh[9]='dhcpd_show_proxy -y'

show_dhcpv4_proxy_exec[10]='show dhcp ipv4 proxy disconnect-history detail'
show_dhcpv4_proxy__ksh[10]='dhcpd_show_proxy -y -T'

show_dhcpv4_proxy_exec[11]='show dhcp ipv4 trace errors'
show_dhcpv4_proxy__ksh[11]='dhcpd_show_ltrace -E -i all'

show_dhcpv4_proxy_exec[12]='show dhcp ipv4 trace events'
show_dhcpv4_proxy__ksh[12]='dhcpd_show_ltrace -V -i all'

show_dhcpv4_proxy_exec[13]='show dhcp ipv4 trace packet'
show_dhcpv4_proxy__ksh[13]='dhcpd_show_ltrace -P -i all'

show_dhcpv4_proxy_exec[14]='show dhcp ipv4 trace'
show_dhcpv4_proxy__ksh[14]='dhcpd_show_ltrace -A -i all'

show_dhcpv4_proxy_exec[15]='show dhcp ipv4 issu'
show_dhcpv4_proxy__ksh[15]='dhc4_issu_status_show'

show_dhcpv4_proxy_exec[16]='show dhcp ipv4 issu status'
show_dhcpv4_proxy__ksh[16]='dhc4_issu_status_show -i'

show_dhcpv4_proxy_exec[17]='show dhcp ipv4 database'
show_dhcpv4_proxy__ksh[17]='dhcpd_show_database'

show_dhcpv4_proxy_exec[18]='show cli history detail'
show_dhcpv4_proxy__ksh[18]='show_parser_history -h 2'

show_dhcpv4_proxy_exec[19]=''
show_dhcpv4_proxy__ksh[19]=''

#######################################################################
###              Show dhcpv4 proxy with location                    ###
#######################################################################
show_dhcpv4_proxy_rplc_exec[1]='show dhcp ipv4 proxy statistics location $location'
show_dhcpv4_proxy_rplc__ksh[1]='dhcpd_show_proxy -a -L $fq_nodeid'

show_dhcpv4_proxy_rplc_exec[2]='show dhcp ipv4 proxy statistics detail location $location'
show_dhcpv4_proxy_rplc__ksh[2]='dhcpd_show_proxy -a -O -L $fq_nodeid'

show_dhcpv4_proxy_rplc_exec[3]='show dhcp ipv4 proxy binding summary location $location'
show_dhcpv4_proxy_rplc__ksh[3]='dhcpd_show_proxy -e -S -L $fq_nodeid'

show_dhcpv4_proxy_rplc_exec[4]='show dhcp ipv4 proxy binding location $location'
show_dhcpv4_proxy_rplc__ksh[4]='dhcpd_show_proxy -e -L $fq_nodeid'

show_dhcpv4_proxy_rplc_exec[5]='show dhcp ipv4 proxy binding detail location $location'
show_dhcpv4_proxy_rplc__ksh[5]='dhcpd_show_proxy -e -T -L $fq_nodeid'

show_dhcpv4_proxy_rplc_exec[6]='show dhcp ipv4 proxy statistics raw all location $location'
show_dhcpv4_proxy_rplc__ksh[6]='dhcpd_show_proxy -a -L $fq_nodeid -D -m'

show_dhcpv4_proxy_rplc_exec[7]='show dhcp ipv4 proxy disconnect-history location $location'
show_dhcpv4_proxy_rplc__ksh[7]='dhcpd_show_proxy -y -L $fq_nodeid'

show_dhcpv4_proxy_rplc_exec[8]='show dhcp ipv4 proxy disconnect-history detail location $location'
show_dhcpv4_proxy_rplc__ksh[8]='dhcpd_show_proxy -y -T -L $fq_nodeid'

show_dhcpv4_proxy_rplc_exec[9]='show dhcp ipv4 trace errors location $location'
show_dhcpv4_proxy_rplc__ksh[9]='dhcpd_show_ltrace -i $fq_nodeid -E'

show_dhcpv4_proxy_rplc_exec[10]='show dhcp ipv4 trace events location $location'
show_dhcpv4_proxy_rplc__ksh[10]='dhcpd_show_ltrace -i $fq_nodeid -V'

show_dhcpv4_proxy_rplc_exec[11]='show dhcp ipv4 trace packets location $location'
show_dhcpv4_proxy_rplc__ksh[11]='dhcpd_show_ltrace -i $fq_nodeid -P'

show_dhcpv4_proxy_rplc_exec[12]='show dhcp ipv4 trace location $location'
show_dhcpv4_proxy_rplc__ksh[12]='dhcpd_show_ltrace -i $fq_nodeid -A'

show_dhcpv4_proxy_rplc_exec[13]='show dhcp ipv4 database location $location'
show_dhcpv4_proxy_rplc__ksh[13]='dhcpd_show_database -L $fq_nodeid'

show_dhcpv4_proxy_rplc_exec[14]=''
show_dhcpv4_proxy_rplc__ksh[14]=''

#######################################################################
###                         -P profile                              ###
#######################################################################
show_dhcpv4_proxy_profile_exec[1]='show dhcp ipv4 proxy profile name $profile_name'
show_dhcpv4_proxy_profile__ksh[1]='dhcpd_show_proxy -d $profile_name'

show_dhcpv4_proxy_profile_exec[2]=''
show_dhcpv4_proxy_profile__ksh[2]=''

show_dhcpv4_proxy_profile_rplc_exec[1]='show dhcp ipv4 proxy profile name $profile_name location $location'
show_dhcpv4_proxy_profile_rplc__ksh[1]='dhcpd_show_proxy -d $profile_name -L $fq_nodeid'

show_dhcpv4_proxy_profile_rplc_exec[2]=''
show_dhcpv4_proxy_profile_rplc__ksh[2]=''

#######################################################################
###                        -V vrf_name                              ###  
#######################################################################
show_dhcpv4_proxy_vrf_exec[1]='show dhcp ipv4 proxy binding vrf $vrf_name'
show_dhcpv4_proxy_vrf__ksh[1]='dhcpd_show_proxy -e -V $vrf_name'

show_dhcpv4_proxy_vrf_exec[2]='show dhcp vrf $vrf_name ipv4 proxy statistics'
show_dhcpv4_proxy_vrf__ksh[2]='dhcpd_show_proxy -b $vrf_name'

show_dhcpv4_proxy_vrf_exec[3]=''
show_dhcpv4_proxy_vrf__ksh[3]=''

show_dhcpv4_proxy_vrf_rplc_exec[1]='show dhcp vrf $vrf_name ipv4 proxy statistics location $location'
show_dhcpv4_proxy_vrf_rplc__ksh[1]='dhcpd_show_proxy -b $vrf_name -L $fq_nodeid'

show_dhcpv4_proxy_vrf_rplc_exec[2]=''
show_dhcpv4_proxy_vrf_rplc__ksh[2]=''

#######################################################################
###                     -C circuit_id                               ### 
#######################################################################
show_dhcpv4_proxy_cid_exec[1]='show dhcp ipv4 proxy binding circuit-id $circuit_id'
show_dhcpv4_proxy_cid__ksh[1]='dhcpd_show_proxy -e -C $circuit_id'

show_dhcpv4_proxy_cid_exec[2]=''
show_dhcpv4_proxy_cid__ksh[2]=''

show_dhcpv4_proxy_cid_rplc_exec[1]='show dhcp ipv4 proxy binding circuit-id $circuit_id location $location'
show_dhcpv4_proxy_cid_rplc__ksh[1]='dhcpd_show_proxy -e -C $circuit_id -L $fq_nodeid'

show_dhcpv4_proxy_cid_rplc_exec[2]=''
show_dhcpv4_proxy_cid_rplc__ksh[2]=''

#######################################################################
###                      -R remote_id                               ###
#######################################################################
show_dhcpv4_proxy_rid_exec[1]='show dhcp ipv4 proxy binding remote-id $remote_id'
show_dhcpv4_proxy_rid__ksh[1]='dhcpd_show_proxy -e -R $remote_id'

show_dhcpv4_proxy_rid_exec[2]=''
show_dhcpv4_proxy_rid__ksh[2]=''

show_dhcpv4_proxy_rid_rplc_exec[1]='show dhcp ipv4 proxy binding remote-id $remote_id location $location'
show_dhcpv4_proxy_rid_rplc__ksh[1]='dhcpd_show_proxy -e -R $remote_id -L $fq_nodeid'

show_dhcpv4_proxy_rid_rplc_exec[2]=''
show_dhcpv4_proxy_rid_rplc__ksh[2]=''

#########################################################################
##    Basic Proxy debug commands.                                      ##
#########################################################################
if [[ "$platform" = "panini" ]]; then
    Common1_exec[1]='show processes blocked location all'
    Common1__ksh[1]='sh_proc_ng_blocked all'
    Common1_exec[2]=''
    Common1__ksh[2]=''
else
    Common1_exec[1]='show processes blocked location all'
    Common1__ksh[1]='show_processes -b -n all'
    Common1_exec[2]=''
    Common1__ksh[2]=''
fi

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

     if [ "$__cardtype" = "SYS" ]; then
          exec_commands show_dhcpv4_proxy
          exec_commands Common1
          
          if [ "$profile_name" != "unspecified" ]; then
              exec_commands show_dhcpv4_proxy_profile
          fi

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

          if [ "$circuit_id" != "" ]; then
              exec_commands show_dhcpv4_proxy_cid
          fi

          if [ "$remote_id" != "" ]; then
               exec_commands show_dhcpv4_proxy_rid
          fi 
     else
          case "$__cardtype" in
          "DRP"|"RP"|"LC")
        
          exec_commands show_dhcpv4_proxy_rplc
          if [ "$profile_name" != "unspecified" ]; then
              exec_commands show_dhcpv4_proxy_profile_rplc
          fi

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

          if [ "$circuit_id" != "" ]; then
              exec_commands show_dhcpv4_proxy_cid_rplc
          fi

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

          ;;
        esac
     fi

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