Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am developing a script that monitors directory sizes. The script send emails and pages when certain percentage thresholds are reached. My question is: how to implement functionality to stop paging and sending email after 3 times. The script will run via a cron job every 15 mins.
#!/bin/ksh
# This script monitors directory size and emails/pages support personnel when certain threshold sizes are exceeded.host=`/bin/hostname`
sysdate=`/bin/date`
COM_DIR=/home/commun
PG_SENT_FILE=${COM_DIR}/pg_sent_file
COM_FILE=${COM_DIR}/comm_file
LOG_DIR=/home/logs
LOG=${LOG_DIR}/monitor.log
#var=`df /var | tail -1 | tr -s ' ' ' ' | cut -d' ' -f4 | cut -d'%' -f1`
title1="is > 95%"
title2="is > 90%"
title3="is > 85%"
title4="is > 80%"#Remove any old temporary files
rm -f ${LOG}*.tmp#Keep one copy of the previous log file
if [[ -f ${LOG} ]]
then
mv -f ${LOG} ${LOG}.old 2>${LOG}.$$.tmp
if (( $? !=0 ))
then
exit 1
fi
rm -f ${LOG}.$$.tmp
fi. ${COM_FILE}
. ${PG_SENT_FILE}
function check
{
dir=$1
per=`df -k ${dir} | tail -1 | tr -s ' ' ' ' | cut -d' ' -f4 | cut -d'%' -f1`;if [ ${per} -ge 95 ]
then
echo "\n**PROBLEM DIRECTORY IS ${dir} **" >> ${LOG}
echo "`df -k ${dir}`" >> ${LOG}
echo "`df -k ${dir}`" | mailx -s "Alert! ${dir} on ${host} ${title1}" "${MAIL_LIST[*]}" "${PIN_LIST[*]}" 2>> ${LOG}
if (( $? !=0 ))
then
exit 1
fi
return 0
fi
if [ ${per} -ge 90 ]
then
echo "\nPROBLEM DIRECTORY IS ${dir} **" >> ${LOG}
echo "`df -k ${dir}`" >> ${LOG}
echo "`df -k ${dir}`" | mailx -s "Alert! ${dir} on ${host} ${title2}" "${MAIL_LIST[*]}" "${PIN_LIST[*]}" 2>> ${LOG}
if (( $? !=0 ))
then
exit 1
fi
return 0
fi
if [ ${per} -ge 85 ]
then
echo "\n**PROBLEM DIRECTORY IS ${dir} **" >> ${LOG}
echo "`df -k ${dir}`" >> ${LOG}
echo "`df -k ${dir}`" | mailx -s "Alert! ${dir} on ${host} ${title3}" "${MAIL_LIST[*]}" 2>> ${LOG}
if (( $? !=0 ))
then
exit 1
fi
return 0
fiif [ ${per} -ge 80 ]
then
echo "\n**PROBLEM DIRECTORY IS ${dir} **" >> ${LOG}
echo "`df -k ${dir}`" >> ${LOG}
echo "`df -k ${dir}`" | mailx -s "Alert! ${dir} on ${host} ${title4}" "${MAIL_LIST[*]}" 2>> ${LOG}
if (( $? !=0 ))
then
exit 1
fi
return 0
fi
}
status=0
echo "\n ${sysdate}" >> ${LOG}check "/var";
if (( $? !=0 ))
then ${status}=1
ficheck "/tmp";
if (( $? !=0 ))
then ${status}=1
fi
check "/home";
if (( $? !=0 ))
then ${status}=1
fi
check "/";
if (( $? !=0 ))
then ${status}=1
ficheck "/logs";
if (( $? !=0 ))
then ${status}=1
ficheck "/cddata";
if (( $? !=0 ))
then ${status}=1
ficheck "/production";
if (( $? !=0 ))
then ${status}=1
fifor i in `ls /logs`
do
check "/logs/$i";
if (( $? !=0 ))
then ${status}=1
fi
donefor j in `ls /cddata`
do
check "/cddata/$j";
if (( $? !=0 ))
then ${status}=1
fi
doneif [ "${status}" = 1 ]
then mailx -s "Error in threshold monitor script" "${MAIL_LIST[*]}"
if (( $? !=0 ))
then
echo "Email notification of script failure unsuccessful-writing to log." >> ${LOG}
fi
fi
exit 0

I'd suggest keeping the number of emails/pages sent in a file.When you run this script, if no page is sent, you write the number zero to the file. If a page is sent, you read the file's current number, increment it and write it back to the file.
A couple of questions to answer:
1-when do I read this number? If I read it and it's already three, do I want to do the check anyway or do I want to just abort the script?
2-once the threshold is reached, then how do I handle things?
You might do something like:-if a problem is found and count >3 and ((count-3) % 8 != 0) (meaning it's less than two hours since last page), then increment count but don't page
-if a problem is found and count >3 and ((count-3) %8 == 0) (it's been two hours since last page), then increment and page (or maybe page someone else)
Lotsa possibilities

1-Read the number of pages sent already before function check is run. Abort the script if #of pages sent already is 3.
2-Once the threshold is reached email and pages are sent to a list of people.
I'm having the most difficulty with developing the syntax of the code which writes out to a file and increments by 1 each time a threshold is reached.

![]() |
unix password reset
|
dual booting windows+unix
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |