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

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

. ./common.bcc

_pmdabcc_check
which curl >/dev/null 2>&1 || _notrun "No curl binary installed"
[ "$(pmpython src/bcc_version_check.python)" = "0.6.1" ] \
  && _notrun "Broken BCC version detected"

target_ip=1.1.1.1

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

_install_filter()
{
    sed \
    -e "s/Using BPF source file .\+/Using BPF source file X/g" \
    #end
}

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

cat <<EOF | _pmdabcc_install | _install_filter
# Installed by PCP QA test $seq on `date`
[pmda]
modules = tcplife
prefix = bcc.
[tcplife]
module = tcplife
cluster = 3
dport = 80
EOF

_pmdabcc_wait_for_metric

# Generate system activity for the BCC tcplife module
curl -s http://${target_ip}

_pmdabcc_wait_for_value bcc.proc.io.net.tcp.pid

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

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

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

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

echo "=== report metric values for daddr ==="
pminfo -dfmtT bcc.proc.io.net.tcp.daddr 2>&1 | tee -a $here/$seq.full \
| _value_filter_exact '"'${target_ip}'"'

echo "=== report metric values for dport ==="
pminfo -dfmtT bcc.proc.io.net.tcp.dport 2>&1 | tee -a $here/$seq.full \
| _value_filter_exact 80

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

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

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

_pmdabcc_remove

status=0
exit
