#!/bin/sh

version=1.01

trap cleanup INT TERM

cleanup()
{
    exit 0
}

helpOutput ()
{
    echo "ibm-psscript.sh (version:" $version ")"
    echo "Run as follows:"
    echo "ibm-psscript.sh -f <filename> -i <interval>"
    echo ""
    echo "Where <filename> is the output filename file"
    echo "<interval> is the time in seconds between each iteration"
    echo "eg:"
    echo "ibm-psscript.sh -f leak_filename.txt -i 120"
    echo ""
    echo "This will monitor all processes at 2 min intervals putting the output"
    echo "into leak_filename.txt"
}

while getopts f:i:h c
do
	case $c in
		f)	if [ $OPTARG != "?" ] ; then
				filename=$OPTARG
			else
				echo "no output file given"
				
                exit 1
			fi;;
    i) if [ $OPTARG != "?" ] ; then
				interval=$OPTARG
			else
				echo "no interval given"
                exit 1
			fi;;
		h | ? | \?)	helpOutput
			exit 0;;
	esac
done
shift `expr $OPTIND - 1`


if [ -z "$interval" ]; then
    interval="1800"
fi

if [ -z "$filename" ]; then
    filename="ibm-psscript.log"
fi

echo "Printing output to $filename every $interval seconds."

echo "ibm-psscript.sh (version:" $version ")" >>$filename

while true
do
  echo `date` >>$filename

  echo "*** vmstat start" >>$filename
  vmstat 1 3 >>$filename
  echo "*** vmstat end" >>$filename

  echo "*** ps auxwww start" >>$filename
  ps auxwww >>$filename
  echo "*** ps auxwww stop" >>$filename

  echo " --------------------" >>$filename
  sleep $interval

done
