File Directory Change Monitoring

File Directory Change Monitoring
  • 12
    Ago

File Directory Change Monitoring

twittergoogle_pluslinkedin

1) Obiettivo:
Vogliamo controllare una directory unix, segnalando via e-mail se il suo contenuto cambia.

2) Soluzione:
Modificare opportunamente i parametri di configurazione, quindi lanciare in background (sh filedirChangeMonitoring.sh &) il seguente 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) Esempio: Supponiamo di avere di permessi rwx sulle directory /home e /home/mydir. 3.1) Creaiamo in /home lo script filedirChangeMonitoring.sh 3.2) Modifichiamo i parametri di configurazione come segue: TO_ADDRESS=”pippo@mail.com” FROM_EMAIL_ADDRESS=”pluto@mail.com” xDirMon=”/home/mydir” xTime=300 3.3) Lanciamo sh filedirChangeMonitoring.sh & 3.4) Lanciamo, in /home/mydir, il comando touch pluto.txt 3.5) Qui sotto il testo dell’e-mail ricevuta dopo la scrittura del file pluto.txt all’interno della directory monitorata /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