#!/pkg/bin/ksh
# -------------------------------------------------
# SYSDB debug script
#
# Sep, 2012, Ketan Kotak
# 
#  Copyright (c) 2012-2014 by cisco Systems, Inc.
#  All rights reserved.
# -------------------------------------------------

# Name of the source file 
pname=$1
pid=$2
num_conns=$3
sh_cmd1=" -c show conn advanced"
sh_cmd2=" -c stats advanced"
# below [ ]+ is to match one or more spaces.
# loc=`sysmgr_show -h 2>&1 | grep -E "Node id[ ]+:" | awk '{print strtonum(\$4)}'`
# "awk" is not available in classic platform XR images
# change to use "sed" to extract the node id string - CSCuo69315
loc=`sysmgr_show -h 2>&1 | grep -E "Node id[ \t]*:" | sed 's/[ \t]*Node id[ \t]*:[ \t]*//g'`

#default options to -l when spawned from AIPC for non-SYSDB process.
option1="-l $loc"
option2="-l $loc"
if [[ $pname = "sysdb_svr_admin" ]]; then
    option1="-A"
    option2="-A"
elif [[ $pname = "sysdb_svr_local" ]]; then
    option1="-l $loc"
    option2="-l $loc"
elif [[ $pname = "sysdb_mc" ]]; then
    sh_cmd1=" -c show conn medusa-advanced"
    option1="-m $loc"
    option2="-l $loc"
elif [[ $pname = "sysdb_shared_sc" ]]; then
    option1="-s"
    option2="-s"
elif [[ $pname = "sysdb_shared_nc" ]]; then
    option1="-N"
    option2="-N"
fi
target="/tmp/sysdb_debug_conn_threshold_output_${pname}_${loc}_${num_conns}_ksh.txt"
rm -f $target
echo "Output from this script ($0) will be saved in $target"
echo "==== Start of Script when num_conns=${num_conns} ==============">> $target
echo "Arguments given: ($*)" >> $target

if [[ $# -gt 3 ]];then
   echo "Script invoked with $# args. will use only first 3" >> $target
fi

date >> $target
echo "Args used are: $1 $2 $3">> $target
echo "Prog Name is '${pname}'" >> $target
echo "PID is '${pid}'" >> $target
echo "Num Connections is '${num_conns}'" >> $target
echo "==================================">> $target
echo "+++++ sysdbshow $option1 $sh_cmd1 +++" >> $target
sysdbshow $option1 $sh_cmd1 >> $target
echo "----- sysdbshow $option1 $sh_cmd1 ---" >> $target
echo >> $target
echo "+++++ sysdbshow $option2 $sh_cmd2 +++" >> $target
sysdbshow $option2 $sh_cmd2 >> $target
echo "----- sysdbshow $option2 $sh_cmd2 ---" >> $target
echo >> $target
