#!/bin/sh
# PCP QA Test No. 1998
# Testing PCP RDS PMDA - install, remove and values.
#
# Copyright (c) 2025 Oracle and/or its affiliates.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#

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

. ./common.python

pmda_path="$PCP_PMDAS_DIR/rds"
pmda_script="$pmda_path/pmdards.python"
test_script="rds/rds_test.python"

[ -d $pmda_path ] || _notrun "RDS PMDA is not installed"

$python -c "from pcp import pmda" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmda module not installed"

status=1	# failure is the default!
trap "_cleanup_pmda rds; exit \$status" 0 1 2 3 15

pmdards_filter()
{
    sed \
        -e "s/Ran [0-9]* tests in [0-9]*\.[0-9][0-9]*s/Ran X tests in YYYs/" \
        -e "s/FAILED (failures=[0-9]*)/FAILED (failures=X)/" \
        -e "/^$/d"
}

pmdards_remove()
{
    cd $pmda_path
    echo
    echo "=== Removing RDS agent ===" | tee -a $seq_full
    $sudo ./Remove >>$seq_full 2>&1
}

pmdards_install()
{
    cd $pmda_path
    $sudo ./Remove >>$seq_full 2>&1

    echo
    echo "=== Installing RDS agent ===" | tee -a $seq_full
    $sudo ./Install </dev/null >>$seq_full 2>&1
    cd $here
}

# Real QA test starts here
_prepare_pmda rds

pmdards_install

if ! pminfo -f pmcd.agent.status 2>&1 | grep -q '"rds"'
then
    echo "Arrgh! PMDA install failed ... see $seq.full"
    _exit 1
fi

echo "=== Report metric values ==="

metrics=`pminfo rds | LC_COLLATE=POSIX sort`
echo "metric=$metrics" >>$seq_full
if [ -z "$metrics" ]
then
    echo "Botch: no rds metrics in the PMNS"
else
    pminfo -dfmtT $metrics 2>&1
fi

pmdards_remove

# Success, all done
status=0
exit
