#!/bin/sh
CORE_FILES_DIR=/core_files
FFDC_FILES_DIR=/core_files/ffdc_data
DP_FILES_DIR=/var/log/dpSlot
MNT_DIR=/mnt
SS_PROG=supportsave

# Helper function to check whether SupportSave is currently running or not
check_for_supportsave_in_progress()
{
	pidof $SS_PROG 1>/dev/null 2>&1
	if [ $? -eq 1 ]; then
		# echo "SupportSave not running"
		return 0
	else
		# echo "SupportSave is running"
		return 1
	fi
}

# we don't care if the total core files amounts to less that 5 K bytes
# LOWER_LIMIT will set this limit under with we don't care to report
LOWER_LIMIT=5

# First, if there is no CORE_FIELS_DIR then we are done :-)
if [ ! -d ${CORE_FILES_DIR} ]
then
	exit
fi

# Avoid croncore stuck at console causing OOM
RUN_COUNT=`ps -elf | grep -c croncore`

if [ ${RUN_COUNT} -ge 5 ]
then
	exit
fi
# let us figure out how much core files are occupying...
ROOTkbytes=`/usr/bin/du -sk ${CORE_FILES_DIR} | cut -f1`
MNTkbytes=`/usr/bin/du -sk ${MNT_DIR}/${CORE_FILES_DIR} | cut -f1`
Ffdckbytes=`/usr/bin/du -sk ${FFDC_FILES_DIR} | cut -f1`
MNTFfdckbytes=`/usr/bin/du -sk ${MNT_DIR}/${FFDC_FILES_DIR} | cut -f1`
corekbytes=`expr ${ROOTkbytes} + ${MNTkbytes} - ${Ffdckbytes} - ${MNTFfdckbytes}`
ffdckbytes=`expr ${Ffdckbytes} + ${MNTFfdckbytes}`

ROOTnumberOfCoreFiles=`/usr/bin/find /${CORE_FILES_DIR}/ \( -name "*core*" \) -type f -print | \
wc -l | sed -e's/[ 	]//g'`
MNTnumberOfCoreFiles=`/usr/bin/find ${MNT_DIR}/${CORE_FILES_DIR}/ \( -name "*core*" \) -type f -print | \
wc -l | sed -e's/[ 	]//g'`
numberOfCoreFiles=`expr ${ROOTnumberOfCoreFiles} + ${MNTnumberOfCoreFiles}`

ROOTnumberOfFfdcFiles=`/usr/bin/find /${FFDC_FILES_DIR}/ \( -name "*ffdc*" \) -type f -print | \
wc -l | sed -e's/[ 	]//g'`
MNTnumberOfFfdcFiles=`/usr/bin/find ${MNT_DIR}/${FFDC_FILES_DIR}/ \( -name "*ffdc*" \) -type f -print | \
wc -l | sed -e's/[ 	]//g'`
numberOfFfdcFiles=`expr ${ROOTnumberOfFfdcFiles} + ${MNTnumberOfFfdcFiles}`

# Now figure out if there are any DP core dumps & log files uploaded to CP ...
ROOTNumberOfDPLogFiles=`/usr/bin/find ${DP_FILES_DIR}* -type f 2>/dev/null | wc -l`
MNTNumberOfDPLogFiles=`/usr/bin/find ${MNT_DIR}${DP_FILES_DIR}* -type f 2>/dev/null | wc -l`
ROOTKbytesOfDPLogFiles=`/usr/bin/du -csk ${DP_FILES_DIR}* 2>/dev/null | tail -1 | cut -f1`
MNTKbytesOfDPLogFiles=`/usr/bin/du -csk ${MNT_DIR}${DP_FILES_DIR}* 2>/dev/null | tail -1 | cut -f1`
numberOfDPLogFile=`expr ${ROOTNumberOfDPLogFiles} + ${MNTNumberOfDPLogFiles}`
dplogkbytes=`expr ${ROOTKbytesOfDPLogFiles} + ${MNTKbytesOfDPLogFiles}`

if [ ${numberOfCoreFiles} -ge 1 ]
then
	printf "
*** CORE FILES WARNING (%-20s) ***
%s KBytes in %s file(s)
use \"supportsave\" command to upload 

" "`/bin/date +'%D - %T'`"  ${corekbytes} ${numberOfCoreFiles} > /dev/console
fi

if [ ${numberOfFfdcFiles} -ge 1 ]
then
	printf "
*** FFDC FILES WARNING (%-20s) ***
%s KBytes in %s file(s)
use \"supportSave\" command to upload

" "`/bin/date +'%D - %T'`"  ${ffdckbytes} ${numberOfFfdcFiles} > /dev/console
fi

if [ ${numberOfDPLogFile} -ge 1 ]
then
	# If SupportSave run is already in progress when this cron job detects DP
	# log files present on CP, then do not flash the banner message since those
	# DP log files might have been fetched on CP by SupportSave.
	check_for_supportsave_in_progress
	if [ $? -eq 0 ]; then
		printf "
*** DP LOG OR CORE FILES WARNING (%-20s) ***
%s KBytes in %s file(s)
use \"supportSave\" command to upload

" "`/bin/date +'%D - %T'`"  ${dplogkbytes} ${numberOfDPLogFile} > /dev/console
	fi
fi
