#!/bin/sh
# PCP QA Test No. 366
# pmlogconf - migrate from version 1.0 to 2.0
#
# Copyright (c) 2010 Ken McDonell.  All Rights Reserved.
# Copyright (c) 2015 Red Hat.
#
# Maintenance notes.
# As pmlogconf usage grows, the generated pmlogconf files are going
# to become more complicated and more variable across platforms and
# in particular groups will change from not being expanded by default
# to be expanded by default, and new groups will be added.
#
# Where this test _expects_ a control line to be expanded to a group
# of metrics, we use platform/version variants of the *.out files.
#
# But where a control line is expanded on some platforms, but not
# on others, we should just cull the control line from the $tmp.conf
# file and the block of related changes in all the *.out files ...
# as long as some expansion is done, the test remains valid.
#
# Newly added groups should be handled by adding to the $tmp.cull-list
# file below, using the pmlogconf "tag", e.g. networking/nfs2-client, and
# possibly removing the block of related changes in all the *.out files
#
# Newly added metrics within a group we care about should be culled
# in _filter().
#

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

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

[ -f $PCP_BINADM_DIR/pmlogconf-setup ] || _notrun "pmlogconf version 2.0 not installed"
[ -f $PCP_BINADM_DIR/pcp-atop ] || _notrun "system monitoring tools not installed"
perl -e "use PCP::PMDA" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "perl PCP::PMDA module not installed"
[ -f $PCP_PMDAS_DIR/mysql/pmdamysql.pl ] || _notrun "mysql pmda not installed"
[ -f $PCP_PMDAS_DIR/postgresql/pmdapostgresql.pl ] || _notrun "postgresql pmda not installed"
[ -f $PCP_PMDAS_DIR/zimbra/pmdazimbra.pl ] || _notrun "zimbra pmda not installed"
[ -f $PCP_PMDAS_DIR/netfilter/pmdanetfilter.pl ] || _notrun "netfilter pmda not installed"

_filter()
{
    sed \
	-e '/network.interface.out.fifo/d' \
	-e '/network.interface.in.fifo/d' \
	-e "s;$PCP_VAR_DIR;\$PCP_VAR_DIR;g" \
	-e '/^[0-9]/d'
}

status=0	# success is the default!
$sudo rm -rf $tmp.* $seq.full
trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15

cat <<End-of-File >$tmp.cull-list
filesystem/xfs-io-linux
filesystem/xfs-io-irix
filesystem/rpc-server
networking/nfs2-client
networking/nfs2-server
networking/nfs3-client
networking/nfs3-server
networking/nfs4-client
networking/nfs4-server
networking/softnet
storage/vdo
storage/vdo-summary
tools/atop
tools/atop-httpstats
tools/atop-nfsclient
tools/atop-hotproc
tools/atop-proc
tools/cockpit
tools/collectl
tools/collectl-interrupts
tools/dmcache
tools/hotproc
tools/mpstat
tools/mpstat-summary
tools/mpstat-interrupts
tools/pidstat
tools/pidstat-summary
tools/tapestat
tools/vector
tools/vector-summary
tools/vmstat
tools/iostat
tools/sar
apache/summary
apache/uptime
apache/processes
nginx/summary
shping/summary
memcache/summary
elasticsearch/summary
postgresql/summary
sqlserver/summary
mysql/summary
oracle/summary
mmv/summary
memory/meminfo
memory/proc-linux
disk/perpartition
zeroconf/atop-proc
zeroconf/nfsclient
zeroconf/numastat
zeroconf/pidstat
zeroconf/pidstat-summary
zeroconf/tapestat
zeroconf/xfs-perdev
End-of-File

# turn $tmp.cull-list into an awk program to cull groups like
#+ networking/nfs2-client:n:default:
## NFS v2 client stats
#----
#
echo "BEGIN { i=0; skip=0" >$tmp.awk
sed -e 's/.*/ cull[i++] = "&"/' <$tmp.cull-list >>$tmp.awk
cat <<'End-of-File' >>$tmp.awk
}
$1 == "#+" { for (tag in cull) {
	split($2, word, /:/)
	if (word[1] == cull[tag]) {
	    skip = 1
	    next
	}
    }
    skip = 0
}
skip == 0 { print }
End-of-File

cat <<'End-of-File' >$tmp.conf
#pmlogconf 1.0
# pmlogconf.sh control file version
#
# pmlogger(1) config file created and updated by
# pmlogconf.sh(1).
#
# DO NOT UPDATE THE INTITIAL SECTION OF THIS FILE.
# Any changes may be lost the next time pmlogconf.sh is used
# on this file.
#

# System configuration
#
#+ I0:n:once:
## hardware configuration [nodevis, osvis, oview, routervis,
## pmchart:Overview]
#----

# Disk activity
#
#+ D0:n:default:
## activity (IOPs and bytes for both reads and writes) over all disks
## [osvis, pmstat, pmchart:Disk, pmchart:Overview]
#----
#+ D1:n:default:
## per controller disk activity [pmchart:DiskCntrls]
#----
#+ D2:n:default:
## per spindle disk activity [dkvis, pmie:per_disk]
#----
#+ D3:n:default:
## all available data per disk spindle
#----

# CPU activity
#
#+ C0:n:default:
## utilization (usr, sys, idle, ...) over all CPUs [osvis, pmstat,
## pmchart:CPU, pmchart:Overview, pmie:cpu]
#----
#+ C2:n:default:
## contributions to CPU wait time
#----
#+ C1:n:default:
## utilization per CPU [clustervis, mpvis, nodevis, oview, pmie:cpu,
## pmie:per_cpu]
#----
#+ C3:n:default:
## per CPU contributions to wait time
#----

# Kernel activity
#
#+ K0:n:default:
## load average and number of logins [osvis, pmstat, pmchart:LoadAvg,
## pmchart:Overview, pmie:cpu]
#----
#+ Ka:n:default:
## run and swap queues [pmkstat]
#----
#+ K1:n:default:
## context switches, total syscalls and counts for selected calls (e.g. read,
## write, fork, exec, select) over all CPUs [pmstat, pmchart:Syscalls,
## pmie:cpu]
#----
#+ K2:n:default:
## per CPU context switches, total syscalls and counts for selected calls
## [pmie:per_cpu]
#----
#+ K3:n:default:
## bytes across the read() and write() syscall interfaces
#----
#+ K4:n:default:
## interrupts [pmkstat]
#----
#+ K5:n:default:
## buffer cache reads, writes, hits and misses [pmchart:BufferCache,
## pmie:filesys]
#----
#+ K6:n:default:
## all available buffer cache data
#----
#+ K7:n:default:
## vnode activity
#----
#+ K8:n:default:
## name cache (namei, iget, etc) activity [pmchart:DNLC, pmie:filesys]
#----
#+ K9:n:default:
## asynchronous I/O activity
#----

# Memory
#
#+ M0:n:default:
## pages in and out (severe VM demand) [pmstat, pmchart:Paging]
#----
#+ M1:n:default:
## address translation (faults and TLB activity)
#----
#+ M2:n:default:
## kernel memory allocation [osvis, pmstat, pmchart:Memory, pmchart:Overview]
#----
#+ M3:n:default:
## current swap allocation and all swap activity [pmchart:Swap, pmie:memory]
#----
#+ M4:n:default:
## swap configuration
#----
#+ M5:n:default:
## "large" page and Origin node-based allocations and activity [nodevis,
## oview]
#----
#+ M7:n:default:
## NUMA migration stats [nodevis, oview]
#----
#+ M6:n:default:
## all NUMA stats
#----

# Network
#
#+ N0:n:default:
## bytes and packets (in and out) and bandwidth per network interface
## [clustervis, osvis, pmchart:NetBytes, pmchart:Overview, pmie:per_netif]
#----
#+ N1:n:default:
## all available data per network interface
#----
#+ N2:n:default:
## TCP bytes and packets (in and out), connects, accepts, drops and closes
## [pmchart:NetConnDrop, pmchart:NetPackets, pmie:network]
#----
#+ N3:n:default:
## all available TCP data [pmchart:NetTCPCongestion]
#----
#+ N4:n:default:
## UDP packets in and out [pmchart:NetPackets]
#----
#+ N5:n:default:
## all available UDP data
#----
#+ N6:n:default:
## socket stats (counts by type and state)
#----
#+ N7:n:default:
## all available data for other protocols (IP, ICMP, IGMP)
#----
#+ N8:n:default:
## mbuf stats (alloc, failed, waited, etc) [pmie:network]
#----
#+ N9:n:default:
## multicast routing stats
#----
#+ Na:n:default:
## SVR5 streams activity
#----

# Services
#
#+ S2:n:default:
## RPC stats [pmie:rpc]
#----

# Filesystems and Volumes
#
#+ F0:n:default:
## Filesystem fullness [pmchart:FileSystem, pmie:filesys]
#----
#+ F1:n:default:
## XFS data and log traffic
#----
#+ F2:n:default:
## all available XFS data
#----
#+ F3:n:default:
## XLV operations and bytes per volume [xlv_vis]
#----
#+ F4:n:default:
## XLV striped volume stats [xlv_vis]
#----
#+ F6:n:default:
## XVM operations and bytes per volume
#----
#+ F7:n:default:
## XVM stripe, mirror and concat volume stats [pmie:xvm]
#----
#+ F8:n:default:
## all available XVM data
#----
#+ F5:n:default:
## EFS activity
#----

# Hardware event counters
#
#+ H0:n:default:
## NUMALink routers [nodevis, oview, routervis, pmchart:NUMALinks,
## pmie:craylink]
#----
#+ H1:n:default:
## Origin hubs [pmie:craylink]
#----
#+ H2:n:default:
## global MIPS CPU event counters (enable first with ecadmin(1))
#----
#+ H3:n:default:
## XBOW activity [xbowvis]
#----

# DO NOT UPDATE THE FILE ABOVE THIS LINE
# Otherwise any changes may be lost the next time pmlogconf.sh is
# used on this file.
#
# It is safe to make additions from here on ...
#

End-of-File
cp $tmp.conf $tmp.orig

# real QA test starts here
echo q \
| $PCP_BINADM_DIR/pmlogconf $tmp.conf >/dev/null

cat $tmp.conf >$seq.full
echo >>$seq.full
cat $tmp.awk >>$seq.full
$PCP_AWK_PROG -f $tmp.awk <$tmp.conf >$tmp.tmp
echo >>$seq.full
echo "after awking ..." >>$seq.full
cat $tmp.tmp >>$seq.full

diff $tmp.orig $tmp.tmp | _filter

# success, all done
exit
