Saturday 28 May 2016

Generating Thread dump and Heap dumps in JBoss through shell script

You can generate Thread Dump  & Heap dump manually in Jboss using the following shell scripts. 

 Download the following script and store it in a location such as /usr/local/bin.
 Provide the file permissions and ownership for this script as you see fit. In this example, they are:

Generating Thread dump

#==============================================================
#
#               Script Name :   thread_dump.sh
#               Developed by:   Bharathkumar
#
#==============================================================
#!/bin/bash

JBOSS_HOME="/u02/local/opt/jboss-eap-6.2/mydomain/slave02"
JAVA_BIN="/usr/java/default/bin"
echo -e "\nPlease enter the \"EAP name\" !!!"
                read EAP
  LOG_DIR="$JBOSS_HOME/servers/$EAP/log"
  #Get the EAP process ID
  EAP_PID=`ps -ef | grep $EAP | grep -v grep | awk '{print $2}'`
 #generating four thread dump files with 30 seconds interval
for i in 1 2 3 4; do
    $JAVA_BIN/jstack -l $EAP_PID  >> $LOG_DIR/threaddump.log
#Move the dump and keep it with creation dates to recognize the time when the dump taken
    mv $LOG_DIR/threaddump.log  $LOG_DIR/threaddump.`date +%m%d%Y_%H%M`.log
   sleep 30
 done
echo -e "************************************************"
echo -e "please check thread dump files under " $LOG_DIR
echo -e "************************************************"
echo -e "\nThank you!!! Bye!!!\n"
    exit 0
Let see how this script will work

while script executing time this script will ask EAP name. once you enter EAP name.. it will be generate 4 dump files with 30 seconds interval.

[jbossas@middleware shellscripts]$ ./thread_dump.sh

Please enter the "EAP name" !!!
rpa1-AS2
************************************************
please check thread dump files under  /u02/local/opt/jboss-eap-6.2/mydomain/slave02/servers/rpa1-AS2/log
************************************************

Thank you!!! Bye!!!

[jbossas@middleware shellscripts]$ ls -ltr /u02/local/opt/jboss-eap-6.2/mydomain/slave02/servers/rpa1-AS2/log/thread*
-rw-rw-r-- 1 jbossas jbossas 59061 May 28 00:05 /u02/local/opt/jboss-eap-6.2/mydomain/slave02/servers/rpa1-AS2/log/threaddump.05282016_0005.log
-rw-rw-r-- 1 jbossas jbossas 59049 May 28 00:13 /u02/local/opt/jboss-eap-6.2/mydomain/slave02/servers/rpa1-AS2/log/threaddump.05282016_0013.log
-rw-rw-r-- 1 jbossas jbossas 58066 May 28 00:14 /u02/local/opt/jboss-eap-6.2/mydomain/slave02/servers/rpa1-AS2/log/threaddump.05282016_0014.log
-rw-rw-r-- 1 jbossas jbossas 59049 May 28 00:15 /u02/local/opt/jboss-eap-6.2/mydomain/slave02/servers/rpa1-AS2/log/threaddump.05282016_0015.log
[jbossas@middleware shellscripts]$ 

Generating Heap dump


#==============================================================
#
#               Script Name :  heap_dump.sh
#               Developed by:   Bharathkumar
#
#==============================================================
#!/bin/bash

JBOSS_HOME="/u02/local/opt/jboss-eap-6.2/mydomain/slave02"
JAVA_BIN="/usr/java/default/bin"
echo -e "\nPlease enter the \"EAP name\" !!!"
                read EAP
  LOG_DIR="$JBOSS_HOME/servers/$EAP/log"
  #Script output log directory
  SCRIPT_LOG="$LOG_DIR/heap_dumps.log"
  #Get the EAP process ID
  EAP_PID=`ps -ef | grep $EAP | grep -v grep | awk '{print $2}'`

#generating three heap dump files with 30 seconds interval
for i in 1 2 3 ; do
                echo "`date +%x" "%X` Starting Heap Dump $DUMP_COUNT" >>$SCRIPT_LOG
                $JAVA_BIN/jmap -dump:format=b,file=$LOG_DIR/heapdump.hprof $EAP_PID >>$SCRIPT_LOG
#Move the heap dump and keep it with creation dates to recognize the time when the dump taken
                mv $LOG_DIR/heapdump.hprof  $LOG_DIR/heap_$EAP.`date +%m%d%Y_%H%M`.hprof
                echo "`date +%x" "%X` Completed Heap Dump $DUMP_COUNT" >>$SCRIPT_LOG
                sleep 30
 done
echo -e "************************************************"
echo -e "please check heap dump files under " $LOG_DIR
echo -e "************************************************"
echo -e "\nThank you!!! Bye!!!\n"
    exit 0
Let see how this script will work

while script executing time this script will ask EAP name. once you enter EAP name.. it will be generate 3 dump files with 30 seconds interval.

[jbossas@middleware shellscripts]$ ./heap_dump.sh 

Please enter the "EAP name" !!!
rpa1-AS2
************************************************
please check heap dump files under  /u02/local/opt/jboss-eap-6.2/mydomain/slave02/servers/rpa1-AS2/log
************************************************

Thank you!!! Bye!!!

[jbossas@middleware shellscripts]$ ls -ltr /u02/local/opt/jboss-eap-6.2/mydomain/slave02/servers/rpa1-AS2/log/heap*
-rw------- 1 jbossas jbossas 123447340 May 28 00:26 /u02/local/opt/jboss-eap-6.2/mydomain/slave02/servers/rpa1-AS2/log/heap_rpa1-AS2.05282016_0026.hprof
-rw------- 1 jbossas jbossas 123723872 May 28 00:27 /u02/local/opt/jboss-eap-6.2/mydomain/slave02/servers/rpa1-AS2/log/heap_rpa1-AS2.05282016_0027.hprof
-rw------- 1 jbossas jbossas 124270421 May 28 00:28 /u02/local/opt/jboss-eap-6.2/mydomain/slave02/servers/rpa1-AS2/log/heap_rpa1-AS2.05282016_0028.hprof
-rw-rw-r-- 1 jbossas jbossas       848 May 28 00:28 /u02/local/opt/jboss-eap-6.2/mydomain/slave02/servers/rpa1-AS2/log/heap_dumps.log


I hope this information is helpful to you......

 Please register to this blog ...to get alerts when I post new articles or if you have need help in Middleware tasks 

No comments: