Parsing output with awk - need assistance on exception

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
}

See if this alternative makes sense to you:

CheckSessions() {
	for run in {1..5}
	do
		[ $run -gt 1 ] && sleep 5

		[ -e "$_MAXLLOGFILE" ] && rm "$_MAXLLOGFILE"

		. "$_STARTMAXL_PATH"startMaxl.sh \
		    "$_MAXLSCRIPTPATH"Display_Sessions.mxl "$_ESSB_USER" \
		    "$_ESSB_PSWD" "$_ESSB_SRVR" "$_ESSB_APP" "$_ESSB_DB" \
		    "$_MAXLLOGFILE"

		if awk '
			{for(i = 5; i <= NF - 2; i++)
				if($i == "MS_DIR" && $(i + 2) != "none") {
					rc = 1
					print $(i + 2)
				}
			}
			END {	exit rc
			}' "$_MAXLLOGFILE" > "$TEMPFILE"
		then
			echo "Attention! No active EAS sessions detected on $essb_app"
			echo "Returning to main script body to proceed"
			rm "$TEMPFILE"
			return 0
		fi
	done
	
	echo
	echo "Active EAS requests still in-progress:"
	echo
	cat "$TEMPFILE"
	echo
	echo "Unable to proceed with script execution"
	echo
	rm "$TEMPFILE"
	return 1
}

I think it does the same thing your script was doing without needing to run grep or sed and the awk script might run a tiny bit faster.

Note that if you are unable to proceed with script execution (as noted in your echo statements), you might want to change the return 1 shown in red above to exit 1 . You haven't shown us whether or not your script does any further processing if this function fails nor if it checks the return code from this function so I have no idea whether it would be appropriate to change the return to an exit in your actual script.

1 Like

Wow - thank you, Don! This is working like a charm!

I do have one additional piece I want to add. On the "3rd" loop, I need to put a piece of email functionality in there.

Something like this:

if [ $run = "3" ]; then
    #echo "Active EAS session(s) still in progress on $essb_app - session(s) have been active for 30 minutes.  Script will continue to loop for 2 more times." | mail -s "MS_DIR Refresh Unable to Run" joesmith@email.com
    echo "Active EAS session(s) still in progress on $essb_app - session(s) have been active for 30 minutes.  Script will continue to loop for 2 more times."
    echo    
fi

I'm not sure how to incorporate it though - is this possible?

Thanks you!

---------- Post updated at 03:44 AM ---------- Previous update was at 03:32 AM ----------

Hi Don -

I've added it as follows and it's working as expected:

TEMPFILE=$ms_logs/tempout.txt

CheckSessions() {
    for run in {1..5}
    do
        [ $run -gt 1 ] && sleep 5
        
        [ -e "$_MAXLLOGFILE" ] && rm "$_MAXLLOGFILE"

        . "$_STARTMAXL_PATH"startMaxl.sh \
            "$_MAXLSCRIPTPATH"Display_Sessions.mxl "$_ESSB_USER" \
            "$_ESSB_PSWD" "$_ESSB_SRVR" "$_ESSB_APP" "$_ESSB_DB" \
            "$_MAXLLOGFILE"

        if awk '
            {for(i = 5; i <= NF - 2; i++)
                if($i == "MS_DIR" && $(i + 2) != "none") {
                    rc = 1
                    print $(i + 2)
                }
            }
            END {    exit rc
            }' "$_MAXLLOGFILE" > "$TEMPFILE"
        then
            echo "Attention! No active EAS sessions detected on $essb_app"
            echo "Returning to main script body to proceed"
            rm "$TEMPFILE"
            return 0
        fi
        
    if [ $run = "3" ]; then
    echo $run
    #echo "Active EAS session(s) still in progress on $essb_app - session(s) have been active for 30 minutes.  Script will continue to loop for 2 more times." | mail -s "MS_DIR Refresh Unable to Run" joesmith@email.com
    echo "Active EAS session(s) still in progress on $essb_app - session(s) have been active for 30 minutes.  Script will continue to loop for 2 more times."
    echo    
    fi
    
    done
    
    echo $run
    echo "Active EAS requests still in-progress:"
    echo
    cat "$TEMPFILE"
    echo
    echo "Unable to proceed with script execution"
    echo
    rm "$TEMPFILE"
    return 1
}

#::-- Begin Script Processing --::#

echo ---------------------------------------------------------
echo "${_SN} beginning at ${_TIME}"                           
echo ---------------------------------------------------------

echo ---------------------------------------------------------                                                                                                
echo "Execute Function to determine Essbase Active Sessions"                                         
echo ---------------------------------------------------------

CheckSessions

_RVAL=$?

if [ $_RVAL -eq 0 ]
then
    echo ---------------------------------------------------------
    echo "Function Result : Successful"                           
    echo ---------------------------------------------------------
  
else
    echo ---------------------------------------------------------
    echo "Function Result : Unsuccessful"                      
    echo ---------------------------------------------------------
    # put email piece here
    exit 1
fi

Please let me know if that is incorrect - thank you!