#!/bin/bash
##############################################################
# Program: gennmon.sh
# 
# Description: Generate the "cricket" directories and files
#     to describe the "nmon" data on a remote machine.
# 
# Author: Dana French (dfrench@mtxia.com)
#         Mt Xia
# 
# Date: 06/10/2003
# 
##############################################################
function usagemsg {
  echo ""
  echo "Usage: ${0} [-v] [-y] [-u ruser] [-m a|s|l] [-d destDir] server"
  echo ""
  echo "  Where '-v' = Turn on verbose mode (default:off)"
  echo "        '-y' = Automatically answer \"yes\" to any questions asked (default:ask)"
  echo "        '-u ruser' = Remote user name to use for rsh commands (default:${RUSER})"
  echo "        '-d destDir' = Specify the destination directory in which "
  echo "                       to create the filesystem information (default:${DATASETID})"
  echo "        '-m MachType' = Machine type of remote machine. Valid Types are:"
  echo "                   a  =  AIX"
  echo "                   s  =  Sun"
  echo "                   l  =  Linux (default)"
  echo ""
  echo "  server = The name of a remote server from which to retrieve"
  echo "           file system information (required parameter)"
  echo ""
}
################################################################
VERBOSE="0"
DESTDIR="."
DFFLAGS="-h"
MACHTYPE=""
OSNAME=""
AUTOYES="0"
RUSER="${RUSER:=xmon}"

while getopts "m:d:u:yv" OPTION
do
    case "${OPTION}" in
        'm') MACHTYPE="${OPTARG}"
             if [[ "_${MACHTYPE}" = _[Aa]* ]]
             then
                 OSNAME="AIX"
                 DFFLAGS="-I"
             fi
             [[ "_${MACHTYPE}" = _[Ss]* ]] && OSNAME="Solaris"
             [[ "_${MACHTYPE}" = _[Ll]* ]] && OSNAME="Linux"
             ;;
        'y') AUTOYES="1";;
        'd') DESTDIR="${OPTARG}";;
        'u') RUSER="${OPTARG}";;
        'v') VERBOSE="1";;
        '?') usagemsg && exit 1 ;;
    esac
done

shift $(( ${OPTIND} - 1 ))

SERVER="${1:?ERROR: run \"${0} -?\" for help and usage}"
shift

(( VERBOSE == 1 )) && echo "#### Beginning ${0} for server ${SERVER} ####"

if [[ ! -d "${DESTDIR}/nmon/${DATASETID}" ]]
then

  (( VERBOSE == 1 )) && echo "Generating destination directory ${DESTDIR}/nmon/${DATASETID}"

   echo "${DESTDIR}/nmon/${DATASETID} does not exist. Create it (y/n)?"
   if (( AUTOYES == 1 ))
   then
       ANS="y"
       (( VERBOSE == 1 )) && echo "${ANS}"
   else
       read ANS JUNK
   fi
     
   if [[ "_${ANS}" = _[Yy]* ]]
   then
       (( VERBOSE == 1 )) && echo "Attempting to create destination directory ${DESTDIR}/nmon/${DATASETID}"
       if mkdir -p "${DESTDIR}/nmon/${DATASETID}"
       then
           (( VERBOSE == 1 )) && echo "${DESTDIR}/nmon/${DATASETID} successfully created"
       else
           echo "ERROR: Unable to create directory \"${DESTDIR}/nmon/${DATASETID}\"" >&2
           exit 2
       fi
   else
       echo "ERROR: Unable to continue with specified directory \"${DESTDIR}/nmon/${DATASETID}\"" >&2
           exit 3
   fi
else
   (( VERBOSE == 1 )) && echo "Destination directory \"${DESTDIR}/nmon/${DATASETID}\" already exists"
fi

DEFAULTSFILE="${DESTDIR}/nmon/Defaults"
TARGETFILE="${DESTDIR}/nmon/target"

#
# Generate Defaults File
#

(( VERBOSE == 1 )) && echo "Generating \"${DEFAULTSFILE}\" file"

echo "
target  --default--
	directory-desc  = \"nmon data\"
	target-type     = %auto-target-name%
	server          = %auto-target-name%
	df              = %auto-base%/../cricket/util/nmon.sh

##### Datasources #########

datasource      --default--
	rrd-ds-type     = GAUGE
	rrd-min         = 0
	rrd-max         = 100

graph   --default--
	draw-as         = LINE2
	units           = \"\"
	y-min           = 0
	y-max           = 100
	y-axis          = \"Description of nmon data\"
" > "${DEFAULTSFILE}"

#
# Generate target File
#

(( VERBOSE == 1 )) && echo "Generating \"${TARGETFILE}\" file"

echo "target --default--
	server          = ${SERVER}
	short-desc      = \"Server\"" > "${TARGETFILE}"

/usr/bin/rsh -l ${RUSER} $SERVER "sed -e '1,/^ZZZZ,/ !d' /tmp/*.nmon | cut -d',' -f1 | sort -n | uniq | egrep -v '^AAA|^BBB|^ZZZ'" |
while read DATASETID
do
    echo "DATASETID=${DATASETID}" >&2
    DATASRC="${DATASETID}"

    (( VERBOSE == 1 )) && echo '' && echo "datasource      server${DATASRC}"
    echo '' >> "${DEFAULTSFILE}"
    echo "datasource      server${DATASRC}" >> "${DEFAULTSFILE}"
    echo "	ds-source       = \"exec:0:%df% %server% %partitions%\"" >> "${DEFAULTSFILE}"
    echo "" >> "${DEFAULTSFILE}"
    echo "targetType      server${DATASRC}" >> "${DEFAULTSFILE}"
    echo "	ds  = \"server${DATASRC}\"" >> "${DEFAULTSFILE}"

    (( VERBOSE == 1 )) && echo "graph      server${DATASRC}"
    echo "" >> "${DEFAULTSFILE}"
    echo "graph   server${DATASRC}" >> "${DEFAULTSFILE}"
    echo "	draw-as         = AREA" >> "${DEFAULTSFILE}"
    echo "	legend          = \"${DATASETID} Data Set\"" >> "${DEFAULTSFILE}"

    TARGNAME="${DATASETID}"
    SHORTDESC="${DATASETID}"

    (( VERBOSE == 1 )) && echo "target ${TARGNAME}"
    echo '' >> "${TARGETFILE}"
    echo "target ${TARGNAME}" >> "${TARGETFILE}"
    echo "	target-type     = server${TARGNAME}" >> "${TARGETFILE}"
    echo "	short-desc      = \"${SHORTDESC/#\//}\"" >> "${TARGETFILE}"
    echo "	partitions      = \"${DATASETID}\"" >> "${TARGETFILE}"

done

echo "" >> "${DEFAULTSFILE}"
echo "html    short-desc      \"${DATASETID}\"" >> "${DEFAULTSFILE}"

(( VERBOSE == 1 )) && echo "#### Ending ${0} for server ${SERVER} ####"

exit 0