#! /pkg/bin/ksh
#
#------------------------------------------------------------------
# hcmgr_show_tech - Healthcheck show tech.
#
# Jul. 2021, Shashank Konda
#
# Copyright (c) 2018-2022 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.

if [ -S "/misc/app_host/docker.sock" ]; then
   export DOCKER_HOST=unix:///misc/app_host/docker.sock
fi

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"
sys_exec[6]="show processes hcmgr"
sys__ksh[6]="sysmgr_show -o -p hcmgr"
sys_exec[7]="show logging"
sys__ksh[7]="show_logging"
sys_exec[8]="show platform"
sys__ksh[8]="show_platform"
sys__ksh[9]=''
#Show Cli's for Healthcheck Metrics
rp_exec[1]="show healthcheck metric asic-errors summary"
rp__ksh[1]="hcmgr_show_metric -U"
rp_exec[2]="show healthcheck metric asic-errors detail all"
rp__ksh[2]="hcmgr_show_metric -U all"
rp_exec[3]="show healthcheck metric cpu"
rp__ksh[3]="hcmgr_show_metric -C"
rp_exec[4]="show healthcheck metric fabric-health"
rp__ksh[4]="hcmgr_show_metric -B"
rp_exec[5]="show healthcheck metric fabric-stats summary"
rp__ksh[5]="hcmgr_show_metric -M"
rp_exec[6]="show healthcheck metric fabric-stats detail all"
rp__ksh[6]="hcmgr_show_metric -M all"
rp_exec[7]="show healthcheck metric filesystem"
rp__ksh[7]="hcmgr_show_metric -E"
rp_exec[8]="show healthcheck metric fpd"
rp__ksh[8]="hcmgr_show_metric -P"
rp_exec[9]="show healthcheck metric free-mem"
rp__ksh[9]="hcmgr_show_metric -F"
rp_exec[10]="show healthcheck metric interface-counters summary"
rp__ksh[10]="hcmgr_show_metric -T"
rp_exec[11]="show healthcheck metric interface-counters detail all"
rp__ksh[11]="hcmgr_show_metric -T all"
rp_exec[12]="show healthcheck metric platform"
rp__ksh[12]="hcmgr_show_metric -A"
rp_exec[13]="show healthcheck metric redundancy"
rp__ksh[13]="hcmgr_show_metric -D"
rp_exec[14]="show healthcheck metric shared-mem"
rp__ksh[14]="hcmgr_show_metric -S"
rp_exec[15]="show healthcheck status"
rp__ksh[15]="hcmgr_show_metric -H"
rp_exec[16]="show healthcheck report"
rp__ksh[16]="hcmgr_show_metric -R"
#ctrace logs and collector logs
rp_exec[17]="run /pkg/bin/ctracedec -AgksLNw hcmgr/debug"
rp__ksh[17]="ctracedec -AgksLNw hcmgr/debug"
rp_exec[18]="run /pkg/bin/ctracedec -AgksLNw hcmgr/error"
rp__ksh[18]="ctracedec -AgksLNw hcmgr/error"
rp_exec[19]="run cat /var/log/hc_collector_sys-res"
rp__ksh[19]="cat /var/log/hc_collector_sys-res"
rp_exec[20]="run cat /var/log/hc_collector_pfm-stats"
rp__ksh[20]="cat /var/log/hc_collector_pfm-stats"
rp_exec[21]="show nosilib trace info"
rp__ksh[21]="show_libnosi_ltrace -I"
rp_exec[22]="show nosilib trace error"
rp__ksh[22]="show_libnosi_ltrace -E"
rp_exec[23]="show healthcheck internal pipeline states"
rp__ksh[23]="hcmgr_show_metric -Q"
rp_exec[24]="show healthcheck internal services"
rp__ksh[24]="hcmgr_show_metric -N"
rp_exec[25]="show healthcheck status use-case"
rp__ksh[25]="hcmgr_show_metric -Z"
rp_exec[26]="show healthcheck status metric"
rp__ksh[26]="hcmgr_show_metric -O"
rp_exec[27]="show healthcheck report metric"
rp__ksh[27]="hcmgr_show_metric -W"
rp_exec[28]="show healthcheck report use-case"
rp__ksh[28]="hcmgr_show_metric -Y"
rp_exec[29]="show healthcheck internal states"
rp__ksh[29]="hcmgr_show_metric -V"
rp_exec[30]="show healthcheck use-case asic-reset summary"
rp__ksh[30]="hcmgr_show_metric -G"
rp_exec[31]="show healthcheck use-case asic-reset detail all all"
rp__ksh[31]="hcmgr_show_metric -G all all"
rp_exec[32]="show healthcheck use-case packet-drop summary"
rp__ksh[32]="hcmgr_show_metric -J"
rp_exec[33]="show healthcheck use-case packet-drop detail all all"
rp__ksh[33]="hcmgr_show_metric -J all all"
rp_exec[34]="run docker logs NOSi"
rp__ksh[34]="docker logs NOSi"
rp_exec[35]="run docker exec NOSi ps -eTlf | grep action"
rp__ksh[35]="docker exec NOSi ps -eTlf | grep action"
rp_exec[36]="run docker exec NOSi ps -eTlf | grep collector"
rp__ksh[36]="docker exec NOSi ps -eTlf | grep collector"
rp_exec[37]="run docker exec NOSi ps -eTlf | grep correlator"
rp__ksh[37]="docker exec NOSi ps -eTlf | grep correlator"
rp_exec[38]="run docker exec NOSi ps -eTlf | grep metric_analyzer"
rp__ksh[38]="docker exec NOSi ps -eTlf | grep metric_analyzer"
rp_exec[39]="run docker exec NOSi ps -eTlf | grep redis-server"
rp__ksh[39]="docker exec NOSi ps -eTlf | grep redis-server"
rp_exec[40]="run docker exec NOSi ps -eTlf | grep pipeline_monitor"
rp__ksh[40]="docker exec NOSi ps -eTlf | grep pipeline_monitor"
rp_exec[41]="run docker exec NOSi ps -eTlf | grep sorted_set_purge"
rp__ksh[41]="docker exec NOSi ps -eTlf | grep sorted_set_purge"
rp_exec[42]="show healthcheck internal pipeline statistics"
rp__ksh[42]="hcmgr_show_metric -X"
rp_exec[43]="run docker exec NOSi supervisorctl status"
rp__ksh[43]="docker exec NOSi supervisorctl status"
rp_exec[44]="run docker exec NOSi redis-cli -s /root/redis/redis.sock save"
rp__ksh[44]="docker exec NOSi redis-cli -s /root/redis/redis.sock save"
rp_exec[45]="run docker exec NOSi redis-cli -s /root/redis/redis.sock info"
rp__ksh[45]="docker exec NOSi redis-cli -s /root/redis/redis.sock info"
rp__ksh[46]=''


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

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
	outfile2="redis_save_$(date +%Y-%m-%d.%H:%M:%S)"
        docker cp NOSi:/data/dump.rdb $__tar_file_directory_on_node/$outfile2
    else 
        case "$__cardtype" in
        "DRP"|"RP")   
            exec_commands rp
	    outfile="ps_hc_$(date +%Y-%m-%d.%H:%M:%S).txt"
            ps -ef | grep hc_collector > $__tar_file_directory_on_node/$outfile
            outfile1="nosi_$(date +%Y-%m-%d.%H:%M:%S)"
            docker cp NOSi:/var/log/nos-i/ $__tar_file_directory_on_node/$outfile1
            outfile2="redis_save_$(date +%Y-%m-%d.%H:%M:%S)"
            docker cp NOSi:/data/dump.rdb $__tar_file_directory_on_node/$outfile2 
        ;;
        "LC")
            exec_commands lc
        ;;
        esac
       exec_commands all	
    fi 

    print_main_heading "show tech-support healthcheck complete"
}

. /pkg/bin/show_tech_file_fragment
