Saturday, September 6, 2014

How to take Thread dump, Heap dump, GC logs for any running weblogic instnace

#!/bin/bash

# These variables must be modified, with the enviornment for the Weblogic app servers
JAVA_HOME=/u01/app/oracle/product/fmw/java/jrockit-jdk1.6.0_33-R28.2.4-4.1.0
OUTPUT_DIR=/tmp/DiagnosticData/
GC_LOG=`ps  -ef | grep $JAVA_HOME/bin/java | grep -v grep  | grep oracle | sed -n -e 's/^.*-Xloggc://p' | awk '{print $1}'`

# Function countArgs
# This is just a function that returns an int of the number of items I passed it
function countArgs {
        echo $#
}

FOUND=false
 while [ $FOUND != "true" ] ; do
     echo "EXISTING JVM(s)"
     EXSJVMS=`ps -ef | grep java | grep -v grep | grep -v NodeManager | grep -v AdminServer | grep -v "gcagent.tmmain.TMMain" | sed -n -e 's/^.*-Dweblogic.Name=//p' | awk '{print $1}'`
     echo "  "$EXSJVMS
     printf " Please enter the JVMs to generate javacores separated by a space: "
     read JVMS
     JVMS=${JVMS:-"FoundSome"}
     if [ "$JVMS" != "FoundSome" ] ; then
FOUND=true
     fi
 done
 JVM_COUNT=`countArgs $JVMS`
 echo


#split JVM names into an array
IFS=' ' read -a JVM_NAMES <<< "${JVMS}"

#create directories based on JVM name
for (( i=0; i < ${JVM_COUNT}; i++));
do
    mkdir -p $OUTPUT_DIR${JVM_NAMES[$i]}
done


#Collect data for each JVM selected

for (( loop=0; loop < ${JVM_COUNT}; loop++));
do

    OUT_DIR=$OUTPUT_DIR${JVM_NAMES[$loop]}

    #These two variables affect the amount of time that this script runs.
    JAVACORE_COUNT=3
    SLEEP_TIME=30

    #Get pid of JVM processes that were inputed
    JVM_PID=`ps -ef | grep "\-Dweblogic\.Name=${JVM_NAMES[$loop]}" | awk '{print $2}'`

    echo Get Heap Dump.
    $JAVA_HOME/bin/jrcmd $JVM_PID heap_diagnostics > $OUT_DIR/heapDiag_$JVM_PID.log

    for ITER in `seq $JAVACORE_COUNT` ;  do
       echo Get Java Stack...Sleep $SLEEP_TIME seconds
       $JAVA_HOME/bin/jrcmd $JVM_PID print_threads > $OUT_DIR/threadDump_$JVM_PID_$ITER.log
       if [ $ITER -ne $JAVACORE_COUNT ]; then
         sleep $SLEEP_TIME
       fi
    done

    # Perform the next three commands concurrently.
    echo  Get vmstat, top for JVM, and top for os...Sleep 35 seconds.
    top -H -b -p $JVM_PID -d 5  -n 7 > $OUT_DIR/top_jvmthreads.out &
    top -n 4 -b -d 10 > $OUT_DIR/top_proc.out &
    vmstat 1 35  > $OUT_DIR/vmstat.out

done


echo Get netstat
netstat -an > $OUTPUT_DIR/netstat.out


#Need to enable GC logging first before we can move this file.
#echo Get gc log data
#cp $GC_LOG $OUT_DIR/gc_log.out

No comments:

Post a Comment