#!/pkg/bin/ksh
#------------------------------------------------------------------
# show tech-support grpc command
#
# Copyright (c) 2016, 2020, 2022-2023 by cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------

#
# Include the standard show tech-support functions
#
. /pkg/bin/show_tech_main_fragment

exec_commands_shell() {
    cmd_set="$1"
    i=1; cmd_exec=$(eval "echo \${"$cmd_set"_exec[$i]}")
    while [ -n "$cmd_exec" ]; do
        cmd_exec=$(eval "echo $cmd_exec")
        echo -e "\n+++ $cmd_exec [`iosclock -d 0`] ++++\n" >>  $__tar_file_directory_on_node/exec_cmd_${int_node_name}.tech
        cmd__ksh=$(eval "echo \${"$cmd_set"__ksh[$i]}")
        cmd__ksh=$(eval "echo $cmd__ksh")
        eval $cmd__ksh >> $__tar_file_directory_on_node/exec_cmd_${int_node_name}.tech
        if [ "$?" != 0 ]; then
             echo "$__errormsg $cmd_exec"
        fi
        echo -e "\n--- $cmd_exec [`iosclock -d 0`] ----\n" >>  $__tar_file_directory_on_node/exec_cmd_${int_node_name}.tech

         # next command from array
         i=$(($i + 1))
         cmd_exec=$(eval "echo \${"$cmd_set"_exec[$i]}")
    done
}

OS_STRING=`uname -s`
jid=`sysmgr_show -p emsd | grep Job |  cut -f 2 -d ":" | cut -f 2 -d " "`

# get_pid function. Given the process name, it will
# echo the pid for that process
# it will have code to take care of the host OS.
function get_pid {
    if [[ ${OS_STRING} = "QNX" ]]; then
        pid=`pidin | grep $1 | cut -c1-8 | head -n 1`
    else
        pid=`ps -el | grep $1 | awk '{ print $4; }' | head -n 1`
    fi
    echo ${pid}
}

#cardtype flag
__cardtype="unspecified"
#debug flag to separate the logs of commands with larger data
__with_debug_data=0

# Parse the arguments to the script.
while [ "$#" -gt "0" ]; do
    case "$1" in
        -L)
            __with_debug_data=1
            __t_sec=540
            shift 1
            ;;
        -t) __cardtype="$2"; shift 2;;
        -S) shared=1;        shift 1;;
        -l) location="$2";   shift 2;;
        *)  default_parser_function "$@"; shift $#;;
    esac
done

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

#
# Find the process IDs of the agent processes
#
grpc_pid=`get_pid emsd`


ext_node_name=`node_list_generation -f MY`

int_node_name=`node_conversion -N $ext_node_name`

location=$ext_node_name

#Added debug flag to separate output of commands with larger data
exec_debug_cmds () {
    debug_exec[1]='show running-config \"debug '$__with_debug_data'\"'
    debug__ksh[1]='nvgen -c -l 1 -t 1 -o 1 \"debug '$__with_debug_data'\"'
    debug_exec[2]='show process memory detail \"debug '$__with_debug_data'\"'
    debug__ksh[2]='sh_proc_memory_sort sh_proc_mem_cli -d \"debug '$__with_debug_data'\"'
    debug_exec[3]='show grpc trace ems \"debug '$__with_debug_data'\"'
    debug__ksh[3]='yfed_show_trace -T ems/errors -T ems/events -T ems/info -T ems/conf -T ems/grpc -T ems/gnmi \"debug '$__with_debug_data'\"'

    debug_exec[4]='show memory $grpc_pid \"debug '$__with_debug_data'\"'
    debug__ksh[4]='show_memory_ng -p $grpc_pid \"debug '$__with_debug_data'\"'
    debug_exec[5]='show logging \"debug '$__with_debug_data'\"'
    debug__ksh[5]='show_logging \"debug '$__with_debug_data'\"'
    debug_exec[6]='show grpc trace yfed \"debug '$__with_debug_data'\"'
    debug__ksh[6]='yfed_show_trace -T yfed/errors-ems -T yfed/events-ems -T yfed/info-ems \"debug '$__with_debug_data'\"'
    debug_exec[7]='show grpc trace yfw \"debug '$__with_debug_data'\"'
    debug__ksh[7]='yfed_show_trace -T ems-yfw/processor.trace -T ems-yfw/map.trace -T ems-yfw/calvados.trace -T ems-yfw/qt.trace -T ems-yfw/me.trace -T ems-yfw/bk.trace \"debug '$__with_debug_data'\"'
}

exec_debug_cmds

#
# Commands to run
#
common_exec[1]="show process emsd"
common__ksh[1]='sysmgr_show -o -p emsd'
if [[ "${OS_STRING}" = "QNX" ]]; then
    common_exec[2]="show version"
    common__ksh[2]="show_version"
    common_exec[3]="show configuration sessions"
    common__ksh[3]="cfgmgr_cmd_config_target_sessions"
    common_exec[4]="show process memory $jid"
    common__ksh[4]="malloc_dump -p $jid"
    common_exec[5]="show install fixes active"
    common__ksh[5]="xrinstall show-fixes active"
    common_exec[6]="show install active summary"
    common__ksh[6]="xrinstall show-packages active -s"
    common_exec[7]="show context"
    common__ksh[7]="corehelper_context -c 0x1 -n all"
    common_exec[8]="show memory-snapshots process emsd location $location"
    common__ksh[8]="show_memory_snapshots -p emsd -n $location"

else
    common_exec[2]="show configuration sessions"
    common__ksh[2]="cfgmgr_cmd_config_target_sessions"
    common_exec[3]="show process memory $grpc_pid"
    common__ksh[3]="sh_proc_mem_cli -p $grpc_pid"
    common_exec[4]="show process mem-stats summary pid $grpc_pid"
    common__ksh[4]="malloc_dump -s $grpc_pid"
    common_exec[5]="show process mem-stats pid $grpc_pid"
    common__ksh[5]="malloc_dump -m $grpc_pid"
    common_exec[6]="show version"
    common__ksh[6]='ng_show_version'
    common_exec[7]="show memory summary"
    common__ksh[7]='show_memory_ng -s'
    common_exec[8]="show install fixes active"
    common__ksh[8]="xrinstall show-fixes active"
    common_exec[9]="show install active summary"
    common__ksh[9]="xrinstall show-packages active -s"
    common_exec[10]="show context"
    common__ksh[10]="corehelper_context -c 0x1 -n all"
    common_exec[11]="show memory-snapshots process emsd location $location"
    common__ksh[11]="show_memory_snapshots -p emsd -n $location"
    common_exec[12]="show configuration internal yiny trace"
    common__ksh[12]="yiny_show_ltrace"
    common_exec[13]="configuration internal yiny dump database gnmi json external output"
    common__ksh[13]="yiny_db_dump_cmd -a gnmi -f PWD -p /tmp/yiny_db_dump.gnmi -x -j external -t 0x0 -o 0x0"
fi

ssh_exec[1]="show ssh"
ssh__ksh[1]='show_ssh_session'
ssh_exec[2]="show ssh session details"
ssh__ksh[2]='show_ssh_alginfo'

grpc_exec[1]="show grpc status"
grpc__ksh[1]='show_grpc grpc status'
grpc_exec[2]="show grpc session all"
grpc__ksh[2]="show_grpc grpc sessions 0x0"
grpc_exec[3]="show grpc statistics"
grpc__ksh[3]="show_grpc grpc statistics"
grpc_exec[4]="show grpc tunnel sessions"
grpc__ksh[4]="show_grpc grpc tunnel-sessions"
grpc_exec[5]="show grpc streams"
grpc__ksh[5]="show_grpc streams"


#
# Follow (attach_process) Commands
#

attach_exec[1]="follow process $grpc_pid 1"
attach__ksh[1]="attach_process -p $grpc_pid -i 1"

#
# process Commands
#
PLATFORM_TYPE=`uname -m`
block_exec[1]="show processes block"
if [[ "${PLATFORM_TYPE}" == "x86_64" ]]; then
    block__ksh[1]='sh_proc_ng_blocked'
else
    block__ksh[1]='show_processes -b'
fi


system_exec[1]="show redundancy"
system__ksh[1]='redcon_show'
if [[ "${PLATFORM_TYPE}" != "x86_64" ]]; then
    system_exec[2]="admin show platform"
    system__ksh[2]='shelfmgr_show_hfr -a'
else
    system_exec[2]="run lsof -U -a -p $grpc_pid"
    system__ksh[2]="lsof -U -a -p $grpc_pid"
    system_exec[3]='run cat /etc/build-info.txt'
    system__ksh[3]='cat /etc/build-info.txt'
    system_exec[4]='run cat /etc/os-release'
    system__ksh[4]='cat /etc/os-release'
    system_exec[5]='run cat /etc/thirdparty-release'
    system__ksh[5]='cat /etc/thirdparty-release'
    system_exec[6]='run uptime'
    system__ksh[6]='uptime'
fi


sh_tech_err_file=$__tar_file_directory_on_node/sh_tech.err

log_file_list='
               /var/log/emsd_go.log*
              '

gather_log_files_from_list () {
    ext_node_folder=`node_conversion -N $ext_node_name`_logs

    # Create folder to host the files
    if [ ! -e "$__tar_file_directory_on_node/$ext_node_folder" ]; then
        mkdir $__tar_file_directory_on_node/$ext_node_folder 2>> $sh_tech_err_file
    fi

    # Copy all the files to the tar file directory
    for file in $log_file_list; do
        if [ -e $file ]; then
            cp -f $file $__tar_file_directory_on_node/$ext_node_folder 2>> $sh_tech_err_file
        fi
    done

    if [ $__with_debug_option = 1 ]; then
        if [ -e $__tar_file_directory_on_node/exec_cmd_${int_node_name}.tech ]; then
            # To gzip the exec_cmd_*.tech file for corresponding node
            gzip $__tar_file_directory_on_node/exec_cmd_${int_node_name}.tech 2>> $sh_tech_err_file
        fi
        rm -f /misc/disk1/tmp_*_network_link_*.log* /misc/disk1/tmp_*_save_state.log*
    fi

}

#
# Our display function
#
display() {
    #
    # Display a header
    #
    if [[ $shared -eq 1 ]]; then
        print_main_heading "show tech-support grpc on shared plane"
    fi

    if [[ "$__cardtype" == "SYS" ]]; then
        #
        # Display the nodes in the LR or system
        # Display the redundancy status of the system
        #
        exec_commands common
        exec_commands system

        #
        # run the ssh commands
        #
        exec_commands ssh


    else
        #
        # Collect follow grpc process
        #
        print_command_heading "follow grpc process"

        #
        # attach_process
        #
        exec_commands attach

    fi

    #
    # run the grpc commands
    #
    exec_commands grpc

    # Copy the debug log files
    gather_log_files_from_list

    # Run the debug option
    exec_commands_shell debug

    # Run the YINY fragment
    . /pkg/bin/show_tech_yiny_fragment

    #
    # Display a footer
    #
    print_main_heading "show tech-support grpc complete"

}

#
# Use the standard show tech-support infra to call our display function and
# send the output to console or file
#
. /pkg/bin/show_tech_file_fragment

