#! /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

signal=$PCP_BINADM_DIR/pmsignal
status=1	# failure is the default!
trap "rm -fr $tmp.*; $sudo rm -fr /tmp/$seq; exit \$status" 0 1 2 3 15

# 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
$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 >>$seq.full
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 >>$seq.full
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 >>$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
