File Directory Change Monitoring
1) Purpose:
We want to check out a unix directory, signaling with an email if its content change.
2) Solution:
Before modify appropriately the configuration parameters inside, then run in background mode (sh filedirChangeMonitoring.sh &) the following shell script.
#!/bin/bash
#
# filedirChangeMonitoring.sh
# DATE : 2014-07-30
# DESC.:
# Script for the monitoring of files in a directory
# The script checks if changes are made to the files of a directory and it alerts with an e-mail
# Before running the script modify the following parameters: TO_ADDRESS, FROM_EMAIL_ADDRESS, xDirMon and xTime
# PARENT SCRIPT: none
#
# NOTE:
# The script should be run in background mode as follow: sh filedirChangeMonitoring.sh &
# The script uses these following support files that should not be deleted or modified in order to not distort the monitoring:
# email_text.txt, testdira.txt and testdirb.txt
# The script requires the sendmail service active.
# (ps -ef| grep filedirChangeMonitoring and kill -9 PID)
# Configuration script parameters
#
# Recipients and sender of the email
TO_ADDRESS=”pippo@mail.com”
FROM_EMAIL_ADDRESS=”pluto@mail.com”
CC_ADDRESS=””
BCC_ADDRESS=””
# Hostname where the script is executed
xHostName=$(hostname –fqdn)
# Email subject
xSubject=”Dir Monitoring Script Notification $(date ‘+%Y/%m/%d %H:%M:%S’) of $xHostName”
# Directory where filedirChangeMonitoring.sh resides
DIR_SCRIPT=”$(pwd)”
# Monitored directory
xDirMon=”/home/mydir”
# File containing the text of the email to be sent
xMAIL_to_SEND=”$DIR_SCRIPT/email_text.txt”
# Control time interval in sec.
xTime=300
# Date command example that returns date and time
# date ‘+DATE: %Y/%m/%d%nTIME:%H:%M:%S’
exitcode=0
# Checks if the monitored directory exists
#
if !(ls $xDirMon) &> /dev/null; then
echo “Monitored directory not existent”
exit 3
fi
# Checks if the contents of the monitored directory has been changed
#
touch $DIR_SCRIPT/testdirb.txt
while true
do
ls -la $xDirMon > $DIR_SCRIPT/testdira.txt
if [ $(diff $DIR_SCRIPT/testdira.txt $DIR_SCRIPT/testdirb.txt| wc -l) -gt 0 ]; then
echo “To: $TO_ADDRESS” > $xMAIL_to_SEND
echo “From: $FROM_EMAIL_ADDRESS” >> $xMAIL_to_SEND
# echo “Cc: $CC_ADDRESS” >> $xMAIL_to_SEND
# echo “Bcc: $BCC_ADDRESS” >> $xMAIL_to_SEND
echo “Subject: $xSubject” >> $xMAIL_to_SEND
diff $DIR_SCRIPT/testdira.txt $DIR_SCRIPT/testdirb.txt >> $xMAIL_to_SEND
# Mail send command
sendmail -v $TO_ADDRESS < $xMAIL_to_SEND
fi
cp $DIR_SCRIPT/testdira.txt $DIR_SCRIPT/testdirb.txt
sleep $xTime
done
if [ $exitcode -lt 3 ]; then
exit 0
else
exit 3
fi
3) Example:
Suppose to have the rigths rwx on /home and /home/mydir directories.
3.1) Create in /home the filedirChangeMonitoring.sh script
3.2) Modify the configuration parameters as follow:
TO_ADDRESS=”pippo@mail.com”
FROM_EMAIL_ADDRESS=”pluto@mail.com”
xDirMon=”/home/mydir”
xTime=300
3.3) Run sh filedirChangeMonitoring.sh &
3.4) Run in /home/mydir the touch pluto.txt command
3.5) Below the email text received after the writing of the pluto.txt file into the monitored directory /home/mydir
2c2
< drwxr-xr-x 3 user1 user1 28672 Jul 31 14:08 .
—
> drwxr-xr-x 3 user1 user1 28672 Jul 31 13:32 .
4d3
< -rw-rw-r– 1 user1 user1 0 Jul 31 14:08 pluto.txt