Wednesday, March 8, 2017

Using inotify to detect file chages and send a notification by PHP

First of all we need to increase the inotify watchers, by default in Linux environments this value is very low. Check this Increase inotify watchers. Next let's create a watcher to always keep watching a folder(/temp/watch), save the change in a log(watchlog.log) and send an email by PHP. By default when inotify find a change the program just close until this is run again.
There is an option -d (demon but I haven't tested)
1. Create a folder called "watchlog"(/path/to/watchlog) and create the script to watch in the folder "/temp/watch"

inotify_folder.sh
#!/bin/bash

log_dir="log/"
log_file="watchlog.log"

while true
do
ts=$(date +"%C%y%m%d")
OUTPUT="$(inotifywait -r -e modify,create,delete --format '%T %:e %w%f' --timefmt '%c' /temp/watch  | awk '{print $4" "$6" "$7}' )"
#$4 time of change
#$6 event
#$7 file path
echo "${OUTPUT}" >> $log_dir$ts$log_file
php inotify_php.php $OUTPUT
done
inotifywait is using the parameter -r to watch recursive, if there are a lot of level and files inotify can take a while to start.
the paremter -e specify wich actions we want to watch, there more actions by default.
2. Create the folder /log in the same level to save the logs. 3. Create the script that will be call by the cron to check if the script 'inotify_folder.sh' is running

inotify_cron.sh
#!/bin/bash

running=`ps auxf | grep inotify_folder | grep -v grep`
if [ -z "$running" ]
then
    /bin/bash inotify_folder.sh
fi
4. Create the PHP script to send the email

  inotify_php.php
<?php
$receivers = array('user@mail.com');
$time = $argv[1];
$event = $argv[2];
$file = $argv[3];

foreach($receivers as $receive){
	mail($receive,'Alert on file '.$file.' with a '.$event,$event.' '.$file);
}

5. Set a cron to check every one minute
*/1 * * * * root cd /path/to/watchlog; sh inotify_cron.sh

No comments:

Post a Comment