#!/bin/sh
#Tag 0x00010D14
# 
# Copyright 1997, Silicon Graphics, Inc.
# ALL RIGHTS RESERVED
# 
# UNPUBLISHED -- Rights reserved under the copyright laws of the United
# States.   Use of a copyright notice is precautionary only and does not
# imply publication or disclosure.
# 
# U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
# Use, duplication or disclosure by the Government is subject to restrictions
# as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
# in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
# in similar or successor clauses in the FAR, or the DOD or NASA FAR
# Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
# 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
# 
# THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
# INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
# DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
# PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
# GRAPHICS, INC.
# 
# $Id: Configure,v 1.2 1999/05/26 02:38:46 kenmcd Exp $
# 
# Configures the espping PMDA, using as input (by default):
#  - /var/pcp/pmdas/espping/sgm.map    (maps hostnames to services of interest)
#  - /var/pcp/pmdas/espping/template.conf   (maps services to "ping" commands)
# and produces as output the espping configuration file.
#

# some degree of paranoia here ...
PATH=/usr/sbin:/usr/bsd:/sbin:/usr/bin:/bin:/usr/pcp/bin
export PATH

status=1
date=`date`
local=`hostname`
prog=`basename $0`

tmp=/tmp/$$
trap "rm -f $tmp.*; exit \$status" 0 1 2 3  15
rm -f $tmp.*

cycle=${ESPPING_CYCLE="300"}		# 5 minutes
timeout=${ESPPING_TIMEOUT="20"}		# 20 seconds
input=${ESPPING_MAP="/var/pcp/pmdas/espping/sgm.map"}
output=${ESPPING_CONFIG="/var/pcp/pmdas/espping/sgm.conf"}
template=${ESPPING_TEMPLATE="/var/pcp/pmdas/espping/template.conf"}

_usage()
{
    [ ! -z "$@" ] && echo $@ 1>&2
    echo 1>&2 "Usage: $prog [options]

Options:
  -a cycletime	time in seconds between running all commands
  -b timeout	period after which a command will be considered hung
  -m map        use map file to provide host - services mapping
                [defaults to /var/pcp/pmdas/espping/sgm.map]
  -o config	output an espping PMDA configuration file
                [defaults to /var/pcp/pmdas/espping/sgm.conf]
  -t template	use template file to provide service 'ping' commands
                [defaults to /var/pcp/pmdas/espping/template.conf]"
    exit
}

_error()
{
    echo "$prog: error - $@"
    exit
}

while getopts "?a:b:o:m:t:" c
do
    case $c in
      a) cycle="$OPTARG" ;;
      b) timeout="$OPTARG" ;;
      m) input="$OPTARG" ;;
      o) output="$OPTARG" ;;
      t) template="$OPTARG" ;;
      ?) _usage "" ;;
    esac
done

shift `expr $OPTIND - 1`
[ $# -gt 0 ] && _usage "$prog: too many arguments"

[ ! -f "$template" ] && _error "cannot read template file $template"
[ ! -f "$input" ] && _error "cannot read from map file $input"

outfile=`basename $output`
cat >$tmp.hdr <<EOF
# 
# $outfile
# 
# --- DO NOT EDIT THIS FILE --- it was automatically generated by
# /var/pcp/pmdas/espping/$prog on $date
# 

EOF

rm -f $tmp.out
nawk -F ',[ \t]*|[ \t]+' <$input '
NF < 2		{ next }
/^#/		{ next }
		{ for (i=2; i <= NF; i++) printf "%s %s\n", $1, $i }
' | while read host service
do
    if [ $host = "LOCAL" ]
    then
	grep "^$service@LOCAL" $template | sed -e "s/LOCAL/$local/g" >>$tmp.out
    else
	grep "^$service@HOST" $template | sed -e "s/HOST/$host/g" >>$tmp.out
    fi
done

sort $tmp.out | uniq | cat $tmp.hdr - > $output
[ ! -s $output ] && _error "espping configuration file is empty/creation failed"

cd /var/pcp/pmdas/espping

# don't want this file showing up in the list of choices...
# 
rm -f /var/pcp/config/espping/espping.conf

# expecting these questions...
#   Please enter c(ollector) or m(onitor) or b(oth) [b] 
#   Which configuration file do you want to use ? [1] 
#   Enter the name of the existing configuration file: 
#   Use this configuration file? [y] 
# 
cat >$tmp.install <<EOF
b
2
$output
y
$cycle
$timeout
EOF

echo
echo "Please wait while espping PMDA is configured..."

./Install < $tmp.install >/dev/null

echo 
echo "The espping PMDA is now configured - any errors from here on will be"
echo "reported in /var/adm/pcplog/pmcd.log or /var/adm/pcplog/espping.log."
echo

status=0
