#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# isis_show_memory - Show memory information for isis
#
# March 2022, Paul Wells
#
# Copyright (c) 2022 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------
#
# Most functionality in this script is implemented by calling external
# utilities, such as malloc_dump. However, some functionality is implemented
# via the "isis_show" executable. As most other isis show commands usually are.
#
# Here, when required, we call that "isis_show" executable.
#
for ARGUMENT in $*
do
    if [ "$ARGUMENT" == "--cmd" ]; then
        exec /pkg/bin/isis_show $*
        exit 0
    fi
done

#
# Call an external utility to show the relevant memory statistics.
#
. /pkg/bin/isis_show_functions

__line_length=79

usage() {
  echo "Usage: $0 [-Z instance ] {-a | -s | -p | -l | -c | -t}" 1>&2
  exit 1
}

#
# Define a shell-function for every action we can take.
#
show_summary() {
    sh_proc_mem_cli -p $1 -d
    echo ""
    malloc_dump -S $1
}

show_pc() {
    malloc_dump -c $1
}

show_library() {
    malloc_dump -l $1
}

show_chunks() {
    malloc_dump -K $1
}

show_traceback() {
    malloc_dump -R $1
}

show_depth() {
    malloc_dump -e$2 $1
}

show_all() {
    print_command_heading "IS-IS $2 Memory Summary"
    show_summary $1
    echo ""
    print_command_heading "IS-IS $2 Memory by PC"
    show_pc $1
    echo ""
    print_command_heading "IS-IS $2 Memory by Library"
    show_library $1
    echo ""
    print_command_heading "IS-IS $2 Memory Chunks"
    show_chunks $1
}

#
# Parse the command-line parameters.
#
while [ "$#" -gt "0" ]; do
    case "$1" in
        -Z) instance="$2"; shift 2;;
        -a) cmd="-a"; shift 1;;
        -s) cmd="-s"; shift 1;;
        -p) cmd="-p"; shift 1;;
        -l) cmd="-l"; shift 1;;
        -c) cmd="-c"; shift 1;;
        -t) cmd="-t"; shift 1;;
        -d) cmd="-d"; depth="$2"; shift 2;;
         *) usage;;
    esac
done

#
# Determine the process-ids and instance-names of the isis processes.
#
if [ -z "$instance" ]; then
    pid_str=`isis_show --cmd proto-global | grep "Process Id:" | sed s/"IOS-XR Process Id:"// `
    inst_str=`isis_show --cmd proto-global | grep "IS-IS Router:" | sed s/"IS-IS Router:"// `
else
    pid_str=`isis_show --cmd proto-global --instance $instance | grep "IOS-XR Process Id:" | sed s/"Process Id:"// `
    inst_str="$instance"
fi

if [ -z "$inst_str" ]; then
    echo "Can not determine IS-IS Instance name."
    exit 1
fi

pid_array=($pid_str)
inst_array=($inst_str)

#
# Call the requested script.
#
for i in "${!pid_array[@]}"
do
    case "$cmd" in
        -s) show_summary ${pid_array[i]};;
        -p) show_pc ${pid_array[i]};;
        -l) show_library ${pid_array[i]};;
        -c) show_chunks ${pid_array[i]};;
        -t) show_traceback ${pid_array[i]};;
        -d) show_depth ${pid_array[i]} $depth;;
        -a) show_all ${pid_array[i]} ${inst_array[i]};;
    esac
done

