#!/bin/sh
# PCP QA Test No. 1286
# Exercise the BCC PMDA netproc hits module - install, remove and values.
#
# Copyright (c) 2020 Red Hat.
#

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

. ./common.bcc

_pmdabcc_check
_pmdabcc_require_kernel_version 4 6
which curl >/dev/null 2>&1 || _notrun "No curl binary installed"
which dig >/dev/null 2>&1 || _notrun "No dig binary installed"
which strace >/dev/null 2>&1 || _notrun "No strace binary installed"

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

target_http=http://1.1.1.1
target_dns=1.1.1.1

_value_filter()
{
    awk '/"'"$1"'"/ && /value [1-9][0-9]*/ {print "OK"; exit}'
}

_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
[pmda]
modules = netproc
prefix = bcc.
[netproc]
module = netproc
cluster = 40
pmda_indom_cache = False
remove_stopped_processes = False
EOF
_pmdabcc_wait_for_metric

# Generate system activity for the BCC netproc module
# TCP
curl -s ${target_http} > /dev/null &
pid_curl=$!
echo "curl pid: ${pid_curl}" >> "$seq.full"
wait ${pid_curl}

# UDP
# dig performs the DNS request from the first forked process
pid_dig=`strace -f -e 'trace=!all' dig @$target_dns -x $target_dns 2>&1 | awk '/Process .* attached/ { print $3; exit }'`
echo "dig pid: ${pid_dig}" >> "$seq.full"

for metric in bcc.proc.io.net.perpid.tcp.in.packets \
              bcc.proc.io.net.perpid.tcp.in.bytes \
              bcc.proc.io.net.perpid.tcp.out.packets \
              bcc.proc.io.net.perpid.tcp.out.bytes
do
    echo "=== report metric values for $metric ==="
    pminfo -f $metric 2>&1 | tee -a $here/$seq.full \
    | tee -a $seq.full \
    | _value_filter ${pid_curl}
done

for metric in bcc.proc.io.net.perpid.udp.in.packets \
              bcc.proc.io.net.perpid.udp.in.bytes \
              bcc.proc.io.net.perpid.udp.out.packets \
              bcc.proc.io.net.perpid.udp.out.bytes
do
    echo "=== report metric values for $metric ==="
    pminfo -f $metric 2>&1 | tee -a $here/$seq.full \
    | tee -a $seq.full \
    | _value_filter ${pid_dig}
done

_pmdabcc_remove

status=0
exit
