#!/bin/sh
#
# Sample - sample CPU and memory in a simplistic way
#
# This can be scheduled from a crontab.
# A better solution on Linux is to use the linuxsampler.
#

if [ `uname -s` = SunOS ] ; then
	AWK=nawk
else
	AWK=awk
fi

d=`dirname $0`/..
mkdir -p $d/logs
f=$d/logs/Sample.log
date '+%Y %m %d %H %M %S' >> $f
ps -e -o user,pcpu,pmem,args |\
grep -v USER |\
$AWK ' \
	{ \
	pcpu[$1] += $2 ; \
	pmem[$1] += $3 ; \
	} \
END \
	{ \
	for ( i in pcpu ) \
		print "\t", i, pcpu[i], pmem[i] \
	} \
	' >> $f

if [ `wc -l < $f` -gt 60000 ] ; then
	tail -50000 $f > $f.trunc
	mv $f.trunc $f
fi

exit 0
