HI Folks -
I have a business need to check weather or not there are "active" sessions within a particular application I interact with prior to running any automation.
I have built a function that first exports all "sessions" from my respective application to a text file. The output is as follows:
MAXL> login hypadmin Hyp3r10n on hq-loh-mst03;
OK/INFO - 1051034 - Logging in user [hypadmin@Native Directory].
OK/INFO - 1241001 - Logged in to Essbase.
MAXL> display session on database MS_DIR.MS_DIR;
user session login_time application database db_connect_time request request_time connection_source connection_ip request_state
+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------
hypadmin 630194144 42 MS_DIR MS_DIR 42 Restructure 42 hq-loh-mst03.gdit.c ::ffff:10.80.50.166 in_progress
Aauren.SecLenby 3690987494 1520 MS_DIR MS_DIR 1520 none 0 hq-loh-mst03.gdit.c ::ffff:10.80.50.166
Aauren.SecLenby 121634785 1517 MS_DIR MS_DIR 1517 none 0 hq-loh-mst03.gdit.c ::ffff:10.80.50.166
WARNING - 1241024 - Possible string truncation in column 1.
WARNING - 1241028 - Output column defined with warnings.
WARNING - 1241024 - Possible string truncation in column 9.
WARNING - 1241028 - Output column defined with warnings.
WARNING - 1241024 - Possible string truncation in column 10.
WARNING - 1241028 - Output column defined with warnings.
OK/INFO - 1241044 - Records returned: [3].
MAXL> logout;
User hypadmin is logged out
As you'll see, there are "3" sessions in my application, however I only need to worry about ones that do not have a request of "none".
Here is my function :
CheckSessions () {
for run in {1..5}
do
if [ $run != "1" ]; then
sleep 5
fi
# delete _MAXLLOGFILE to prepare for new one
[ -e ${_MAXLLOGFILE} ] && rm ${_MAXLLOGFILE}
. ${_STARTMAXL_PATH}startMaxl.sh ${_MAXLSCRIPTPATH}Display_Sessions.mxl ${_ESSB_USER} ${_ESSB_PSWD} ${_ESSB_SRVR} ${_ESSB_APP} ${_ESSB_DB} ${_MAXLLOGFILE}
awk '{for (I=1;I<=NF;I++) if ($(I+4) == "MS_DIR") print $(I+6)}' ${_MAXLLOGFILE} | grep 'none' &> /dev/null
if [ $? == 0 ]; then
echo "Attention! No active EAS sessions detected on $essb_app"
echo "Returning to main script body to proceed"
return 0
else
echo "Active EAS session(s) detected"
echo This is time number $run
echo "Will loop for $time more times"
fi
if [ $run = "5" ]; then
echo
echo "Active EAS Sessions still present"
echo "Unable to proceed with script execution"
echo
return 1
fi
done
}
This works fine if there isn't "none" in the request column. But when none exists in the request column with other non "none" strings, it will say its successful when in fact it's not.
My issue is obviously my awk statement and how I'm piping it to grep. Can someone help me modify my script to ensure the function will return 1 when a non "none" string is found EVEN IF there are none strings in the column?
Thanks!
---------- Post updated at 05:24 PM ---------- Previous update was at 03:37 PM ----------
Ive been able to achieve my goal using a TEMP file. However, I'm sure there is a neater way:
CheckSessions () {
for run in {1..5}
do
if [ $run != "1" ]; then
sleep 5
fi
[ -e ${_MAXLLOGFILE} ] && rm ${_MAXLLOGFILE}
. ${_STARTMAXL_PATH}startMaxl.sh ${_MAXLSCRIPTPATH}Display_Sessions.mxl ${_ESSB_USER} ${_ESSB_PSWD} ${_ESSB_SRVR} ${_ESSB_APP} ${_ESSB_DB} ${_MAXLLOGFILE}
TEMPFILE=$ms_logs/tempout.txt
awk '{for (I=1;I<=NF;I++) if ($(I+4) == "MS_DIR") print $(I+6)}' ${_MAXLLOGFILE} > ${TEMPFILE}
sed -i '/none/d' ${TEMPFILE}
if [[ -s ${TEMPFILE} ]] ; then
echo "" > /dev/null 2>&1
else
echo "Attention! No active EAS sessions detected on $essb_app"
echo "Returning to main script body to proceed"
rm -f ${TEMPFILE}
return 0
fi
if [ $run = "5" ]; then
echo
echo "Active EAS requests still in-progress:"
echo
cat ${TEMPFILE}
echo
echo "Unable to proceed with script execution"
echo
rm -f ${TEMPFILE}
return 1
fi
done
}