0

I have a log file, which has the below lines in it. I need a bash script, which takes the values in Bold (4092.1M / 20.1G) & do the math & then alert from Zabbix

[Eden: 3728.0M(3728.0M)->0.0B(3952.0M) Survivors: 368.0M->144.0M Heap: 4092.1M(48.0G)->144.0M(48.0G)]

[Eden: 3568.0M(3568.0M)->0.0B(3568.0M) Survivors: 528.0M->528.0M Heap: 20.1G(48.0G)->16.7G(48.0G)]

It should warn if the value is more than 30G and it should alert if the value is more than 35G.

Please look for MB / GB as given in the above example. I need to identify which format it is and read the value accordingly.

Can you please help me here

1
  • I'm feeling too lazy to code it myself but I'd use awk, extract that field after the Heap keyword, then replace the G by '*1000' & just remove all M's & do the calculation within awk itself. Ought to be no more than a one liner. Mar 11, 2015 at 8:15

1 Answer 1

1

You can use this as a starting point, and adapt to your liking:

#!/bin/bash

logfile="/var/log/file.log"

while read line; do
   value=`echo $line | awk -F: '{print $4}' | awk -F'(' '{print $1}' | sed 's/^.//'`

   if [ "`echo $value | grep -o ".$"`" == "M" ]; then
      value=`echo $value | awk -F'.' '{print $1}'`
      value=$((value/1000))

   elif [ "`echo $value | grep -o ".$"`" == "G" ]; then
        value=`echo $value | awk -F'.' '{print $1}'`
   fi

   if [ $value -gt 30 ] && [ $value -lt 35 ]; then
      echo "Warning: value is greater than 30G"
   elif [ $value -gt 35 ]; then
      echo "Alert: value is greater than 35G"
   fi
done < $logfile

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .