#!/bin/sh



DUMP_PIDS=`ps  --no-heading -C java -f --width 1000 |awk '{print $2}'`
if [ -z "$DUMP_PIDS" ]; then
    echo "The server $HOST_NAME is not started!"
    exit 1;
fi

DUMP_ROOT=~/dump
if [ ! -d $DUMP_ROOT ]; then
	mkdir $DUMP_ROOT
fi

DUMP_DATE=`date +%Y%m%d%H%M%S`
DUMP_DIR=$DUMP_ROOT/dump-$DUMP_DATE
if [ ! -d $DUMP_DIR ]; then
	mkdir $DUMP_DIR
fi

for PID in $DUMP_PIDS ; do
#Full thread dump ߳ռã
	$JAVA_HOME/bin/jstack $PID > $DUMP_DIR/jstack-$PID.dump 2>&1
	echo -e ".\c"
#ӡһJava̡Java coreļԶDebugJavaϢJavaϵͳԺJVMв
	$JAVA_HOME/bin/jinfo $PID > $DUMP_DIR/jinfo-$PID.dump 2>&1
	echo -e ".\c"
#jstatܹ̬ӡjvm(Java Virtual Machine Statistics Monitoring Tool)ͳϢyoung gcִеĴfull gcִеĴڴĿռСͿʹϢ	
	$JAVA_HOME/bin/jstat -gcutil $PID > $DUMP_DIR/jstat-gcutil-$PID.dump 2>&1
	echo -e ".\c"
	$JAVA_HOME/bin/jstat -gccapacity $PID > $DUMP_DIR/jstat-gccapacity-$PID.dump 2>&1
	echo -e ".\c"
#δָѡʱjmapӡӳ䡣ÿĿVMصĹʼַӳСļ·ӡ	
	$JAVA_HOME/bin/jmap $PID > $DUMP_DIR/jmap-$PID.dump 2>&1
	echo -e ".\c"
#-heapӡĸҪϢãѿռʹúͿ	
	$JAVA_HOME/bin/jmap -heap $PID > $DUMP_DIR/jmap-heap-$PID.dump 2>&1
	echo -e ".\c"
#-dumpjvmĶڴϢһļ,Ȼͨeclipse memory analyzerз
#ע⣺jmapʹõʱjvmǴڼ״̬ģֻڷ̱ʱΪ˽ʹãɷжϡ 
	$JAVA_HOME/bin/jmap -dump:format=b,file=$DUMP_DIR/jmap-dump-$PID.dump $PID 2>&1
	echo -e ".\c"
#ʾ̴򿪵ļϢ
if [ -r /usr/sbin/lsof ]; then
	/usr/sbin/lsof -p $PID > $DUMP_DIR/lsof-$PID.dump
	echo -e ".\c"
	fi
done
#Ҫռ㱨洢ϵͳϢġ
if [ -r /usr/bin/sar ]; then
/usr/bin/sar > $DUMP_DIR/sar.dump
echo -e ".\c"
fi
#Ҫռ㱨洢ϵͳϢġ
if [ -r /usr/bin/uptime ]; then
/usr/bin/uptime > $DUMP_DIR/uptime.dump
echo -e ".\c"
fi
#ڴ鿴
if [ -r /usr/bin/free ]; then
/usr/bin/free -t > $DUMP_DIR/free.dump
echo -e ".\c"
fi
#Եõڽ̡ڴ桢ڴҳIOtrapsCPUϢ
if [ -r /usr/bin/vmstat ]; then
/usr/bin/vmstat > $DUMP_DIR/vmstat.dump
echo -e ".\c"
fi
#CPUصһЩͳϢ
if [ -r /usr/bin/mpstat ]; then
/usr/bin/mpstat > $DUMP_DIR/mpstat.dump
echo -e ".\c"
fi
#IOصһЩͳϢ
if [ -r /usr/bin/iostat ]; then
/usr/bin/iostat > $DUMP_DIR/iostat.dump
echo -e ".\c"
fi
#صһЩͳϢ
if [ -r /bin/netstat ]; then
/bin/netstat > $DUMP_DIR/netstat.dump
echo -e ".\c"
fi
echo "OK!"