DB2 diag log messages monitoring script
The below script can be used to find no of messages logged in DB2.
This script will automatically email you the execution steps along with no of messages logged from previous day beginning to current day's beginning.
Note:
Replace the <email_id> variable with your email id.
Environment:
OS: AIX 7.1
DB2: V10.5 FP4
#!/usr/bin/ksh
cat /dev/null <<Comment
To count no of errors,critical,severe,warning enteries
in db2diag log
The script will check the db2diag.log file for messages from the start of previous day to the begining of the current day.
No command line arguments
Output file will be at /tmp/db2diag_msg_count_$(date +%Y-%m-%d-%S)_$$.log
Any errors will be logged at /tmp/db2diag_msg_error_file_$(date +%Y-%m-%d-%S)_$$.log
The output files and error files older than 1 day will be removed.
--Gopinath - 27/Dec/2016
Comment
##################################################################
#To debug uncomment the below line and comment when not using:
#set -x
#---------------------------------------------------------------
##################################################################
#Unset the variables:
unset host_name current_user_home output_dir output_filename error_dir error_filename email_id error_count critical_count severe_count warning_count err_msg rc1 yest_date today_date
#Variable declaration:
typeset host_name=$(hostname) #Hostname
typeset current_user_home=${HOME} #Current user home directory
typeset output_dir="/tmp" #Output directory
typeset output_filename="db2diag_msg_count_$(date +%Y-%m-%d-%H%M%S)_$$.log" #Output filename
typeset error_dir="/tmp" #Error file directory
typeset error_filename="db2diag_msg_error_file_$(date "+%Y-%m-%d-%H%M%S")_$$.log" #Error file directory
typeset email_id="<email_id>@<yourcompany.com>" #DBA email id
typeset -i error_count #db2diag error msg count variable
typeset -i critical_count #db2diag critical msg count variable
typeset -i severe_count #db2diag severe msg count variable
typeset -i warning_count #db2diag warning msg count variable
typeset -i rc1 #Return code no.1
typeset yest_date #Yesterday's date
typeset today_date #Today's date
#Logging all errors:
touch ${error_dir}/${error_filename} 2> /dev/null
exec 2>>${error_dir}/${error_filename}
#Function delete older files
delete_old_file ()
{
find /tmp/* -prune -a -mtime +1 -name "db2diag_msg_count_*.log" -exec rm -f {} \;
find /tmp/* -prune -a -mtime +1 -name "db2diag_msg_error_file_*.log" -exec rm -f {} \;
}
#Function message loggr()
msg_loggr ()
{
typeset msg=${1}
typeset mode=${2}
if [ ${mode} -a ${mode} = "off" ]; then
print "$(date "+%Y-%m-%d %H:%M:%S"): ${msg}" > ${error_dir}/${error_filename}
send_email "${host_name}-DB2Diag message count script error" "errorfile"
fi
print "$(date "+%Y-%m-%d %H:%M:%S"): ${msg}" >> ${output_dir}/${output_filename}
}
#Function send email ()
send_email ()
{
typeset mail_subject=${1}
typeset mail_mode=${2}
if [ ${mail_mode} -a ${mail_mode} = "errorfile" ]; then
mail -s "${mail_subject}" ${email_id} < ${error_dir}/${error_filename}
exit
fi
mail -s "${mail_subject}" ${email_id} < ${output_dir}/${output_filename}
}
#Extracting date range:
today_date="$(date +%Y-%m-%d)"
yest_date="$(TZ=SGT11 date +%Y-%m-%d)"
#Checking the output directory/file and creating:
if [ ! -e ${output_dir} ]; then
msg_loggr "Directory ${output_dir} not found" "off"
else
touch ${output_dir}/${output_filename}
if [ ! -f ${output_dir}/${output_filename} ]; then
msg_loggr "Unable to create ${output_filename} in ${output_dir}" "off"
fi
msg_loggr "Script started"
msg_loggr "Output file created: ${output_dir}/${output_filename} "
msg_loggr "Error file created: ${error_dir}/${error_filename} "
fi
# Sourcing the DB2 profile:
msg_loggr "Sourcing the DB2 profile "
if [ -f ${current_user_home}/sqllib/db2profile ]; then
. ${current_user_home}/sqllib/db2profile
msg_loggr "Sourcing the DB2 profile completed."
else
msg_loggr "db2profile not found"
msg_loggr "db2profile not found" "off"
fi
#Counting the db2diag messages:
msg_loggr "DB2DIAG extraction start date: ${yest_date}"
msg_loggr "DB2DIAG extraction end date: ${today_date}"
msg_loggr "----------------------------------------- "
print "No of Errors messages:" >> ${output_dir}/${output_filename}
print "db2diag -readfile -t ${yest_date}:${today_date} -l Error -c " >> ${output_dir}/${output_filename}
db2diag -readfile -t ${yest_date}:${today_date} -l Error -c >> ${output_dir}/${output_filename}
print "No of Critical messages:" >> ${output_dir}/${output_filename}
print "db2diag -readfile -t ${yest_date}:${today_date} -l Critical -c" >> ${output_dir}/${output_filename}
db2diag -readfile -t ${yest_date}:${today_date} -l Critical -c >> ${output_dir}/${output_filename}
print "No of Severe messages:" >> ${output_dir}/${output_filename}
print "db2diag -readfile -t ${yest_date}:${today_date} -l Severe -c" >> ${output_dir}/${output_filename}
db2diag -t ${yest_date}:${today_date} -l Severe -c >> ${output_dir}/${output_filename}
print "No of Warning messages:" >> ${output_dir}/${output_filename}
print "db2diag -readfile -t ${yest_date}:${today_date} -l Warning -c" >> ${output_dir}/${output_filename}
db2diag -readfile -t ${yest_date}:${today_date} -l Warning -c >> ${output_dir}/${output_filename}
msg_loggr "----------------------------------------- "
#End of execution
if [ -s ${error_dir}/${error_filename} ]; then
msg_loggr "Errors found, please check ${error_dir}/${error_filename}"
else
msg_loggr "No errors found, removing ${error_dir}/${error_filename}"
rm ${error_dir}/${error_filename}
fi
#Deleting older files
delete_old_file
msg_loggr "Deleting older files."
msg_loggr "Script ended."
#Emailing the output file:
if [ -s ${output_dir}/${output_filename} ]; then
send_email "${host_name}-DB2Diag message count report"
fi
#End of script