#!/bin/sh
# PCP QA Test No. 1362
# check postgresql PMDA can reconnect when db bounces
#
# Copyright (c) 2018 Red Hat.  All Rights Reserved.
#

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

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

[ -d $PCP_PMDAS_DIR/postgresql ] || _notrun "postgresql PMDA directory is not installed"
[ -f $PCP_PMDAS_DIR/postgresql/pmdapostgresql.python ] || _notrun "postgresql PMDA is not installed"

echo '\q' | $sudo -u postgres psql >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "Cannot run psql as the postgres user, postgresql not installed or running?"
$sudo -u postgres psql -c "select VERSION();" | grep -s "PostgreSQL 8" > /dev/null
[ $? -eq 0 ] && _notrun "not testing postgres v8.x, too old"

CONF=$PCP_PMDAS_DIR/postgresql/pmdapostgresql.conf

status=1	# failure is the default!
$sudo rm -rf $tmp.* $seq.full
trap "cd $here; $sudo mv $CONF.$seq $CONF; _cleanup_pmda postgresql; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

pmdapostgresql_remove()
{
    echo | tee -a $here/$seq.full
    echo "=== remove postgresql agent ===" | tee -a $here/$seq.full
    $sudo ./Remove >$tmp.out 2>&1
    cat $tmp.out >>$here/$seq.full
    _filter_pmda_remove <$tmp.out
}

pmdapostgresql_install()
{
    # start from known starting points
    cd $PCP_PMDAS_DIR/postgresql
    $sudo mv $CONF $CONF.$seq
    cat <<EOF>$tmp.conf
[authentication]
host=local
port=5432
dbname=postgres
user=postgres
password=password
osuser=postgres
EOF

    $sudo mv $tmp.conf $CONF
    $sudo ./Remove >/dev/null 2>&1
    _service pmcd stop 2>&1 | _filter_pcp_stop

    echo | tee -a $here/$seq.full
    echo "=== postgresql agent installation ===" | tee -a $here/$seq.full
    $sudo ./Install </dev/null >$tmp.out 2>&1
    cat $tmp.out >>$here/$seq.full
    _filter_pmda_install < $tmp.out | sed -e '1,/Updating the PMCD control file/d' \
    | $PCP_AWK_PROG '
/Check postgresql metrics have appeared/   { if ($7 < 20) $7 = "X"
                                          if ($9 >= 200) $9 = "Y"
                                          if ($12 >= 7000) $12 = "Z"
                                        }
                                        { print }'
}

_prepare_pmda postgresql

# note: _restore_auto_restart pmcd done in _cleanup_pmda()
_stop_auto_restart pmcd

# real QA test starts here
pmdapostgresql_install

echo;echo == check initial fetch
pminfo -f postgresql.stat.activity.usename >/dev/null 2>&1 || exit
echo OK

echo === restart postgresql service
$sudo service postgresql restart >>$here/$seq.full 2>&1

sleep 1

echo === checking log for disconnect messages
pminfo -f postgresql.stat.activity.usename >/dev/null 2>&1
grep -i "Warning: connection lost" $PCP_LOG_DIR/pmcd/postgresql.log | sed -e 's/^.*Warning/Warning/'

sleep 2

echo;echo == fetch again .. should have reconnected
pminfo -f postgresql.stat.activity.usename >/dev/null 2>&1 | exit
echo OK

echo "PMDA log file ..." >>$here/$seq.full
$sudo cat $PCP_LOG_DIR/pmcd/postgresql.log >>$here/$seq.full

pmdapostgresql_remove

status=0
exit
