#!/bin/sh


INTERVAL=3

if [ -n "$1" ];then
	INTERVAL="$1"
fi


PROCESS_NAME="exem-ndm-agent"


HEAD=`ps -eo user,pid,pcpu,pmem,rss,sz,size,vsize,cmd | sed -n '1p'`
echo "            ${HEAD}"

PEAK=0

while :
do
	CUR_DATE=`date +%d' '%H:%M:%S`

	PROCESS_PID=`ps -ef | grep -v grep | grep -v vi | grep -v tail | grep -v ctl | grep -v svc | grep ${PROCESS_NAME} | awk -F' ' '{print $2}'`
	if [ -n "${PROCESS_PID}" ];then

		RESULT=`ps -p ${PROCESS_PID} -o user,pid,pcpu,pmem,rss,sz,size,vsize,cmd --no-heading`

		RSS=`echo ${RESULT} | awk -F' ' '{print $5}'`
		if [ ${RSS} -gt ${PEAK} ];then
			OUT_PEAK="  PEAK:${PEAK}/${RSS}"
			PEAK=${RSS}
			PEAK_STAT="peak"
		else
			OUT_PEAK="  PEAK:${PEAK}"
			PEAK_STAT=""
		fi

		echo "${CUR_DATE} ${RESULT}${OUT_PEAK}"

		if [ -n "${PEAK_STAT}" ];then
			echo "${CUR_DATE} ${RESULT}${OUT_PEAK}" >> peak.log
		fi

	else
		echo "${CUR_DATE} ${PROCESS_NAME} is not running."
		PEAK=0
	fi

	sleep ${INTERVAL}

done


