#!/bin/sh
# PCP QA Test No. 1158
# Exercise the BCC PMDA biotop module - install, remove and values.
#
# Copyright (c) 2018 Andreas Gerstmayr.
#

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

. ./common.bcc

_pmdabcc_check
_bcc_check_ArgString || _notrun "bcc is broken (ArgString bug) on this platform"

status=1       # failure is the default!
signal=$PCP_BINADM_DIR/pmsignal
$sudo rm -rf $tmp.* $seq.full

write_size=`expr 20 \* 1024 \* 1024` # number of bytes to write into testfile
out_file="$PCP_TMPFILE_DIR/pcp-qa-$seq" # block device required here (no tmpfs)

_value_filter_bytes()
{
    cat > $tmp.value
    echo === input to _value_filter >>$here/$seq.full
    cat $tmp.value >> $here/$seq.full

    _found=false
    _values=`awk '/inst.*value/ {print $NF}' $tmp.value`
    for value in $_values
    do
        if _within_tolerance "Expecting ${write_size} +- 10%" $value ${write_size} 10%
	then
            echo "found value ${write_size} +- 10%"
	    _found=true
            break
        fi
    done
    if ! $_found
    then
	echo "Failed to find writer process in:"
	cat $tmp.value
    fi
}

_cleanup()
{
    _pmdabcc_cleanup
    $sudo rm -f "$out_file"
}

_prepare_pmda bcc
trap "_cleanup; exit \$status" 0 1 2 3 15
_stop_auto_restart pmcd

# real QA test starts here
cat <<EOF | _pmdabcc_install
[pmda]
modules = biotop
prefix = bcc.
[biotop]
module = biotop
cluster = 10
interval = 5
debug = True
EOF
_pmdabcc_wait_for_metric

# Generate system activity for the BCC biotop module
echo Started dd at $(date) >> $here/$seq.full
dd if=/dev/zero of=${out_file} bs=${write_size} count=1 oflag=direct 2>$here/$seq.full

# wait until PMDA refreshes BPF table containing dd
_pmdabcc_wait_for_value bcc.proc.io.perdev.comm '"dd"'

echo "=== report metric values for pid ==="
pminfo -dfmtT bcc.proc.io.perdev.pid 2>&1 | tee -a $here/$seq.full \
| _value_filter_nonzero

echo "=== report metric values for comm ==="
pminfo -dfmtT bcc.proc.io.perdev.comm 2>&1 | tee -a $here/$seq.full \
| _value_filter_exact '"dd"'

echo "=== report metric values for direction ==="
pminfo -dfmtT bcc.proc.io.perdev.direction 2>&1 | tee -a $here/$seq.full \
| _value_filter_exact '"W"'

echo "=== report metric values for major ==="
pminfo -dfmtT bcc.proc.io.perdev.major 2>&1 | tee -a $here/$seq.full \
| _value_filter_nonzero

echo "=== report metric values for minor ==="
pminfo -dfmtT bcc.proc.io.perdev.minor 2>&1 | tee -a $here/$seq.full \
| _value_filter_any

echo "=== report metric values for disk ==="
pminfo -dfmtT bcc.proc.io.perdev.disk 2>&1 | tee -a $here/$seq.full \
| _value_filter_any

echo "=== report metric values for io ==="
pminfo -dfmtT bcc.proc.io.perdev.io 2>&1 | tee -a $here/$seq.full \
| _value_filter_nonzero

echo "=== report metric values for bytes ==="
pminfo -dfmtT bcc.proc.io.perdev.bytes 2>&1 | tee -a $here/$seq.full \
| _value_filter_bytes

echo "=== report metric values for duration ==="
pminfo -dfmtT bcc.proc.io.perdev.duration 2>&1 | tee -a $here/$seq.full \
| _value_filter_nonzero

_pmdabcc_remove

status=0
exit
