#!/bin/bash
# -----------------------------------------------------------------------------
# show_tech_pam_calvados - Show tech-support for PAM in Sysadmin
#
# 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
#
source /opt/cisco/calvados/script/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=$(/opt/cisco/calvados/script/node_type)
fi

node_name=$(uname -n | cut -d ":" -f2 | tr _ /)

copy_rp_files() {
    int_node_name=${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)
    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/disk1/pam  \ # check
                /opt/pam \
                /opt/cisco/pam \
                /opt/cisco/calvados/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
}

# ***********************************************************
#  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]='/opt/cisco/calvados/bin/show_cmd "show version | nomore"'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show platform'
sys_show__ksh[$sys_cmd_index]='/opt/cisco/calvados/bin/show_cmd "show platform | nomore"'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show logging'
sys_show__ksh[$sys_cmd_index]='/opt/cisco/calvados/bin/show_cmd "show logging | nomore"'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show running-config'
sys_show__ksh[$sys_cmd_index]='/opt/cisco/calvados/bin/show_cmd "show running-config | nomore"'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show install active'
sys_show__ksh[$sys_cmd_index]='/opt/cisco/calvados/bin/show_cmd "show install active | nomore"'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show processes pam_manager location all'
sys_show__ksh[$sys_cmd_index]='/opt/cisco/calvados/bin/show_cmd "show processes pam_manager location all | nomore"'

((sys_cmd_index++))
sys_show_exec[$sys_cmd_index]='show memory summary location all'
sys_show__ksh[$sys_cmd_index]='/opt/cisco/calvados/bin/show_cmd "show memory summary location all | nomore"'

rp_lc_cmd_index=1
rp_lc_show_exec[$rp_lc_cmd_index]='show memory_status location $node_name'
rp_lc_show__ksh[$rp_lc_cmd_index]='/opt/cisco/calvados/bin/show_cmd "show memory_status location $node_name | nomore"'

((rp_lc_cmd_index++))
rp_lc_show_exec[$rp_lc_cmd_index]='show cpu-memory-snapshots location $node_name'
rp_lc_show__ksh[$rp_lc_cmd_index]='/opt/cisco/calvados/bin/show_cmd "show cpu-memory-snapshots location $node_name | nomore"'

((rp_lc_cmd_index++))
rp_lc_show_exec[$rp_lc_cmd_index]='show processes memory location $node_name'
rp_lc_show__ksh[$rp_lc_cmd_index]='/opt/cisco/calvados/bin/show_cmd "show processes memory location $node_name | nomore"'

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

    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys_show
    elif [ "$__cardtype" == "RP" ] || [ "$__cardtype" == "DRP" ]; then
        exec_commands rp_lc_show
        copy_rp_files
    elif [ "$__cardtype" == "LC" ]; then
        exec_commands rp_lc_show
    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.
#
source /opt/cisco/calvados/script/show_tech_file_fragment
