|
#!/bin/bash
##############################################################
# Program: genfs.sh
#
# Description: Generate the "cricket" directories and files
# to describe the filesystem structure on a remote machine.
# This script uses "rsh" to run a "df" command on the
# remote machine and builds the "cricket" files based on the
# output from the "df command.
#
# 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:${DESTDIR})"
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}/filesystems" ]]
then
(( VERBOSE == 1 )) && echo "Generating destination directory ${DESTDIR}/filesystems"
echo "${DESTDIR}/filesystems 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}/filesystems"
if mkdir -p "${DESTDIR}/filesystems"
then
(( VERBOSE == 1 )) && echo "${DESTDIR}/filesystems successfully created"
else
echo "ERROR: Unable to create directory \"${DESTDIR}/filesystems\"" >&2
exit 2
fi
else
echo "ERROR: Unable to continue with specified directory \"${DESTDIR}/filesystems\"" >&2
exit 3
fi
else
(( VERBOSE == 1 )) && echo "Destination directory \"${DESTDIR}/filesystems\" already exists"
fi
DEFAULTSFILE="${DESTDIR}/filesystems/Defaults"
TARGETFILE="${DESTDIR}/filesystems/target"
#
# Generate Defaults File
#
(( VERBOSE == 1 )) && echo "Generating \"${DEFAULTSFILE}\" file"
echo "
target --default--
directory-desc = \"${OSNAME} filesystem usage\"
target-type = %auto-target-name%
server = %auto-target-name%
df = %auto-base%/../cricket/util/df.aix
##### 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 = \"Percentage of Filesystem Used\"
" > "${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 "/usr/bin/df ${DFFLAGS}" |
awk '{ print $2, $5, $6 }' |
sed -e "1d" |
while read SIZE USED FSLINE
do
MBSIZE=$( echo "${SIZE} * 512 / 1024 / 1024" | bc )
DATASRC="${FSLINE//\//-}"
DATASRC="${DATASRC/#-/}"
DATASRC="${DATASRC:-root}"
(( 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 = \"${FSLINE} partition Size = ${MBSIZE} MB\"" >> "${DEFAULTSFILE}"
TARGNAME="${FSLINE//\//-}"
TARGNAME="${TARGNAME/#-/}"
TARGNAME="${TARGNAME:-root}"
SHORTDESC="${FSLINE/#\//}"
SHORTDESC="${SHORTDESC:-root}"
(( VERBOSE == 1 )) && echo "target ${TARGNAME}"
echo '' >> "${TARGETFILE}"
echo "target ${TARGNAME}" >> "${TARGETFILE}"
echo " target-type = server${TARGNAME}" >> "${TARGETFILE}"
echo " short-desc = \"${SHORTDESC/#\//}\"" >> "${TARGETFILE}"
echo " partitions = \"${FSLINE}\"" >> "${TARGETFILE}"
done
echo "" >> "${DEFAULTSFILE}"
echo "html short-desc \"Filesystem Usage\"" >> "${DEFAULTSFILE}"
(( VERBOSE == 1 )) && echo "#### Ending ${0} for server ${SERVER} ####"
exit 0
|
|
|