#! /bin/sh
# PCP QA Test No. 1108
# pv:821339 sed gives "Too many commands" in daily cron script
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
# Copyright (c) 2018 Red Hat. All Rights Reserved.
#
# check-group-exclude: pmlogger
# check-group-exclude: pmnewlog
# check-group-include: pmlogextract
#

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

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

NEEDREWRITE=$PCP_LOG_DIR/pmlogger/.NeedRewrite

_cleanup()
{
    # restore yesterday's files
    #
    cd $PCP_LOG_DIR/pmlogger
    for dir in */$seq
    do
	[ "$dir" = "*/$seq" ] && continue
	$sudo mv "$dir"/* $dir/..
	$sudo rmdir "$dir"
    done
    cd $here

    test -f $PCP_LOG_DIR/NOTICES.orig && \
	$sudo mv $PCP_LOG_DIR/NOTICES.orig $PCP_LOG_DIR/NOTICES
    $sudo rm -f $tmp.*
    [ -f $NEEDREWRITE.$seq ] && $sudo mv $NEEDREWRITE.$seq $NEEDREWRITE
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15
$sudo rm -f $tmp.* $seq.full

[ -f $NEEDREWRITE ] && $sudo mv $NEEDREWRITE $NEEDREWRITE.$seq

# don't need to regenerate config.default with pmlogconf
#
export PMLOGGER_CHECK_SKIP_LOGCONF=yes

_filter()
{
    sed \
	-e "s;$PCP_BINADM_DIR/pmlogger;pmlogger;g" \
	-e '/^+ date-and-timestamp /s/stamp .*/stamp .../' \
	-e '/^+ cd /s/cd .*/cd .../' \
	-e '/^+ mv /s/mv .*/mv .../' \
	-e '/^+ ln /d' \
	-e '/^+ pmlogger /s/pmlogger .*/pmlogger .../' \
	-e '/^+ pmnewlog /s/pmnewlog .*/pmnewlog .../' \
	-e '/^+ mkaf /s/mkaf .*/mkaf .../' \
	-e '/^+ pmsignal/{
s/ [0-9][0-9]*/ ... PID/
}' \
	-e '/^+ rm /d' \
	-e '/^+ xz /d' \
	-e '/^+ pmlogger_merge /d' \
	-e '/^[12][0-9][0-9][0-9][01][0-9][0-3][0-9]\.[0-9][0-9]\.[0-9][0-9]/d' \
	-e '/Warning: no write access/d' \
	-e 's/ *$//' \
	| tr -s ' '
}

echo "pmlogger processes at the beginning ..." >>$seq.full
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep pmlogger >>$seq.full

# Avoid conflict with normal management of the summary logs ...
# this is lifted from pmlogger_daily

# each summarized log is named yyyymmdd using yesterday's date
# previous day's logs are named yymmdd (old format) or
# yyyymmdd (new year 2000 format)
#
SUMMARY_LOGNAME=`pmdate -1d %Y%m%d`
echo "SUMMARY_LOGNAME=$SUMMARY_LOGNAME" >>$seq.full
echo "Files to be moved before we start ..." >>$seq.full
ls -l $PCP_LOG_DIR/pmlogger/*/$SUMMARY_LOGNAME* >>$seq.full 2>&1
# save yesterday's files
#
cd $PCP_LOG_DIR/pmlogger
for file in */$SUMMARY_LOGNAME*
do
    [ "$file" = "*/$SUMMARY_LOGNAME*" ] && continue
    dir=`dirname $file`
    [ ! -d "$dir/$seq" ] && $sudo mkdir "$dir/$seq"
    $sudo mv "$file" "$dir/$seq"
done
cd $here

# create a temporary control file with well-known contents, ensure
# we exercise a fix for rebuilding zero-length configuration files
$sudo rm -f $tmp.control
echo '$version=1.1' >> $tmp.control
echo "LOCALHOSTNAME y n PCP_LOG_DIR/pmlogger/LOCALHOSTNAME -c $tmp.conf" >> $tmp.control
touch $tmp.conf

# make sure pmlogger_daily gets a timestamp that is at least
# a minute after the previous primary logger was started to avoid
# the collision on the timestamp and the extra 3 mv's needed to
# rename the archive files from <timestamp> to <timestamp>-00
#
current=`pminfo -f pmcd.pmlogger.archive | sed -n -e '/"primary"/{
s;.*/;;
s/"//
p
}'`
if [ -z "$current" ]
then
    echo "Arrgh ... cannot get current primary pmlogger timestamp from"
    pminfo -f pmcd.pmlogger.archive
    ls -l $PCP_RUN_DIR/pmlogger*
    status=1
    exit
fi
echo "current=$current" >>$seq.full
sec=0
while [ $sec -lt 60 ]
do
    now=`pmdate %Y%m%d.%H.%M`
    [ "$now" != "$current" ] && break
    echo "now=$now" >>$seq.full
    sleep 1
    sec=`expr $sec + 1`
done
echo "done, now=$now" >>$seq.full

# real QA test starts here
test -f $PCP_LOG_DIR/NOTICES && \
	$sudo mv $PCP_LOG_DIR/NOTICES $PCP_LOG_DIR/NOTICES.orig
$sudo $PCP_BINADM_DIR/pmlogger_daily -o -k 550 -N -c $tmp.control 2>&1 \
| tee -a $seq.full \
| _filter
