1748072 Members
5510 Online
108758 Solutions
New Discussion юеВ

Scripting Assistance

 
SOLVED
Go to solution
Jay Core
Frequent Advisor

Scripting Assistance

Hi.

Scripting Newbie here. Just looking for a little script to monitor a specific file; when this file goes over a certain size (NOT percent used as in a bdf script), it will send an email. Thanks in advance.

Jay
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Scripting Assistance

It's rather simple. I would probably set it up as a cron job to run every 10 minutes or so:

------------------------------
#!/usr/bin/sh

typeset FNAME=/aaa/bbb/myfile
typeset -i MAXSIZE=1000000
typeset ADDRESS="mickey@mouse.com"
typeset SUBJECT="Filesize Notification"
typese SZ=""

export PATH=${PATH}:/usr/bin

if [[ -f "${FNAME}" ]]
then
SZ=$(ls -l "${FNAME}" | awk '{print $5}')
if [[ ${SZ} -gt ${MAXSIZE} ]]
then
echo "File ${FNAME} size ${SZ} bytes." | mailx -s "${SUBJECT}" ${ADDRESS}
fi
fi

-----------------------------------------
If it ain't broke, I can fix that.
Jeff_Traigle
Honored Contributor

Re: Scripting Assistance

One approach...

#!/usr/bin/sh

FILEDIR=/my/file/path
FILENAME=myfile
FULLPATH=${FILEDIR}/${FILENAME}
WARNSIZE=1024c
FOUND=$(find ${FILEDIR} -name ${FILENAME} -size +${WARNSIZE})

if [ "${FULLPATH}" = "${FOUND}" ]
then
mailx -s "${FULLPATH} Warning" username@company.com <${FULLPATH} has exceeded threshold of ${WARNSIZE}. Take corrective action.
EOM
fi

Obviously, change the dummy values and adjust the WARNSIZE value to fit your needs. Schedule to run via cron at the desired interval.
--
Jeff Traigle
Sandman!
Honored Contributor

Re: Scripting Assistance

You can create a script that is run from cron every so often and sends an email once the size is over the threshold. The test can be as simple as getting the size of the file from the command line by lsting the contents of the directory it is in. For ex. if the notification should be sent after the file size exceeds 1MB then:

# ll -art file_name | awk '$5 > 1048576'
James R. Ferguson
Acclaimed Contributor

Re: Scripting Assistance

Hi Jay:

# cat ./monitor
#!/usr/bin/sh
typeset -i SIZE=$1
typeset -i CURR=0
typeset FILE=$2
while true
do
CURR=`wc -c < ${FILE}`
if [ ${CURR} -gt ${SIZE} ]; then
mailx -s "${FILE} is larger than ${SIZE} characters" root < /dev/null
exit
fi
sleep 10
done

...Run as:

# ./monitor 1000 myfile

...that is, pass the maximum size in characters you want the file to grow to until an email alert is generated (here, to 'root') along with the name of the file that you want monitored.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Scripting Assistance

HI (again) Jay:

I maeant to add:

Adjust the loop timer as you see fit and run the script as a background process or eliminate the loop ('while true') and simply 'cron' the script to run periodically.

Regards!

...JRF...
Jay Core
Frequent Advisor

Re: Scripting Assistance

Beautiful - thanks all.