#!/pkg/bin/ksh
# -----------------------------------------------------------------------------
# show_tech_pam - Show tech-support for PAM
#
# July 2021, Akshansh Jain
#
# Copyright (c) 2021 by cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------------------

#
# Load the script provided by show-tech infra, which provides worker functions
#
. /pkg/bin/show_tech_main_fragment

#
# Parse the arguments to the script
#
__cardtype="unspecified"
while [ "$#" -gt "0" ]; do
    case "$1" in
        -t) __cardtype="$2"; shift 2;;
        *)  default_parser_function "$@"; shift $#;;
    esac
done

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

copy_rp_files() {
    ext_node_name=$(node_list_generation -f MY)
    int_node_name=$(node_conversion -N "$ext_node_name")
    ps -eaf | grep "pam\|sshfs" > $__tar_file_directory_on_node/"${int_node_name}"_ps_grep_pam_output

    LOG_FILES=( /var/log/pam_manager.log \
                /var/log/pam_install.log \
                /var/log/start_pam.log \
                /var/log/pam-edcd-execution.log \
                /var/log/pam_plugin_install.log \
                /etc/cron.d/edcd_scheduler.txt)
    for LOG_FILE in "${LOG_FILES[@]}"
    do
        # Replace "/" with "_"
        LOG_FILE_CONVERTED=${LOG_FILE//\//_}
        [ -f "$LOG_FILE" ] && cp -p "$LOG_FILE" $__tar_file_directory_on_node/"${int_node_name}""$LOG_FILE_CONVERTED"
        [ -f "${LOG_FILE}".1.gz ] && cp -p "${LOG_FILE}".1.gz $__tar_file_directory_on_node/"${int_node_name}""$LOG_FILE_CONVERTED.1.gz"
        [ -f "${LOG_FILE}".2.gz ] && cp -p "${LOG_FILE}".2.gz $__tar_file_directory_on_node/"${int_node_name}""$LOG_FILE_CONVERTED.2.gz"
    done

    LOG_DIRS=(  /misc/disk1/cisco_support \
                /misc/scratch/pam  \
                /misc/disk1/pam \
                /opt/pam \
                /pkg/opt/cisco/pam)
    for LOG_DIR in "${LOG_DIRS[@]}"
    do
        # Replace "/" with "_"
        LOG_DIR_CONVERTED=${LOG_DIR//\//_}
        [ -d "$LOG_DIR" ] && cp -rp "$LOG_DIR" $__tar_file_directory_on_node/"${int_node_name}""$LOG_DIR_CONVERTED"
    done
}

# This method is needed as PAM's show cpu/memory commands can only be run from Active RP for all nodes
add_cpu_memory_commands() {
    exec_commands=( "show cpu-memory-snapshots location" \
                    "show cpu-top-consumers location" \
                    "show memory-top-consumers location" \
                    "show memory-delta location")
    ksh_commands=(  "show_cpu_memory_snapshots -n" \
                    "show_mem_cpu_summary.pl -C -n"
                    "show_mem_cpu_summary.pl -M -n"
                    "show_mem_cpu_summary.pl -L -n")
    rp_lc_nodes=()

    # Get all RP nodes, convert notation and append
    rp_ext_nodes=$(node_list_generation -f RP)
    read -r -a rp_ext_nodes <<< "$rp_ext_nodes"
    for node in "${rp_ext_nodes[@]}"
    do
        converted_node=$(node_conversion -E "$node")
        rp_lc_nodes+=("$converted_node")
    done

    # Get all LC nodes, convert notation and append
    lc_ext_nodes=$(node_list_generation -f LC)
    read -r -a lc_ext_nodes <<< "$lc_ext_nodes"
    for node in "${lc_ext_nodes[@]}"
    do
        converted_node=$(node_conversion -E "$node")
        rp_lc_nodes+=("$converted_node")
    done

    # Add commands to system show command array for each node
    for node in "${rp_lc_nodes[@]}"
    do
        for i in "${!exec_commands[@]}"
        do
            exec_command="${exec_commands[$i]} ${node}"
            ksh_command="${ksh_commands[$i]} ${node}"
            sys_show_exec+=("$exec_command")
            sys_show__ksh+=("$ksh_command")
        done
    done

}

# ***********************************************************
#  Show commands to be run by the show tech-support commands
# ***********************************************************

#############################################################
# Show commands that run once per system
sys_cmd_index=1
sys_show_exec[$sys_cmd_index]='show version'
sys_show__ksh[$sys_cmd_index]='ng_show_version'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show platform'
sys_show__ksh[$sys_cmd_index]='show_platform_sysdb'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show logging'
sys_show__ksh[$sys_cmd_index]='show_logging'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show running-config'
sys_show__ksh[$sys_cmd_index]='nvgen -c -l 1 -t 1 -o 1'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show install active'
sys_show__ksh[$sys_cmd_index]='sdr_instcmd show install active'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show processes blocked location all'
sys_show__ksh[$sys_cmd_index]='sh_proc_ng_blocked -l all'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show processes pam_manager location all'
sys_show__ksh[$sys_cmd_index]='sysmgr_show -o -p pam_manager -n all'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show memory summary location all'
sys_show__ksh[$sys_cmd_index]='show_memory_ng -n all -s'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show edcd eem database'
sys_show__ksh[$sys_cmd_index]='show_edcd -E -d'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show edcd eem logs'
sys_show__ksh[$sys_cmd_index]='show_edcd -E -l'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show edcd execution-status'
sys_show__ksh[$sys_cmd_index]='show_edcd -S'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show edcd ondemand database'
sys_show__ksh[$sys_cmd_index]='show_edcd -o -b'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show edcd pam event'
sys_show__ksh[$sys_cmd_index]='show_edcd -V'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show edcd pam pattern'
sys_show__ksh[$sys_cmd_index]='show_edcd -P'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show edcd scheduler'
sys_show__ksh[$sys_cmd_index]='show_edcd -j'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show pam status'
sys_show__ksh[$sys_cmd_index]='pam_status'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show pam process-monitoring-status'
sys_show__ksh[$sys_cmd_index]='pam_status -p'

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

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

        #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/bin/vrfch.sh CTRL_VRF /opt/cisco/calvados/script/show_tech_fast -r -c pam -i '$RP_IP' -t /'$__tar_file_directory'/  -m show_tech_pam_calvados
EOF
    elif [ "$__cardtype" == "SYS" ]; then
        add_cpu_memory_commands
        exec_commands sys_show
    else [ "$__cardtype" == "RP" ] || [ "$__cardtype" == "DRP" ]
        copy_rp_files
    fi
    print_main_heading "show tech-support PAM 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
