#!/bin/sh
# PCP QA Test No. 1170
# Exercise the BCC PMDA tcptop 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"
case "`uname -srm`"
in
    Linux\ 5.2.*)
    		_notrun "Bad Linux 5.2 kernel headers will cause BPF to fail"
		;;
esac

target_ip=1.1.1.1

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

# Some combinations of distro and kernel are known to be "bad"
# for some BPF modules
#
kernel=`uname -r`
echo "$kernel" >>$seq.full
case `admin/whatami`
in
    *Fedora\ 32\ *)
	_notrun "bcc tcptop module will not compile on Fedora 32"
	;;
    *Ubuntu\ 19.10\ *)
	_notrun "bcc tcptop module will not compile on Ubuntu 19.10"
	;;
    *Ubuntu\ 20.04\ *)
	# works on 5.4 kernel, not on 5.8.0-1033-azure
	#
	case "$kernel"
	in
	    5.8.0-*-azure)
		_notrun "bcc tcptop module will not compile on Ubuntu kernel $kernel"
		;;
	esac
	;;
    *openSUSE\ Leap\ 15.1\ *)
	_notrun "bcc tcptop module will not compile on openSUSE 15.1"
	;;
esac

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

# real QA test starts here
cat <<EOF | _pmdabcc_install
# Installed by PCP QA test $seq on `date`
[pmda]
modules = tcptop
prefix = bcc.
[tcptop]
module = tcptop
cluster = 12
interval = 5
debug = True
EOF
_pmdabcc_wait_for_metric

# Generate system activity for the BCC tcptop module
echo Started curl at $(date) >> $here/$seq.full
curl -s http://${target_ip} > /dev/null

# wait until PMDA refreshes BPF table containing curl
_pmdabcc_wait_for_value bcc.proc.io.net.tcptop.daddr '"'${target_ip}'"'

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

# curl processes already exited, therefore command is not available (only PID is shown)
echo "=== report metric values for comm ==="
pminfo -dfmtT bcc.proc.io.net.tcptop.comm 2>&1 | tee -a $here/$seq.full \
| _value_filter_any

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

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

echo "=== report metric values for daddr ==="
pminfo -dfmtT bcc.proc.io.net.tcptop.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.tcptop.dport 2>&1 | tee -a $here/$seq.full \
| _value_filter_exact 80

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

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

_pmdabcc_remove

status=0
exit
