I am performing a grep for word "percent" in log

strings /var/lib/activemq/data/gc.log |grep percent | grep ten 

which gives me an output

<mem type="tenure" free="2188996168" total="2617245696" percent="83" />

out of which i need to check the last instances and report

CRITICAL is between 10 and 0
WARNING is between 20 and 10
OK status if the usage is over 20

for now I did strings /var/lib/activemq/data/gc.log |grep percent | grep ten |tail -5 | awk {'print $5'}

and the current output is , I understand this number would vary


how do I only consider the last 3 values to return an OK, CRITICAL or Warning staus?

  • Why in the world do you use strings to output the plain-text file? Use cat instead. However in your case you don't need even cat. – rush Jul 17 at 11:13
  • Is gc.log an XML file? XML is better parsed with an XML parser such as xmlstarlet. – Kusalananda Jul 18 at 21:01
  • @Kusalananda the gc.log is not in XML format – Karthik Jul 19 at 6:42
  • Since you are clearly trying to parse XML, maybe the relevant XML-formatted sections could be extracted, and then parsed properly? – Kusalananda Jul 19 at 7:02
  • @Kusalananda It is NOT an XML file :) – Karthik Jul 19 at 8:28

We can do this with awk.

awk -F '"' '/percent/ && /ten/ {if( $(NF-1) > 20 ) {print "OK" } else if ( $(NF-1) < 20 ) {print "WARNING" } else if ( $(NF-1) < 10 ) { print "CRITICAL" }}' /var/lib/activemq/data/gc.log
  • -F '"' double quotes as field separator
  • /percent/ && /ten/ grep lines which has the patterns.
  • ( $(NF-1) > 20 ) compare the second last field and print the requires string
  • that depends on percent="83" being the last argument, which in XML I would not want to depend on ... – pLumo Jul 17 at 11:14
  • @pLumo agreed... but OP has $5 in its command so it should be having fixed fields... – msp9011 Jul 17 at 11:20
  • @msp9011, this is returning OK for all the times it find the term percent in the file. I just need to look at the last 3 instances and report an alert based on the threshold i mentioned. – Karthik Jul 18 at 8:22

You can use awk only:

awk '
/ten/ && /percent/ {
        if (m[1] < 10) print "CRITICAL"
        else if (m[1] < 20) print "WARNING"
        else print "OK"
}' file  | tail -n3

But for XML data it is better to use an xml parser:

    xmlstarlet select -t  -m '//mem[@type="tenure"]' -v '@percent' -n < file \
    | tail -n3

for i in $last_percents; do
    if [ $i -lt 10 ]; then echo "CRITICAL"
    elif [ $i -lt 20 ]; then echo "WARNING"
    else echo "OK"
  • the file is not in XML format.. But he first awk does what I am looking for. is there a way to display the percentage values as well. Currently it outputs only OK 3 times . – Karthik Jul 18 at 8:20
  • sure,just add ,m[1] after "OK" in the print statement – pLumo Jul 18 at 8:42
  • that worked thank you. Iam trying to use this script to setup this in a script that would trigger an alert being Ok, Critial or Warning. exit code 0 for ok and 1 for warning and 2 for critical – Karthik Jul 18 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.