#!/bin/sh
# PCP QA Test No. 1443
# check pmlogger_janitor
#
# Copyright (c) 2023 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

_check_job_scheduler

_cleanup()
{
    cd $here
    if $needclean
    then
	_service pmlogger start 2>&1 | _filter_pcp_start
	_restore_job_scheduler $tmp.cron $tmp.systemd $sudo
	_wait_for_pmlogger
	needclean=false
    fi
    $sudo rm -rf $tmp $tmp.* /var/tmp/janitor-$seq.log
    for dir in orphan-$hostname orphan-localhost
    do
	$sudo rm -rf "$PCP_ARCHIVE_DIR/$dir"
    done
    $sudo rm -f "$PCP_ETC_DIR/pcp/pmlogger/control.d/orphan-$hostname"
    $sudo rm -f "$PCP_ETC_DIR/pcp/pmlogger/control.d/orphan-localhost"
}

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

needclean=true
hostname=`hostname | sed -e 's/\..*//'`

_filter()
{
    tee -a $seq.full \
    | sed \
	-e '/ PPID /d' \
	-e '/check .*-g pmlogger/d' \
	-e '/^pcp /d' \
	-e 's/2[0-9][0-9][0-9][01][0-9][0-3][0-9]\.[0-5][0-9]\.[0-5][0-9]/TIMESTAMP/g' \
	-e 's/TIMESTAMP-[0-9][0-9]/TIMESTAMP/g' \
	-e "s@$PCP_ARCHIVE_DIR/@PCP_ARCHIVE_DIR/@g" \
	-e "s/$hostname/HOSTNAME/g" \
	-e "s/PID $pid_1\$/PID PID_1/" \
	-e "s/PID $pid_2\$/PID PID_2/" \
    # end
}

# real QA test starts here
_remove_job_scheduler $tmp.cron $tmp.systemd $sudo

# create orphaned pmloggers for pmlogger_janitor testing
#

cat <<End-of-File >$tmp.policy
# policy file for the orphan class, qa/1443
[class]
orphan

[ident]
orphan-%h

[control]
\$version=1.1
\$PMLOGGER_CHECK_SKIP_JANITOR=yes

\$class=orphan
%h n n PCP_ARCHIVE_DIR/%i -c $tmp.config

[create]
# matches all hosts
hostname(.*)
End-of-File

cat <<End-of-File >$tmp.config
log mandatory on default { sampledso.bin }
End-of-File

# start 2 pmloggers
#
for host in localhost $hostname
do
    $sudo rm -f /etc/pcp/pmlogger/control.d/orphan-$host
    $sudo pmlogctl create -p $tmp.policy $host
    $sudo pmlogctl start -p $tmp.policy -c orphan $host
done

# looking for a line like ...
# localhost            20230318.07.31    orphan    729669 running
# to get the pid
#
pid_1=`pmlogctl status -c orphan localhost 2>&1 | awk '
NF == 5 && $5 = "running"	{ print $4 }'`
pmlogctl status -c orphan localhost >>$seq.full
echo "pid_1=$pid_1" >>$seq.full
if [ -z "$pid_1" ]
then
    echo "Arrgh, no PID from ..."
    pmlogctl status -c orphan localhost
else
    # remove the status file for this one
    #
    $sudo rm $PCP_TMP_DIR/pmlogger/$pid_1
fi
pid_2=`pmlogctl status -c orphan $hostname 2>&1 | awk '
NF == 5 && $5 = "running"	{ print $4 }'`
pmlogctl status -c orphan $hostname >>$seq.full
echo "pid_2=$pid_2" >>$seq.full
if [ -z "$pid_2" ]
then
    echo "Arrgh, no PID from ..."
    pmlogctl status -c orphan $hostname
fi

# hide 'em from the control files
#
for host in localhost $hostname
do
    $sudo rm -f /etc/pcp/pmlogger/control.d/orphan-$host
done

$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep -E '[P]PID|/[p]mlogger ( |$)' >>$seq.full

# now run the janitor ...
#
$sudo -u pcp $PCP_BINADM_DIR/pmlogger_janitor -l /var/tmp/janitor-$seq.log
cat /var/tmp/janitor-$seq.log >>$seq.full
_filter </var/tmp/janitor-$seq.log >$tmp.tmp
grep Killing <$tmp.tmp | LC_COLLATE=POSIX sort
grep Compressing <$tmp.tmp | LC_COLLATE=POSIX sort
grep -v Killing <$tmp.tmp | grep -v Compressing

# success, all done
exit
