#! /bin/sh
# PCP QA Test No. 314
# Exercise pmie_daily functionality - log rotation
#
# Copyright (c) 2007 Aconex.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard filters
. ./common.product
. ./common.filter
. ./common.check

_cleanup()
{
    cd $here
    if $was_running
    then
	_restore_auto_restart pmie
	_service pmie start >>$here/$seq.full 2>&1
    else
	_service pmie stop >>$here/$seq.full 2>&1
	$sudo $PCP_BINADM_DIR/pmsignal -a -s TERM pmie >>$here/$seq.full 2>&1
	_wait_pmie_end
    fi
    $sudo rm -fr $tmp.*
    $sudo rm -fr /tmp/$seq;
}

# wait for a file to appear ...
#
_wait_for()
{
    _i=0
    while [ ! -f "$1" ]
    do
	_i=`expr $_i + 1`
	if [ "$_i" -ge 100 ]
	then
	    echo "_wait_for: failed to see file $1 after 100 iterations"
	    return
	fi
	pmsleep 0.1
    done
}

signal=$PCP_BINADM_DIR/pmsignal
status=1	# failure is the default!
trap "_cleanup;
exit \$status" 0 1 2 3 15

was_running=false
[ -f $PCP_RUN_DIR/pmie.pid ] && was_running=true

if $was_running
then
    _stop_auto_restart pmie
fi

# create a pmie config file, causing frequent output (to log)
cat > $tmp.config << EOF1
delta = 0.2 seconds;
fetched = simple.numfetch;
EOF1

echo "=== pmie config ===" >$seq.full
cat $tmp.config >>$seq.full

# create pmie control files and test out various good/bad conditions

cat > $tmp.control << EOF2
\$version=1.0
LOCALHOSTNAME n /tmp/$seq/1.good.log -v -c $tmp.config
EOF2

echo "=== pmie control ===" >>$seq.full
cat $tmp.control >>$seq.full

# real QA test starts here
_service pmie stop >>$seq.full
_wait_pmie_end
$sudo $signal -a -s TERM pmie 2>/dev/null
$sudo rm -fr /tmp/$seq && mkdir /tmp/$seq || exit 1
id pcp >/dev/null 2>&1 && $sudo chown -R pcp:pcp /tmp/$seq
pmstore simple.numfetch 0 >/dev/null

# fire em all up
echo "Starting pmie process"
echo "=== pmie_check ===" >>$seq.full
$sudo $PCP_BINADM_DIR/pmie_check -c $tmp.control -VV -l $tmp.log
$sudo cat $tmp.log >>$seq.full
_wait_for /tmp/$seq/1.good.log
ps $PCP_PS_ALL_FLAGS | grep '[p]mie' >>$seq.full
sleep 6		# fill original log a bit
cat /tmp/$seq/1.good.log >>$seq.full
ps $PCP_PS_ALL_FLAGS | grep '[p]mie' >>$seq.full

echo "Rotate, rotate..."
previous=`pmdate -1d %Y%m%d`
echo "=== pmie_daily ===" >>$seq.full
$sudo $PCP_BINADM_DIR/pmie_daily -c $tmp.control -VV -l $tmp.log
$sudo cat $tmp.log >>$seq.full
_wait_for /tmp/$seq/1.good.log
ps $PCP_PS_ALL_FLAGS | grep '[p]mie' >>$seq.full
sleep 3		# fill rotated log a bit

echo "Shutdown pmie process"
echo "=== pmie_check ===" >>$seq.full
$sudo $PCP_BINADM_DIR/pmie_check -c $tmp.control -s -VV -l $tmp.log
$sudo cat $tmp.log >>$seq.full
ps $PCP_PS_ALL_FLAGS | grep '[p]mie' >>$seq.full

grep rotated /tmp/$seq/1.good.log >/dev/null \
	|| echo "First log not rotated?"
grep rotated /tmp/$seq/1.good.log.$previous >/dev/null \
	|| echo "New log not started?"

# look for data in each log file, checking rotation actually did something
oldlines=`wc -l < /tmp/$seq/1.good.log.$previous 2>/dev/null || echo 0`
newlines=`wc -l < /tmp/$seq/1.good.log 2>/dev/null || echo 0`
# 5 samples / sec x ~6 sec x 2 lines per sample + 6 lines for header and footer
# so 66
_within_tolerance "Old logfile line count" "$oldlines" 66 %75 -v
# 5 samples / sec x ~3 sec x 2 lines per sample + 6 lines for header and footer
# so 36
_within_tolerance "New logfile line count" "$newlines" 36 %75 -v

echo "=== previous log ($oldlines lines) ===" >>$seq.full
cat /tmp/$seq/1.good.log.$previous >>$seq.full
echo "=== current log ($newlines lines) ===" >>$seq.full
cat /tmp/$seq/1.good.log >>$seq.full

# success, all done
status=0
exit
