#!/pkg/bin/ksh
#
# show_sdr_mgbl_proxy_debug command
#
# Oct 2014, Srihari Kondapaneni
#
# Copyright (c) 2014-2015 by cisco Systems, Inc.
#
# All rights reserved.
#----------------------------------------------------------------------------
# This script collects all mgbl proxy traces and logs for
# all the instances..

SDR_MGBL_PROXY_LTRACE_DIRS="/dev/shmem/ltrace/sdr_mgbl"
#jobid to hex
to_hex() {
    if (($# == 0)); then
        echo "no jobids to convert"
        return 
    fi

    let int="${1}"
    let size="${2:-0}"  #length of output hex string

    declare -a digit
    digit=( 0 1 2 3 4 5 6 7 8 9 a b c d e f )
    hex=''

    while ((int > 0)); do
         hex="${digit[$((int % 16))]}${hex}"
         int=$((int / 16))
    done

    if ((size != 0)); then
        typeset -Z${size} zeros=0
        typeset -R${size} hex="${zeros}${hex}"
    fi

    echo ${hex}
    return 
}

if [[ "${0##*/}" == "show_sdr_mgbl_debug" ]]; then
   (
   date
   uname -a
   for DIR in $(ls $SDR_MGBL_PROXY_LTRACE_DIRS)
   do
       echo "=============================================="
       hex_jid=$(to_hex "$DIR" )
       sysmgr_show -o -p 0x$hex_jid
       rack=`sysmgr_show -o -p 0x$hex_jid | grep "Process group:" | awk -F _ '{print $NF}'`
       echo "=============================================="
       echo "sdr_mgbl_proxy dump for rack $rack"
       /pkg/bin/sdr_mgbl_dump_tool -r $rack
       echo ""
       echo "=============================================="
       echo "sdr_mgbl_proxy traces for job_id $DIR"
       echo "=============================================="
       echo "show_sdr_mgbl_trace -j $DIR"
       show_sdr_mgbl_trace -j $DIR 
       echo "=============================================="
   done
   ) 2>&1
fi
