#!/bin/sh
#
# Shell script to kill off memtst (in the morning from cron)
#
# Typical crontab entries to start at 10:00 PM & stop at 6:00 AM (64MB):
# 00 22 * * * /usr/local/src/memtst/memtst-start 64
# 00 06 * * * /usr/local/src/memtst/memtst-stop person@host.subdom.domain
#
PATH=:/usr/local/bin:/usr/ucb:/usr/bin:/usr/etc:
#
# Parameter 1 is the email address to send the report to,
# by default send to "root".

DEFMAILTO=root
MAILTO=${1-$DEFMAILTO}

#
# Where the error log files are on a SUN. Use * to look through
# all of them they are shuffled on SAT AM on SUN-OS 4.1.1.
#

MESSAGES=/var/adm/messages*

#
# The following have to jibe with the ones in memtst-start.
# PIDFILE should be left in /tmp so that the code following
# properly detects a reboot (/tmp is cleared on reboot). OUTFILE
# is the output from memtst and should be kept on a real disk partition
# which survives a reboot (/usr/tmp).
#

PIDFILE=/tmp/memtst.pid
OUTFILE=/usr/tmp/memtst.out

# Start of program

HOST=`hostname`
TMPFILE=/tmp/memtst$$

if [ ! -f $PIDFILE ]
then
  echo "memtst is not running, $PIDFILE does not exist." >> $TMPFILE
  echo "The system may have crashed and rebooted." >> $TMPFILE
  echo "The error log file(s) \"$MESSAGES\" will be searched for parity errors." >> $TMPFILE
  echo "" >> $TMPFILE
else
  kill -9 `cat $PIDFILE`
  rm $PIDFILE
fi

fgrep -i parity $MESSAGES >> $TMPFILE
if [ -s $TMPFILE ]
then
  echo "" >> $TMPFILE
  echo "----------------------memtst output follows---------------------------" >> $TMPFILE
  echo "" >> $TMPFILE
  cat $OUTFILE >> $TMPFILE
  Mail -s "Memory failure on $HOST" $MAILTO < $TMPFILE
else
  echo "" >> $TMPFILE
  echo "No errors to report, here is the memtst output:" >> $TMPFILE
  echo "" >> $TMPFILE
  cat $OUTFILE >> $TMPFILE
  Mail -s "No Memory Parity errors found on $HOST" $MAILTO < $TMPFILE
fi
rm $TMPFILE 
