Parsing output with awk - need assistance on exception

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Parsing output with awk - need assistance on exception
# 1  
Old 09-26-2017
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:

Code:
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 :

Code:
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:

Code:
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
}

# 2  
Old 09-27-2017
See if this alternative makes sense to you:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-27-2017
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:

Code:
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:

Code:
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk assistance - Comparing 2 csv files

Hello all, I have searched high and low for a solution to this, many have come really close but not quite what I'm after. I have 2 files. One contains GUID's, for example: 8121E002-96FE-4C9C-BC5A-6AFF20DACECD 84468F30-F3B7-418B-81F0-0908E80792BF A second file, contains a path to the... (8 Replies)
Discussion started by: tirmUK
8 Replies

2. Shell Programming and Scripting

Assistance required with awk and regular expressions

Hello there, I am trying to get my head around the section below of a script we use that incorporates AWK and Regular Expressions. { match($0,"The broker*");print $1,$2,$3 ":", substr($0, RSTART,RLENGTH)} I have a basic understanding of how match works, what I am struggling with is the... (2 Replies)
Discussion started by: jimbojames
2 Replies

3. Shell Programming and Scripting

Assistance with an awk code to split files but keep the header

---------- Post updated at 11:48 AM ---------- Previous update was at 11:46 AM ---------- Hello all I have an awk code that successfully creates separate text files based on the first six letters of the second field. What it doesn't do is preserve the header into each resulting file. ... (6 Replies)
Discussion started by: colecandoo
6 Replies

4. Shell Programming and Scripting

Monitor logs for exception and if exception come then sent an email

Hi Folks, please advise , I have logs generated on unix machine at location /ops/opt/aaa/bvg.log , now sometimes there come exception in these logs also, so I want to write such a script such that it should continuously monitor these logs and whenever any exception comes that is it try to find... (3 Replies)
Discussion started by: tuntun27272727
3 Replies

5. Shell Programming and Scripting

Re: exception using AWK

I have following file: NAME=ora.DG1.svc TYPE=ora.service.type CARDINALITY_ID=1 TARGET=ONLINE STATE=ONLINE NAME=ora.orlene.DG2.svc TYPE=ora.service.type CARDINALITY_ID=1 TARGET=ONLINE STATE=OFFLINE NAME=ora.MN.acfs TYPE=ora.registry.acfs.type TARGET=ONLINE (4 Replies)
Discussion started by: rcc50886
4 Replies

6. Shell Programming and Scripting

Awk - Script assistance on identifying non matching fields

Hoping for some assistance. my source file consists of: os, ip, username win7, 123.56.78, john win7, 123.56.78, paul win7, 10.1.1.1, john win7, 10.2.2.3, joe I've been trying to run a script that will only return ip and username where the IP address is the same and the username is... (3 Replies)
Discussion started by: tekvaio
3 Replies

7. UNIX for Dummies Questions & Answers

awk o/p assistance

Hi, I would like to know the awk command that gets the below o/p: File contents: Board1;9a;60;36;60.0;60;0;0.0 Board2;96;60;35;58.3;55;0;0.0 Board3;92;60;60;100.0;60;60;100.0 Used awk script: #!/bin/awk -f BEGIN { FS = ";"; printf (" Device | ... (1 Reply)
Discussion started by: Dendany83
1 Replies

8. Shell Programming and Scripting

Awk Assistance

Hello A friend of mine posted this on another site that I follow. It is to advanced for me to figure out. If solved I will give credit where credit is due: NOTE: Does not have to be AWK. Any Language will work, Hi. I need a little assistant to write an awk script on linux that reads a file... (12 Replies)
Discussion started by: abacus
12 Replies

9. Shell Programming and Scripting

AWK statement formatting assistance

I am writing a script that ssh's out to our various servers and extracts diskspace info to generate into a report. With the mix of servers linux/solairs 8-10/AIX the easiest way is to use df -k (though I much rather prefer df -h). I have pasted the relevant code: dfdata=`ssh -q -o... (1 Reply)
Discussion started by: rkruck
1 Replies

10. Shell Programming and Scripting

Script Assistance - Outputting to file with Awk

I'm trying to take a list of domains, find out the MX resolve it to IP then find out what the NS is and output the contents to a new file. The only problem i'm having is when checking the Ip or host of the MX i can only get it to print the column with the MX record and the results of the host... (1 Reply)
Discussion started by: spartan22
1 Replies
Login or Register to Ask a Question