#!/bin/sh
# PCP QA Test No. 1685
# Exercise the chrony PMDA using dbpmda with canned data.
#
# Copyright (c) 2026 Nutanix, Inc.  All Rights Reserved.
#

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

. ./common.python

python_path=`which $python`
pmda_path="$PCP_PMDAS_DIR/chrony"
pmda_script="$pmda_path/pmdachrony.python"
test -f "$pmda_script" || _notrun "pmdachrony not supported"
$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 "cd $here; rm -rf $tmp $tmp.*; exit \$status" 0 1 2 3 15

_filter()
{
    tee -a $seq_full | \
    sed \
	-e "s;$PCP_PMDAS_DIR;\$PCP_PMDAS_DIR;" \
	-e '/pmResult/s/ .* numpmid/ ... numpmid/' \
	-e '/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/s/[^ ]*/TIMESTAMP/' \
	-e "s;$python_path;\$PCP_PYTHON_PROG;" \
	-e "s;$python_basename;python;" \
    #end
}

_filter_result()
{
    _filter | _filter_dumpresult
}

domain=165
export CHRONY_TESTDIR="$here/chrony"
export CHRONYC="$here/chrony/chronyc"

# real QA test starts here
PCP_PYTHON_PMNS=root $python "$pmda_script" > $tmp.root

echo "== Testing source instance domain" | tee -a $seq_full
TERM=ansi dbpmda -n $tmp.root -ie <<End-of-File 2>&1 | _filter
open pipe $python_path $pmda_script
instance $domain.0
End-of-File

echo "== Testing tracking metrics" | tee -a $seq_full
TERM=ansi dbpmda -n $tmp.root -ie <<End-of-File 2>&1 | _filter
open pipe $python_path $pmda_script
getdesc on
desc chrony.tracking.ref_id
desc chrony.tracking.stratum
desc chrony.tracking.leap_status
fetch chrony.tracking.ref_id
fetch chrony.tracking.stratum
fetch chrony.tracking.leap_status
fetch chrony.tracking.freq
End-of-File

echo "== Testing sources metrics" | tee -a $seq_full
TERM=ansi dbpmda -n $tmp.root -ie <<End-of-File 2>&1 | _filter
open pipe $python_path $pmda_script
getdesc on
desc chrony.sources.m
desc chrony.sources.stratum
fetch chrony.sources.m
fetch chrony.sources.stratum
End-of-File

echo "== Testing sourcestats metrics" | tee -a $seq_full
TERM=ansi dbpmda -n $tmp.root -ie <<End-of-File 2>&1 | _filter
open pipe $python_path $pmda_script
getdesc on
desc chrony.sourcestats.np
desc chrony.sourcestats.offset
fetch chrony.sourcestats.np
fetch chrony.sourcestats.offset
End-of-File

# success, all done
status=0
exit
