#!/bin/bash
# Script runs for approximately 2 minutes.
# The parameter is the ID of the process to be probed
#
# You may expand this script with addtional system commands

# Assign parameter value to PID variable
PID=${1}

if [ "$PID" = "" ]; then
	echo "Please specify a process ID"
	exit 127
fi

# The directory to hold all the output files
OUTPUTDIR=$TOP/logs/diagnostic/proc_info
mkdir -p $OUTPUTDIR

# source the common.functions
. $TOP/bin/diagnostic/common/common.functions

# put timestamp and version info in README.txt file
getReportHeader > $OUTPUTDIR/README.txt

# Stat commands which run in the background over the 2 minute period.
# Ensure each command exists before calling to it
#
[ "X`which netstat 2> /dev/null`" == "X" ] || [ `netstat -an > $OUTPUTDIR/netstat.out &` ]

[ "X`which vmstat 2> /dev/null`" == "X" ] || [ `vmstat 5 24 > $OUTPUTDIR/vmstat.out &` ]

# prstat -mvcLn 40 -p $PID 5 50 > $OUTPUTDIR/prstat.out &

[ "X`which mpstat 2> /dev/null`" == "X" ] || [ `mpstat 5 24 > $OUTPUTDIR/mpstat.out &` ]

# lsof and proc tools executed once only, at the start of the 2 minutes.
[ "X`which lsof 2> /dev/null`" == "X" ] || [ `lsof -p $PID > $OUTPUTDIR/lsof.out` ]

[ "X`which pfiles 2> /dev/null`" == "X" ] || [ `pfiles $PID > $OUTPUTDIR/pfiles.out` ]

[ "X`which pmap 2> /dev/null`" == "X" ] || [ `pmap $PID > $OUTPUTDIR/pmap.out` ]

[ "X`which pldd 2> /dev/null`" == "X" ] || [ `pldd $PID > $OUTPUTDIR/pldd.out` ]

# Java thread dumps pstacks are taken over the course of the 2 minutes.
# May need to increase the resolution of this section (more pstacks, less
# time delay between them) if there are spikes in prstat output. 
[ "X`which pstack 2> /dev/null`" == "X" ] || [ `pstack $PID > $OUTPUTDIR/pstack1.out` ]
kill -3 $PID 
sleep 60

[ "X`which pstack 2> /dev/null`" == "X" ] || [ `pstack $PID > $OUTPUTDIR/pstack2.out` ]
kill -3 $PID 
sleep 60

[ "X`which pstack 2> /dev/null`" == "X" ] || [ `pstack $PID > $OUTPUTDIR/pstack3.out` ]
kill -3 $PID

exit 0
