#!/bin/sh
# PCP QA Test No. 1611
# Exercise pmlogger and the logger REST API interfaces.
#
# Copyright (c) 2025 Red Hat.  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

do_valgrind=false
if [ "$1" = "--valgrind" ]
then
    _check_valgrind
    do_valgrind=true
fi

audit_args="always,exit -F arch=`uname -m` -S kill -S tgkill -S tkill -F a1=1 -k sighup"
if which auditctl >/dev/null 2>&1
then
    date +'%H:%M:%S' >$tmp.start
    $sudo auditctl -a $audit_args >>$seq_full 2>&1
fi

needclean=true

_cleanup()
{
    cd $here
    date +'start cleanup: %H:%M:%S.%N' >>$seq_full
    if $needclean
    then
	echo "Stopping pmproxy ..." >>$seq_full
	_service pmproxy stop >>$seq_full 2>&1
	_restore_config $PCP_SYSCONF_DIR/pmproxy/pmproxy.conf
	_restore_config $PCP_SYSCONF_DIR/pmproxy/pmproxy.options
	_restore_auto_restart pmproxy
	needclean=false
	if $pmproxy_was_running
	then
	    date +'restart pmproxy %H:%M:%S.%N' >>$seq_full
	    _service pmproxy restart >>$seq_full 2>&1
	    date +'pmproxy restarted %H:%M:%S.%N' >>$seq_full
	fi
	if which auditctl >/dev/null 2>&1
	then
	    $sudo auditctl -d $audit_args >>$seq_full 2>&1
	    $sudo ausearch -k sighup -i -ts `cat $tmp.start` >>$seq_full 2>&1
	fi
	# pmlogger scripts can get confused by the pmproxy run
	# in this test ... safest to start with a new pmlogger
	#
	_service pmlogger restart 2>&1 | _filter_pcp_restart
	_restore_job_scheduler $tmp.cron $tmp.systemd $sudo
	_wait_for_pmlogger
    fi
    date +'end cleanup %H:%M:%S.%N' >>$seq_full

    $sudo rm -rf $tmp $tmp.* $archive_path
}

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

_filter()
{
    sed \
	-e "s@$tmp@TMP@g" \
	-e "s@$archive_path@ARCHIVE_PATH@g" \
	-e "s/TMP\.data\./LOCAL_ARCHIVE./g" \
	-e "s/2[0-9][0-9]*\.[0-9][0-9]\.[0-9][0-9]\./REMOTE_ARCHIVE./g" \
    # end
}

_filter_metrics()
{
    sed \
	-e "/^pmcd\.*/d" \
	-e "/^event\.*/d" \
    | LC_COLLATE=POSIX sort
}

# real QA test starts here
archive_host=`hostname`
archive_path=$PCP_REMOTE_ARCHIVE_DIR/$archive_host
archive_botch=$PCP_LOG_DIR/pmproxy/pmproxy

pmproxy_was_running=false
[ -f $PCP_RUN_DIR/pmproxy.pid ] && pmproxy_was_running=true
echo "pmproxy_was_running=$pmproxy_was_running" >>$seq_full
date +'stopping pmproxy %H:%M:%S.%N' >>$seq_full

_save_config $PCP_SYSCONF_DIR/pmproxy/pmproxy.conf

_stop_auto_restart pmproxy
if ! _service pmproxy stop >$tmp.tmp 2>&1; then cat $tmp.tmp; _exit 1; fi
cat $tmp.tmp >>$seq_full

$sudo rm -fr $archive_path $archive_botch

# pmproxy config
cat >$tmp.conf << End-Of-File
# Installed by PCP QA test $seq on `date`
[pmproxy]
pcp.enabled = false
http.enabled = true
[pmlogger]
enabled = true
[discover]
enabled = false
[pmseries]
enabled = false
End-Of-File
$sudo cp $tmp.conf $PCP_SYSCONF_DIR/pmproxy/pmproxy.conf

# pmproxy.options
_save_config $PCP_SYSCONF_DIR/pmproxy/pmproxy.options
cp $PCP_SYSCONF_DIR/pmproxy/pmproxy.options $tmp.options
echo >>$tmp.options
echo "Added by PCP QA test $seq on `date`" >>$tmp.options
echo '-D appl0,appl2' >>$tmp.options
$sudo cp $tmp.options $PCP_SYSCONF_DIR/pmproxy/pmproxy.options
if ! _service pmproxy start >$tmp.tmp 2>&1; then cat $tmp.tmp; _exit 1; fi
_filter_pmproxy_start <$tmp.tmp
_wait_for_pmproxy || _exit 1

# need to disable timers and cron jobs ... pmlogger_daily -Q risks
# whacking our pmproxy with a SIGHUP
#
# NB: the timers are re-enabled in _cleanup()
#
_remove_job_scheduler $tmp.cron $tmp.systemd $sudo

PCP_DERIVED_CONFIG=""; export PCP_DERIVED_CONFIG

# pmlogger config
cat >$tmp.conf << End-Of-File
log mandatory on once {
    hinv.ncpu
    hinv.ndisk
}
log mandatory on every 1 second {
    sample.datasize
    disk.all.read
    disk.all.write
    kernel.all.load
    kernel.all.pswitch
}
End-Of-File

echo
echo "=== Running pmlogger in remote recording mode"
date +'starting pmlogger %H:%M:%S.%N' >>$seq_full
remote_mode="-c $tmp.conf -T 8sec http://localhost:44322"
if $do_valgrind
then
    _run_valgrind pmlogger $remote_mode -l $tmp.log
else
    pmlogger $remote_mode -l $tmp.log
fi \
| _filter
date +'pmlogger done %H:%M:%S.%N' >>$seq_full
cat $tmp.log >>$seq_full
cat $PCP_LOG_DIR/pmproxy/pmproxy.log >>$seq_full

if [ -d $archive_path ]
then
    echo "## Found remote archive files"
    ls -1 $archive_path | _filter | LC_COLLATE=POSIX sort
    echo "## with the following metrics"
    pminfo -a $archive_path | _filter_metrics
else
    echo "Archive push failed: $archive_path directory not created"
    status=1
    exit
fi
if [ -d $archive_botch ]
then
    echo "Bad archive path $archive_botch found but should not be"
    status=1
    exit
fi

# success, all done
exit
