#!/bin/ksh
# Set the array with the configurable directories so that you can parse them later
set -A dir_array Bin_Path Data_Path Archive_Path Archive_Backup_Path
set -A config_file_path "$HOME/DB2Audit/DB2Audit.conf"
set -A LOG_FILE "$HOME/DB2Audit/db2audit.log"
#########################################
# if_error ()
# Prints an error and returns the proper
# return code to the script
#########################################
function if_error
{
if [[ $? -ne 0 ]]; then
# check return code passed to function
for i in "$@";
do
print "FATAL ERROR: Please check log file for details. Log file is located in the home dir of the user."
print "[`date +"%Y%m%d-%H:%M:%S"`] $i" >> $LOG_FILE
done
# if rc > 0 then print error msg and quit
exit $?
fi
}
# Set globals holding respective paths to empty strings. To be filled later.
set -A Bin_Path ""
set -A Data_Path ""
set -A Archive_Path ""
set -A Archive_Backup_Path ""
set -A Arch_Backup_File "arch_bkup_"
set -A ARCHIVE_BACKUP_FILE ""
# Check if the file exists
if [ -e "$config_file_path" ];
then
# For each of the dirs detailed in the array, fill up the globals with the dir paths
for dir in ${dir_array[@]}
do
case $dir in
"Bin_Path")
Bin_Path=`grep $dir $config_file_path | cut -d "=" -f2`
if [ -d $Bin_Path ]; then
print "[`date +"%Y%m%d-%H:%M:%S"`] Configured Bin Path : $Bin_Path" >> $LOG_FILE
# If path exists, check for executable
if [ ! -f "$Bin_Path\db2audit" ]; then
if_error "Not a valid bin path" "Check for the right directory where you can find the command line tool db2audit"
fi
else
if_error "ERROR: Cannot locate configured Bin Path $Bin_Path" "Please check if directory exists"
fi
;;
"Data_Path")
Data_Path=`grep $dir $config_file_path | cut -d "=" -f2`
if [ -d "$Data_Path" ]; then
print "[`date +"%Y%m%d-%H:%M:%S"`] Configured Data Path : $Data_Path" >> $LOG_FILE
else
if_error "ERROR: Cannot locate configured Data Path $Data_Path" "Please check if directory exists"
fi
;;
"Archive_Path")
Archive_Path=`grep $dir $config_file_path | cut -d "=" -f2`
if [ -d "$Archive_Path" ]; then
print "[`date +"%Y%m%d-%H:%M:%S"`] Configured Archive Path = $Archive_Path" >> $LOG_FILE
else
if_error "ERROR: Cannot locate configured Archive Path $Archive_Path" "Please check if directory exists"
fi
;;
"Archive_Backup_Path")
Archive_Backup_Path=`grep $dir $config_file_path | cut -d "=" -f2`
if [ -d "$Archive_Backup_Path" ]; then
print "[`date +"%Y%m%d-%H:%M:%S"`] Configured Archive Backup Path = $Archive_Backup_Path" >> $LOG_FILE
else
if_error "ERROR: Cannot locate configured Archive Backup Path $Archive_Backup_Path" "Please check if directory exists"
fi
;;
"*")
if_error "Parameters not configured properly in conf file. Please re-check."
;;
esac
done
ARCHIVE_BACKUP_FILE=$Archive_Backup_Path/$Arch_Backup_File`date "+%Y%m%d-%H-%M-%S"`.bkup
else
if_error "FATAL ERROR: Config file not found." "Cannot configure db2 script."
fi
if [ -f $Archive_Path/*.del ]; then
print "[`date +"%Y%m%d-%H:%M:%S"`] Moving all .del files to the archive backup folder" >> $LOG_FILE
`cp -f $Archive_Path/*.del $Archive_Backup_Path`
`rm -rf $Archive_Path/*.del`
if_error "ERROR: Could not backup files to specified path" "Please check Archive_Path and Archive_Backup_Path"
fi
if [ -f $Archive_Backup_Path/*.del ]; then
print "[`date +"%Y%m%d-%H:%M:%S"`] Copying the contents of all .del files to the Archive Backup File. You can identify the file by the timestamp." >> $LOG_FILE
`cat $Archive_Backup_Path/*.del >> $ARCHIVE_BACKUP_FILE`
`rm -rf *.del`
if_error "ERROR: Could not backup files to specified path" "Please check Archive_Path and Archive_Backup_Path"
fi
# Check for Archive Backup folder size, and delete old log files if size>250M
LOG_FOLDER_SIZE=`du -sm $Archive_Backup_Path | cut -f 1`
if [ $LOG_FOLDER_SIZE -gt 250 ]; then
ARCH_OLDEST_FILE=`ls -rU $Archive_Backup_Path | tail -1`
print "[`date +"%Y%m%d-%H:%M:%S"`] Archive Folder size exceeded ceiling limit. Deleting oldest log file $ARCH_OLDEST_FILE" >> $LOG_FILE
`rm /$Archive_Backup_Path/$ARCH_OLDEST_FILE`
if_error "ERROR: Cannot delete old log files" "Please check permissions on files"
fi
# Run the DB2 Audit Scripts in this section
print "[`date +"%Y%m%d-%H:%M:%S"`] Logging all events to DB2Audit Log file. It is located in the home dir of the user."
print "[`date +"%Y%m%d-%H:%M:%S"`] Executing DB2 Audit Commands" | tee -a $LOG_FILE
print "[`date +"%Y%m%d-%H:%M:%S"`] Writing 'db2audit describe' output to log file" >> $LOG_FILE
print "[`date +"%Y%m%d-%H:%M:%S"`] Executing db2audit describe"
`$Bin_Path/db2audit describe >> $LOG_FILE`
if_error "Flush Unsuccessful" "Please check Bin_Path for db2audit script"
print "[`date +"%Y%m%d-%H:%M:%S"`] Writing 'db2audit archive' output to log file" >> $LOG_FILE
print "[`date +"%Y%m%d-%H:%M:%S"`] Executing db2audit archive"
`$Bin_Path/db2audit archive >> $LOG_FILE`
if_error "Archive Unsuccessful" "Please check the Bin_Path for the db2audit script"
print "[`date +"%Y%m%d-%H:%M:%S"`] Writing 'db2audit extract' output to log file" >> $LOG_FILE
print "[`date +"%Y%m%d-%H:%M:%S"`] Executing db2audit extract"
`$Bin_Path/db2audit extract delasc delimiter ! from files $Archive_Path/db2audit.instance.log.0.20* >> $LOG_FILE`
if_error "Please check Bin_Path for db2audit script" "Audit Log Extract Unsuccessful"
print "[`date +"%Y%m%d-%H:%M:%S"`] Removing DB2 Audit Instance Log File from $Archive_Path" | tee -a $LOG_FILE
`rm $Archive_Path/db2audit.instance.log.0.20*`
print "[`date +"%Y%m%d-%H:%M:%S"`] Completing DB2 Audit Command Execution" | tee -a $LOG_FILE
IT咨询服务 · 2012-06-01