#!/usr/bin/ksh93
##############################################################
# Program: genemstat.sh
# 
# Description: Generate the "cricket" directories and files
#     to describe the "emstat" 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} ####"

[[ "_${SERVER}" = "_mdctxuapp80" ]] &&
  DIRDESC="Production App. Server #1 System Performance Data"

[[ "_${SERVER}" = "_mdctxuapp81" ]] &&
  DIRDESC="Production App. Server #2 System Performance Data"

[[ "_${SERVER}" = "_mdctxuapp82" ]] &&
  DIRDESC="Production App. Server #3 System Performance Data"

[[ "_${SERVER}" = "_mdctxuapp51" ]] &&
  DIRDESC="Pre-Production App. Server #1 System Performance Data"

[[ "_${SERVER}" = "_mdctxuapp52" ]] &&
  DIRDESC="Pre-Production App. Server #2 System Performance Data"

[[ "_${SERVER}" = "_mdctxuapp53" ]] &&
  DIRDESC="Pre-Production App. Server #3 System Performance Data"

print "
#####################################################################
# Top-level defaults file for unix hosts
#
# Started 12-16-99 by James Moore <jam@afn.org>
#
# Change Log :
# 	28/01/2000 : UCD-SNMP OID's added 
#              : default datasource added
#              : default graph added
#              - Stuart Grimshaw <stuart.grimshaw@blackburn.gov.uk>
#		02-01-2000 : Changed OIDs for UCD load averages
#              : Added graphs for ucd disk usage
#              - James Moore
#
# To Do :
#
#####################################################################

target --default--
    directory-desc      = \"${DIRDESC}\"
	snmp-host	=	%server%

# Memory stats
OID	xmdPagSpEfree		.1.3.6.1.4.1.2.3.1.2.2.2.1.3.1.4.1
OID	xmdMemRealEfree		.1.3.6.1.4.1.2.3.1.2.2.2.1.2.1.10.0

# CPU Stats
OID	xmdCPUGluser		.1.3.6.1.4.1.2.3.1.2.2.2.1.1.101.0
OID	xmdCPUGlkern		.1.3.6.1.4.1.2.3.1.2.2.2.1.1.102.0
OID	xmdCPUGlidle		.1.3.6.1.4.1.2.3.1.2.2.2.1.1.104.0


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

datasource --default--
	rrd-ds-type	=	GAUGE


datasource xmdPagSpEfree
	ds-source	=	snmp://%snmp%/xmdPagSpEfree

datasource xmdMemRealEfree
	ds-source	=	snmp://%snmp%/xmdMemRealEfree

datasource xmdCPUGluser
	ds-source	=	snmp://%snmp%/xmdCPUGluser

datasource xmdCPUGlkern
	ds-source	=	snmp://%snmp%/xmdCPUGlkern

datasource xmdCPUGlidle
	ds-source	=	snmp://%snmp%/xmdCPUGlidle

datasource xmdEmStat
	ds-source       =	\"exec:0:%df% %server%\"

#### Target Types #########

targetType	CPU
	ds	=	\"xmdCPUGluser, xmdCPUGlkern\"
	view	=	\"cpu: xmdCPUGluser xmdCPUGlkern\"

targetType	TCPU
        ds      =       \"xmdCPUGlidle\"
        view    =       \"Total-CPU:  xmdCPUGlidle\"

targetType	Memory
	ds	=	\"xmdMemRealEfree, xmdPagSpEfree\"
	view	=	\"Memory: xmdMemRealEfree xmdPagSpEfree\"

targetType	EmStat
        ds      =       \"xmdEmStat\"
	view	=	\"EmStat: xmdEmStat\"

#### Graphs ###############

graph --default--
	y-max	=	undef

graph	xmdPagSpEfree
	legend	=	\"% Available Paging Space\"
	#scale	=	1024,*
	#bytes	=	true
	#units	=	\"%\"
        draw-as = LINE1

graph	xmdMemRealEfree
	legend	=	\"% Available RAM\"
	#scale	=	1024,*
	#bytes	=	true
	#units	=	\"%\"
        draw-as = AREA

graph	xmdCPUGluser
	legend	=	\"% User CPU\"
        draw-as = AREA

graph	xmdCPUGlkern
	legend	=	\"% System CPU\"
        draw-as = LINE1

graph	xmdCPUGlidle
	legend	=	\"% CPU Idle\"
        draw-as = AREA

graph	xmdEmStat
	legend	=	\"Instruction Emulations per second\"
        draw-as = AREA

html    page-header
    <img src=\"images/txu_hc.gif\" align=left>
	<center>
    <h1>${SERVER}</h1> 
    <h1>%auto-title%</h1>
	</center>
    <br clear=left>
" > ${DESTDIR}/Defaults


print "
Target --default--
	server		=	${SERVER}.tu.com
	snmp-community	=	public

target CPU
	target-type	=	CPU
	short-desc	=	\"User and System CPU Utilization.\"

target Total_CPU
	target-type	=	TCPU
	short-desc	=	\"Total CPU Utilization.\"

target Memory
	target-type	=	Memory
	short-desc	=	\"Memory, and Paging Space Utilization\"

target  EmStat
	directory-desc  = \"Instruction Emulation\"
	target-type     = \"EmStat\"
	server          = ${SERVER}
	df              = %auto-base%/../cricket/util/emstat.sh
	short-desc	=	\"Instruction Emulations\"
" > ${DESTDIR}/target




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

exit 0