#!/bin/sh
# PCP QA Test No. 1740
# Exercise Linux acct(5) process accounting metrics.
#
# Copyright (c) 2020 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
if [ $# -eq 0 ]
then
    echo "QA output created by $seq"
else
    echo "QA output created by $seq $*"
fi

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

[ $PCP_PLATFORM = linux ] || _notrun "Linux-specific pmdaproc testing"

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

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

_filter()
{
    # capture all in .full but just output first inst and value
    tee -a $here/$seq.full | \
    sed \
	-e 's/inst \[[0-9][0-9]*/inst [N/g' \
	-e 's/or \".*\"]/or INSTNAME]/g' \
	-e 's/value .*/value VALUE/g' \
    | head -3
}

_header()
{
    echo
    echo === $* ===
    echo
}

# real QA test starts here
psinfo=`pminfo acct.psinfo | LC_COLLATE=POSIX sort`
flag=`pminfo acct.flag | LC_COLLATE=POSIX sort`
id=`pminfo acct.id | LC_COLLATE=POSIX sort`

_pminfo()
{
    for metric in $psinfo $flag $id
    do
	$sudo pminfo -f $metric | _filter
    done
}

# ensure accounting is initially off
$sudo pmstore acct.control.enable_acct 0 >>$seq.full

_header "initially disabled"
_pminfo

_header "enable accounting as non-root"
pmstore acct.control.enable_acct 1
_header "enable accounting as root"
$sudo pmstore acct.control.enable_acct 1
_pminfo

_header "disable accounting"
$sudo pmstore acct.control.enable_acct 0
_pminfo

# success, all done
status=0
exit
