#!/bin/sh

# Copyright 1999, 2000, 2001 (c) Thomas Erskine
# See the COPYRIGHT file with the distribution.

# remstats-monitor - watch what is happening while remstats is running
#	This is only intended for development monitoring, i.e.
#	rough profiling.  If you find some other use for it, please
# 	let me know.  <thomas.erskine@sourceworks.com>
# $Id: remstats-monitor.sh,v 1.2 2001/08/28 15:22:24 remstats Exp $

# - - -   Configuration   - - -

# This is *NOT* portable.  It assumes the standard location of files
# and a SVR4 style ps and a /bin/date which accepts %Y to mean
# a 4-digit year.

# How long to sleep between commands
sleeptime=5
pscols='pid,ppid,user,s,rss,time,args'
if [ "`basename $0`" = 'monitor' ] ; then
	suffix='';
else
	suffix=`basename $0`
	suffix=`echo $suffix | sed -e 's/^monitor//'`
fi
LOCKFILE=/var/lib/remstats/tmp/LOCK-run-remstats$suffix
STATUSFILE=/var/lib/remstats/tmp/STATUS-run-remstats$suffix
dateprog=/bin/date
datefmt='+%Y-%m-%d %H:%M:%S'

# - - -   Version History   - - -

# $Revision: 1.2 $

# - - -   Setup   - - -

if [ $# = 1 ] ; then
	sleeptime=$1
fi

# - - -  Mainline   - - -

while [ : ] ; do
	if [ -f $LOCKFILE ] ; then
		running="(`cat $LOCKFILE` running)"
	else
		running=''
	fi
	echo "=== `$dateprog \"$datefmt\"` ${running}$status ==="
	if [ -f $STATUSFILE ] ; then
		status=" `cat $STATUSFILE`"
	else
		status=''
	fi
	ps -e -o $pscols | \
		egrep 'updater|-collector|graph-writer|-monitor|datapage-interfaces|datapage-inventory|run-remstats' | \
		grep -v grep |\
		sed -e 's#/usr/local/bin/perl -w ##' -e 's#sh -c PATH=[^ ]* ##' -e 's#/var/remstats/bin/##g' -e 's#/var/remstats/tmp/##g'
	sleep $sleeptime
done
