#! /pkg/bin/ksh
#
#------------------------------------------------------------------
# hcmgr_show_tech - Healthcheck show tech.
#
# Apr. 2020, Shashank Konda
#
# Copyright (c) 2018-2021 by cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------


. /pkg/bin/show_tech_main_fragment

__cardtype="unspecified";
# Read in the arguments to the script, setting node_required and filename
# according to these arguments.
# Note that it is important for security reasons that users can only enter
# alphanumeric filenames and nodes and that anywhere calling this script must
# enforce this.

while [ $# -gt 0 ]; do
  case "$1" in
     -t) __cardtype="$2";            shift 2;;
     -l) location="$2";shift 2;;
     *)  default_parser_function "$@"; shift $#;;
  esac
done

if [ "$__cardtype" == "unspecified" ]; then
    __cardtype=`node_type`
fi

HC_TAR_DIR="/harddisk:/hc_tar"
HC_TAR_BACKUP_DIR="/harddisk:/hc_tar_backup"

if [ -d ${HC_TAR_DIR} ]; then
    #
    # The directory exists, but are there any files that need copying in it?
    # Use the rc of ls to determine if any files of the form that should be
    # copied exist.
    #
    ls ${HC_TAR_DIR}/*.log 1> /dev/null 2>&1
    if [ $? -eq 0 ]; then
        cp ${HC_TAR_DIR}/*.log $__tar_file_directory_on_node
    fi
fi

#----------------------------------------------------------------------------
# System commands
#
sys_exec[1]="show install log"
sys__ksh[1]="xrinstall show-log"
sys_exec[2]="show version"
sys__ksh[2]="show_version"
sys_exec[3]="show running-config"
sys__ksh[3]="nvgen -c -l 1 -t 1 -o 1"
sys_exec[4]="show netconf-yang status"
sys__ksh[4]="show_netconf_agent status"
sys_exec[5]="show install active summary"
sys__ksh[5]="xrinstall show-packages active -s"
#Show Cli's for Healthcheck Metrics
sys_exec[6]="show healthcheck metric asic-errors summary"
sys__ksh[6]="hcmgr_show_metric -U"
sys_exec[7]="show healthcheck metric asic-errors detail all"
sys__ksh[7]="hcmgr_show_metric -U all"
sys_exec[8]="show healthcheck metric cpu"
sys__ksh[8]="hcmgr_show_metric -C"
sys_exec[9]="show healthcheck metric fabric-health"
sys__ksh[9]="hcmgr_show_metric -B"
sys_exec[10]="show healthcheck metric fabric-stats summary"
sys__ksh[10]="hcmgr_show_metric -M"
sys_exec[11]="show healthcheck metric fabric-stats detail all"
sys__ksh[11]="hcmgr_show_metric -M all"
sys_exec[12]="show healthcheck metric filesystem"
sys__ksh[12]="hcmgr_show_metric -E"
sys_exec[13]="show healthcheck metric fpd"
sys__ksh[13]="hcmgr_show_metric -P"
sys_exec[14]="show healthcheck metric free-mem"
sys__ksh[14]="hcmgr_show_metric -F"
sys_exec[15]="show healthcheck metric interface-counters summary"
sys__ksh[15]="hcmgr_show_metric -T"
sys_exec[16]="show healthcheck metric interface-counters detail all"
sys__ksh[16]="hcmgr_show_metric -T all"
sys_exec[17]="show healthcheck metric platform"
sys__ksh[17]="hcmgr_show_metric -A"
sys_exec[18]="show healthcheck metric redundancy"
sys__ksh[18]="hcmgr_show_metric -D"
sys_exec[19]="show healthcheck metric shared-mem"
sys__ksh[19]="hcmgr_show_metric -S"
sys_exec[20]="show healthcheck status"
sys__ksh[20]="hcmgr_show_metric -H"
sys_exec[21]="show healthcheck report"
sys__ksh[21]="hcmgr_show_metric -R"
#ctrace logs and collector logs
sys_exec[22]="run /pkg/bin/ctracedec -AgksLNw hcmgr/debug"
sys__ksh[22]="ctracedec -AgksLNw hcmgr/debug"
sys_exec[23]="run /pkg/bin/ctracedec -AgksLNw hcmgr/error"
sys__ksh[23]="ctracedec -AgksLNw hcmgr/error"
sys_exec[24]="run cat /var/log/hc_collector_sys-res"
sys__ksh[24]="cat /var/log/hc_collector_sys-res"
sys_exec[25]="run cat /var/log/hc_collector_pfm-stats"
sys__ksh[25]="cat /var/log/hc_collector_pfm-stats"
sys_exec[26]="show logging"
sys__ksh[26]="show_logging"
sys_exec[27]="show platform"
sys__ksh[27]="show_platform"
sys__ksh[28]=''


#--------------------------------------------------------------------

display() {
    print_main_heading "show tech-support healthcheck"

    if [ "$__cardtype" == "ONESYS" ]; then
         #setting admin flag for collecting showtech from sysadmin
         echo "one showtech" >> $__tar_file_directory_on_node/admin_flag

         #checking for chvrf support
         if [[ `which chvrf` != "" ]]; then
             #adding -n option to prevent calvados console from time out
             ./pkg/sbin/admin-cli-proxy-xr_static -n -p XR << EOF
             run uname -a;run which scp;run chvrf 0 /opt/cisco/calvados/script/show_tech_fast -r -c healthcheck -i '$RP_IP' -t /'$__tar_file_directory'/  -m hcmgr_show_tech
EOF
         else
             #adding -n option to prevent calvados console from time out
             /pkg/sbin/admin-cli-proxy-xr_static -n -p XR << EOF
             run uname -a;run which scp;run /opt/cisco/calvados/script/show_tech_fast -r -c healthcheck -i '$RP_IP' -t /'$__tar_file_directory'/  -m hcmgr_show_tech
EOF
         fi

    elif [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
	outfile="ps_hc_$(date +%Y-%m-%d.%H:%M:%S).txt"
	ps -ef | grep hc_collector > $__tar_file_directory_on_node/$outfile

    else 
        case "$__cardtype" in
        "DRP"|"RP")   
            exec_commands rp 
        ;;
        "LC")
            exec_commands lc
        ;;
        esac
       exec_commands all	
    fi 

    print_main_heading "show tech-support healthcheck complete"
}

. /pkg/bin/show_tech_file_fragment
