help needed - log file monitoring script

hi Gurus,
Need to pick your brains on this minor script project.
I would like to continuously monitor a log file with sample log messages as below, and if PSOldGen percentage is either 99% or 100% for consecutively 10 times, alert someone.

{Heap before gc invocations=46516:
 PSYoungGen      total 63616K, used 54426K [0xcdc00000, 0xd2c00000, 0xf8800000)
  eden space 48064K, 100% used [0xcdc00000,0xd0af0000,0xd0af0000)
  from space 15552K, 40% used [0xd0d20000,0xd1356b38,0xd1c50000)
  to   space 15040K, 0% used [0xd1d50000,0xd1d50000,0xd2c00000)
 PSOldGen        total 892928K, used 869520K [0x78400000, 0xaec00000, 0xcdc00000)
  object space 892928K, 97% used [0x78400000,0xad5243f8,0xaec00000)
 PSPermGen       total 786432K, used 185250K [0x48400000, 0x78400000, 0x78400000)
  object space 786432K, 23% used [0x48400000,0x538e8b10,0x78400000)
1016119.682: [GC [PSYoungGen: 54426K->6458K(58752K)] 923947K->875979K(951680K), 0.0316028 secs]
Heap after gc invocations=46516:
 PSYoungGen      total 58752K, used 6458K [0xcdc00000, 0xd2800000, 0xf8800000)
  eden space 47808K, 0% used [0xcdc00000,0xcdc00000,0xd0ab0000)
  from space 10944K, 59% used [0xd1d50000,0xd239eb68,0xd2800000)
  to   space 14592K, 0% used [0xd0b80000,0xd0b80000,0xd19c0000)
 PSOldGen        total 892928K, used 869520K [0x78400000, 0xaec00000, 0xcdc00000)
  object space 892928K, 97% used [0x78400000,0xad5243f8,0xaec00000)
 PSPermGen       total 786432K, used 185250K [0x48400000, 0x78400000, 0x78400000)
  object space 786432K, 23% used [0x48400000,0x538e8b10,0x78400000)
}

In the sample above, the percentage I'm interested in is that 97% immediately after the PSOldGen line. I can't grep for "object space" because that would give me PSPermGen, the 23% line as well. I don't care about that.
How can I read a line with the exact text "PSOldGen" and get that percentage on the very next line?
Would I be able to accomplish my objectives in one script? Or should I direct the percentage output to some other file and create another script that monitors that file?
If the script can be done purely in shell (ksh or bash), that'd be great, but other implementations like in python or perl are also cool.
Any pointers would be much appreciated. Thanks in advance.

check the below code is working or not...

grep -A 1 "PSOldGen" filename 

if the above code is giving the output (with the next line ).. then we can easily count the no. of occurance which has the value more than 98

1 Like

How about this?

awk '/^PSOldGen/{getline;match($0,/[0-9]*\%/);if(int(substr($0,RSTART,RLENGTH-1)) > 98 ){print $0}}' filename
1 Like

This will print only the numbers you are expected to see.
As per your input it should print 97 twice.
From there its fairly easy to do the calculation yourself.

 
perl -0lne 'BEGIN{$,="\n"}@arr=(/PSOldGen.+\nobject space .+(\d{2,3})%.+\n/g); print @arr' inputFile
1 Like

Thank you all!

nawk '/PSOldGen/ {getline;{gsub(/%/,"");print $4}}' inputfile
97
97
awk '/PSOldGen/ { getline;if (int($4)>=99) {i++} else {i=0}
                  if (i>=10) {print "Something wrong, be careful";i=0 }
                 }' infile