Someone asked me how to do this, so here you go. Schedule this to run once every 5 minutes (or whatever you set $MYFREQUENCY to) and it will email $MYEMAIL if it detects any changes or new files.
Code:
#!/bin/bash
# Description: This script monitors $MYDIR (checking once every $MYFREQUENCY minutes) for
# files that have recently been created or modified, and if it finds one, it sends a logger
# message to LOGHOST, and/or an email to $MYEMAIL.
MYDIR=/dir/to/monitor
MYEMAIL='username@domain.com'
MYFREQUENCY='-5'
MESSAGEFILE="message.txt"
MYDATE=$(date +%y-%m-%d)
MYTIME=$(date +%H:%M)
MAILER=$(which mutt)
MYNAME=$(hostname)
MYSUBJECT="Data Changed in $MYDIR directory on host ${MYNAME}"
if [[ ! -f ${MAILER} ]] ; then
echo "Program \"mutt\" is required, but is not available on this host. Please install mutt package and try again."
exit 1
fi
FILECOUNT="$(find ${MYDIR} -mmin ${MYFREQUENCY} -type f | wc -l)"
if [ ${FILECOUNT} -gt 0 ]
then
echo "Directory ${MYDIR} on host ${MYNAME} has been modified:" >> /tmp/${MESSAGEFILE}
echo "Filename : Timestamp : Size in bytes" >> /tmp/${MESSAGEFILE}
find $MYDIR -mmin ${MYFREQUENCY} -type f -printf '%p : %Ac : %s bytes *** ' >> /tmp/${MESSAGEFILE}
logger -p local0.info "CHANGE DETECTED: $(cat /tmp/${MESSAGEFILE})"
cat /tmp/${MESSAGEFILE} | mutt -s "CHANGED DETECTED in host ${MYNAME}" -a /tmp/${MESSAGEFILE} ${MYEMAIL}
/bin/rm -f /tmp/${MESSAGEFILE}
fi
exit 0