#!/bin/sh
# PCP QA Test No. 1845
# pmlogger_daily for pmproxy-pushed archives (like 1698)
# - rewrite and merge testing
# - see qa/1869 for cull and compress testing
#
# Copyright (c) 2025 Ken McDonell.  All Rights Reserved.
#

if [ $# -eq 0 ]
then
    seq=`basename $0`
    echo "QA output created by $seq"
else
    # use $seq from caller, unless not set
    [ -n "$seq" ] || seq=`basename $0`
    echo "QA output created by `basename $0` $*"
fi

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

# mimic test in pmlogger_daily ...
#
if [ -x "$PCP_BINADM_DIR/pmlogger_daily_report" ]
then
    if [ -f $PCP_VAR_DIR/config/pmlogconf/zeroconf/disk ]
    then
	:
    else
	_notrun "no zeroconf installation for pmlogger_daily_report metrics"
    fi
else
    _notrun "pmlogger_daily_report script not installed"
fi


_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

save_log_dir="$PCP_LOG_DIR"

_filter()
{
    tee -a $seq_full \
    | sed \
	-e '/cannot create .*pmlogger_daily.pid:/d' \
	-e '/pmlogger_daily.pid: Permission denied/d' \
	-e '/^Sending SIGHUP to pmproxy /d' \
	-e "s@$tmp@TMP@g" \
	-e "s@$PCP_BINADM_DIR@PCP_BINADM_DIR@g" \
	-e '/^PCP_BINADM_DIR\/pmsignal: .* Operation not permitted/d' \
	-e "s@$save_log_dir@PCP_LOG_DIR@g" \
	-e "s@\[.*/control@[TMP/control@" \
	-e "s/`pmdate -1d %Y%m%d`/TODAY-1/g" \
	-e "s/`pmdate -2d %Y%m%d`/TODAY-2/g" \
	-e '/^Start \[/s/ 2[0-9:. -]*/ DATESTAMP/' \
	-e '/^End \[/s/ 2[0-9:. -]*/ DATESTAMP /' \
    | _filter_ls -u -g
}

export PCP_LOG_DIR=$tmp
export PCP_ARCHIVE_DIR=$tmp/pmlogger
export PCP_REMOTE_ARCHIVE_DIR=$tmp/pmproxy

mkdir -p $PCP_REMOTE_ARCHIVE_DIR/qahost || exit 1
mkdir -p $PCP_ARCHIVE_DIR || exit 1
# ok-foo archives are less than 20sec long, so make the MM.SS parts
# of the archive basenames to ensure there is no temporal overlap
#
cat <<End-of-File >$tmp.config
qahost	archives/ok-mv-foo	`pmdate -1d %Y%m%d`
qahost	archives/ok-foo		`pmdate -2d %Y%m%d.00.10`
qahost	archives/ok-mv-foo	`pmdate -2d %Y%m%d.01.20`
qahost	archives/ok-foo		`pmdate -2d %Y%m%d.02.30`
End-of-File
./mk.logfarm -c $tmp.config $PCP_REMOTE_ARCHIVE_DIR
ls -lR $tmp >>$seq_full
find $PCP_REMOTE_ARCHIVE_DIR -name "*.index" \
| while read arch
do
    pmlogdump -L $arch >>$seq_full
done

cat <<End-of-File >$tmp.control
\$version=1.1
End-of-File

# real QA test starts here
echo "== default options, but no compression ==="
$PCP_BINADM_DIR/pmlogger_daily -V -c $tmp.control -x 5 -l $tmp.out >$tmp.tmp 2>&1
_filter <$tmp.tmp
_filter <$tmp.out

echo "Remaining archives files ..."
find $PCP_REMOTE_ARCHIVE_DIR -type f -print \
| LC_COLLATE=POSIX sort \
| _filter

# success, all done
exit
