Listing 1 entrap
#!/bin/ksh
#****************************************************************
# Listing 1:
# File: entrap
#
# Description:
#
# This script is the driver that calls the other Entrap scripts.
# Several enviroment variables are set, the existence of important
# files and directories is verified, and the user is prompted to
# create certain directories if they do not exist. If an invalid
# or no operand is supplied, a usage message prints and the program
# terminates.
#
# Author: John Spurgeon (john.p.spurgeon@intel.com)
#
#****************************************************************
# Define ENTRAP to show that we have been here.
export ENTRAP=entrap
# Define TRUE and FALSE.
export TRUE=0
export FALSE=1
# Initialize ERRORS.
ERRORS=$FALSE
# Entrap Directories
ENTRAP_DIR=/opt/entrap/entrap
BIN_DIR=$ENTRAP_DIR/bin
VAR_DIR=$ENTRAP_DIR/var
ETC_DIR=$ENTRAP_DIR/etc
CONFIG_DIR=$ETC_DIR/conf
export FILTER_ROOT=$ETC_DIR/filters
export SNAPSHOT_ROOT=$VAR_DIR/snapshots
export TEMP_DIR=$VAR_DIR/tmp
export CONFIG_NAME=entrap
export CONFIG_FILE=$CONFIG_DIR/$CONFIG_NAME
# Entrap Programs
export COUNT_ADDED=$BIN_DIR/count_added.sh
export LIST_ADDED=$BIN_DIR/list_added.sh
export COUNT_DELETED=$BIN_DIR/count_deleted.sh
export LIST_DELETED=$BIN_DIR/list_deleted.sh
export COUNT_CHANGED=$BIN_DIR/count_changed.sh
export LIST_CHANGED=$BIN_DIR/list_changed.sh
export FILTER=$BIN_DIR/filter.sh
export ATTRIBUTES=$BIN_DIR/attributes.sh
export ENTRAP=$BIN_DIR/entrap
# Valid Entrap Commands
CMD_SNAPSHOT=snapshot
CMD_FILTER=filter
CMD_COMPARE=compare
VALID_COMMANDS="$CMD_SNAPSHOT $CMD_FILTER $CMD_COMPARE"
# Default Values
if ! type ckyorn 2>- >-
then
# For non-Solaris environments, this function emulates the internal ckyorn
# function.
# optional parameters:
# d - default value
# p - user prompt
# Q - disable q for quit
# return: Y, y, N, N and q, Q if Q option is not disabled.
function ckyorn {
typeset option=
typeset default=
typeset prompt=
typeset REPLY=
typeset UREPLY=
typeset Q=Qq
while getopts :p:d:Q option
do
case $option
in
d) default=$OPTARG ;;
p) prompt=$OPTARG ;;
Q) typeset Q="" ;;
esac
done
while echo "$prompt\c" 1>&2
do
read REPLY
UREPLY=$REPLY
case "${UREPLY:=$default}"
in
[ynYN$Q]*) break ;;
*) echo "ERROR: Please enter yes or no." 1>&2 ;;
esac
done
echo "$UREPLY"
}
fi
# Get the options
while getopts c:o: OPTION
do
case "$OPTION"
in
c) CONFIG_NAME=$OPTARG ;;
\?) exit 1;;
esac
done
CONFIG_FILE=$CONFIG_DIR/$CONFIG_NAME
shift $(($OPTIND - 1))
# Verify that Entrap directories exist.
for DIR in \
$ENTRAP_DIR \
$BIN_DIR \
$VAR_DIR \
$ETC_DIR \
$TEMP_DIR \
$CONFIG_DIR \
$SNAPSHOT_ROOT \
$FILTER_ROOT
do
if ! [ -d $DIR ]
then
echo "[$(basename $0)] warning: Directory \"$DIR\" does not exist.\c"
ans=$(ckyorn -d y -p Create?)
case $ans in
y* | Y* )
mkdir $DIR
if [ -d $DIR ]
then
echo "[$(basename $0)] info: Directory \"$DIR\" was created."
else
echo "[$(basename $0)] error: Directory \"$DIR\" could not \
be created." >& 2
echo "[$(basename $0)] problem: Directory \"$DIR\" is \
required." >& 2
echo "[$(basename $0)] solution: Create directory \"$DIR\" \
or update entrap settings." >& 2
ERRORS=$TRUE
fi
;;
* )
echo "[$(basename $0)] error: Directory \"$DIR\" was not created."
echo "[$(basename $0)] problem: Directory \"$DIR\" is required." >& 2
echo "[$(basename $0)] solution: Create directory \"$DIR\" or \
update entrap settings." >& 2
ERRORS=$TRUE
;;
esac
fi
done
# Verify that the Entrap program and configuration files exist.
for FILE in \
$CONFIG_FILE \
$COUNT_ADDED \
$LIST_ADDED \
$COUNT_DELETED \
$LIST_DELETED \
$COUNT_CHANGED \
$LIST_CHANGED \
$FILTER \
$ATTRIBUTES \
$ENTRAP \
$BIN_DIR/$CMD_SNAPSHOT \
$BIN_DIR/$CMD_FILTER \
$BIN_DIR/$CMD_COMPARE
do
if ! [ -f $FILE ]
then
echo "[$(basename $0)] error: File \"$FILE\" does not exist." >& 2
echo "[$(basename $0)] problem: File \"$FILE\" is required." >& 2
echo "[$(basename $0)] solution: Reinstall the missing file." >& 2
ERRORS=$TRUE
fi
done
# Get the operand and options strings.
OPERAND=$1
if [ $# -gt 0 ]
then
shift
fi
OPTIONS=$@
# Verify that the command is valid.
VERIFIED_OPERAND=$FALSE
if ! [ -z "$OPERAND" ]
then
for COMMAND in $VALID_COMMANDS
do
if [ "$OPERAND" == "$COMMAND" ]
then
VERIFIED_OPERAND=$TRUE
fi
done
fi
if [ $ERRORS -eq $TRUE ]
then
exit 1
fi
if [ $VERIFIED_OPERAND -eq $TRUE ]
then
$BIN_DIR/$OPERAND $OPTIONS
else
echo "\nUsage:\tentrap [-c config_file] [command]"
echo "\n\twhere command is one of:\n"
for COMMAND in $VALID_COMMANDS
do
echo "\t`$BIN_DIR/$COMMAND -h`"
done
echo
echo "snapshots:\t\c"
echo $(ls -m $SNAPSHOT_ROOT)
echo "filters:\t\c"
echo $(ls -m $FILTER_ROOT)
echo "config files:\t\c"
echo $(ls -m $CONFIG_DIR)
echo "temp files:\t\c"
echo $(ls -m $TEMP_DIR)
echo
fi
find $TEMP_DIR -atime +1 -type f -exec rm -i {} ';'
exit 0
#!/bin/ksh
|