#!/bin/sh
#
# Copyright (c) 2022 Red Hat.  All Rights Reserved.
#
# PCP QA Test No. 1828
# test hinv.map.scsi_id and multipath stats
# Uses linux/scsi-map-root.tar.xz. To remake, use qa/linux/mkfakesys.sh
# and then create the tarball with tar cJf ~/src/pcp/qa/linux/scsi-map-root.tgz \
#                                  proc sys/block sys/devices/{pci*,pseu*,virt*}
seq=`basename $0`
echo "QA output created by $seq"

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

[ $PCP_PLATFORM = linux ] || _notrun "Linux-specific scsi device metrics testing"

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

# instance assignments (internal ids) are not deterministic
_slice_sort_reassemble()
{
    sed \
	-e 's/\([0-9][0-9]*\) or "docker/N or "docker/' \
	-e 's/\([0-9][0-9]*\) or "cciss/N or "cciss/' \
	-e 's/\([0-9][0-9]*\) or "home/N or "home/' \
	-e 's/\([0-9][0-9]*\) or "md/N or "md/' \
	-e 's/\([0-9][0-9]*\) or "sd/N or "sd/' \
	-e 's/\([0-9][0-9]*\) or "sr/N or "sr/' \
	-e 's/\([0-9][0-9]*\) or "vd/N or "vd/' \
	-e 's/\([0-9][0-9]*\) or "rbd/N or "rbd/' \
	-e 's/\([0-9][0-9]*\) or "nbd/N or "nbd/' \
	-e 's/\([0-9][0-9]*\) or "nvme/N or "nvme/' \
	-e 's/\([0-9][0-9]*\) or "luks-01568490-8910-4c31-84bd-b4ddb53858c0/N or "luks-01568490-8910-4c31-84bd-b4ddb53858c0/' \
	-e 's/\([0-9][0-9]*\) or "mpathb/N or "mpathb/' \
	-e 's/\([0-9][0-9]*\) or "shack-root/N or "shack-root/' \
	-e 's/\([0-9][0-9]*\) or "shack-swap/N or "shack-swap/' \
    | $here/src/sortinst
}

# real QA test starts here
root=$tmp.root
export LINUX_STATSPATH=$root
export LINUX_MDADM=/bin/true
pmda=$PCP_PMDAS_DIR/linux/pmda_linux.so,linux_init
metrics=`pminfo -L -K clear -K add,60,$pmda disk.dev disk.dm hinv.map.scsi_id | LC_COLLATE=POSIX sort`

tgz=$here/linux/scsi-map-root-001.tgz
rm -fr $root
mkdir $root || _fail "root in use when processing $tgz"
cd $root
tar xzf $tgz

PMINFO="pminfo -L -K clear -K add,60,$pmda"
base=`basename $tgz`
echo "== Checking metric values - $base" | tee -a $seq_full
$PMINFO -f $metrics 2>&1 | tee -a $seq_full \ | _slice_sort_reassemble
( echo && echo "== done" && echo ) | tee -a $seq_full

for leaf in read read_bytes write write_bytes total blkread blkwrite blktotal \
    total_bytes read_merge write_merge avactive read_rawactive write_rawactive \
    total_rawactive
do
    echo "== Checking aggregated values of disk.dev.$leaf sd[bcde] sum to disk.wwid.$leaf " | \
    	tee -a $seq_full
    $PMINFO -f disk.wwid.$leaf | \
    	$PCP_AWK_PROG '/333333330000007d1/ {print "disk.wwid.'$leaf'", $6}' | \
	tee -a $seq_full
    $PMINFO -f disk.dev.$leaf | \
    	$PCP_AWK_PROG '/sd[bcde]/ {sum += $6} END {print "disk.dev.'$leaf' sums to", sum}' | \
	tee -a $seq_full
done

# success, all done
status=0
exit
