Shell script to monitor heap / Open JDK

#!/bin/sh

for gc_log in `find /log/location/* -name gc.log 2>/dev/null`
do
server_name=`echo $gc_log| awk -F/ ‘{print $2}’`
maxHeapSize=`ps -aefww|grep tomcat|grep $server_name|grep -o “\-Xmx[0-9]*”|grep -o “[0-9]*”`
if [ -z $maxHeapSize ]
then
echo “check inside the startup.xml”
maxHeapSize=`grep “<option bits=\”64\” name=\”\-Xmx[0-9]*” /${server_name}/conf/startup.xml|grep -o “\-Xmx6144m”|grep -o “[0-9]*”`
fi

maxHeapSizeinBytes=$(echo $(( maxHeapSize * 1024)))
temp_file_1=”/tmp/${server_name}.out.txt”
tac $gc_log | grep -m1 -B 20 “Heap before GC invocations” | egrep “Heap|PSYoungGen |ParOldGen |PSPermGen “|tac >$temp_file_1

PSYGTotalBefore=`cat $temp_file_1 |grep -o “PSYoungGen.*total [0-9]*”|grep -o [0-9]*|head -1`
PSYGTotalAfter=`cat $temp_file_1 |grep -o “PSYoungGen.*total [0-9]*”|grep -o [0-9]*|tail -1`
PSOGTotalBefore=`cat $temp_file_1 |grep -o “ParOldGen.*total [0-9]*”|grep -o [0-9]*|head -1`
PSOGTotalAfter=`cat $temp_file_1 |grep -o “ParOldGen.*total [0-9]*”|grep -o [0-9]*|tail -1`
PSPGTotalBefore=`cat $temp_file_1 |grep -o “PSPermGen.*total [0-9]*”|grep -o [0-9]*|head -1`
PSPGTotalAfter=`cat $temp_file_1 |grep -o “PSPermGen.*total [0-9]*”|grep -o [0-9]*|tail -1`
PSYGUsedBefore=`cat $temp_file_1 |grep -o “PSYoungGen.*used [0-9]*”|grep -o “used [0-9]*”|grep -o “[0-9]*”|head -1`
PSYGUsedAfter=`cat $temp_file_1 |grep -o “PSYoungGen.*used [0-9]*”|grep -o “used [0-9]*”|grep -o “[0-9]*”|tail -1`
PSOGUsedBefore=`cat $temp_file_1 |grep -o “ParOldGen.*used [0-9]*”|grep -o “used [0-9]*”|grep -o “[0-9]*”|head -1`
PSOGUsedAfter=`cat $temp_file_1 |grep -o “ParOldGen.*used [0-9]*”|grep -o “used [0-9]*”|grep -o “[0-9]*”|tail -1`
PSPGUsedBefore=`cat $temp_file_1 |grep -o “PSPermGen.*used [0-9]*”|grep -o “used [0-9]*”|grep -o “[0-9]*”|head -1`
PSPGUsedAfter=`cat $temp_file_1 |grep -o “PSPermGen.*used [0-9]*”|grep -o “used [0-9]*”|grep -o “[0-9]*”|tail -1`
BeforeGCTotal=$(echo $(( PSYGTotalBefore + PSOGTotalBefore + PSPGTotalBefore)))
BeforeGCUsed=$(echo $(( PSYGUsedBefore + PSOGUsedBefore + PSPGUsedBefore)))
AfterGCTotal=$(echo $(( PSYGTotalAfter + PSOGTotalAfter + PSPGTotalAfter)))
AfterGCUsed=$(echo $(( PSYGUsedAfter + PSOGUsedAfter + PSPGUsedAfter)))

#PERCENTusedbefore=$(echo $(( 100 * BeforeGCUsed / BeforeGCTotal)))
#PERCENTusedafter=$(echo $(( 100 * AfterGCUsed / AfterGCTotal)))
PERCENTusedbefore=$(echo $(( 100 * BeforeGCUsed / maxHeapSizeinBytes )))
PERCENTusedafter=$(echo $(( 100 * AfterGCUsed / maxHeapSizeinBytes )))
echo “% Used Before: $PERCENTusedbefore”
echo “% Used After: $PERCENTusedafter”
rm $temp_file_1
done

Advertisements