#!/usr/bin/perl # You may need to change this path to /usr/local/bin/perl $mailprog = "/usr/sbin/sendmail"; #************************************************************** # # Script to notify you of changes in specified directories: PWSmonitorchanges V2.0 # # Written by: # Premier Website Solutions - http://www.premierwebsitesolutions.com # Created - in 2003 (V1.0) # Modified - September 12, 2004 (V2.0) # - rewrote script to make usable by others # Last Modified - November 30, 2004 (V2.1) # - now also gives owner and group of modified files(*) # # (*) features not in free version # Join my scripts club for $10 US and get access to full features of all scripts # www.premierwebsitesolutions.com/scripts/scriptsclub.pws # # # Set a few variables below, list your directories to monitor in the directories.txt # file, upload this script and the directories.txt file to anywhere on your server, # then set a cron job to run the script every 15 minutes. I put mine in a subfolder # of the servers scripts folder. (/scripts/custom) # # To set the cronjob, in shell, type crontab -e, then enter what's between the quotes # on the following line as a new line in your cron listings: # "*/15 * * * * /scripts/custom/PWSmonitorchanges.cgi >/dev/null 2>&1" # # Ownership and permissions of the script should be root:root, and 0700, unless you # want to enable wheel users to use it, then it would be root:wheel 0770. # # If you set it to run at a different frequency, change the $minutes variable below # to match. # # # Registered users of this script will be notified of any future updates. # If you registered this copy with me, put your email here for future reference. # This copy is registered to: # #************************************************************** #******** The first 3 variables are the only ones you need to set. ********# #******** The rest are for those who feel like taking more control. ********# # This is where the email will be sent when a change is detected. # This script is useless until you put your email address here. # If you use spamassassin, you should include a name, like this: # $sendto_email = 'me '; $sendto_email = ''; # This is the sender for the email message. # Change it if you wish. $sender_email = 'Mike '; # This is the path to where you put this script and the directories.txt file. $path = "/scripts/custom"; # The script finds files altered within this many minutes. It should match the # frequency of the cron job setting. What happens is, the script runs every 15 # minutes and checks for files added or altered within the last 15 minutes. $minutes = 15; # This is the maximum number of directories deep, (from the starting point), # that the "find" command will go. $maxdepth = 6; # List directories or files to ignore here. Put directories between /'s, otherwise # any file or directory that has that in it's name will be ignored. # The ones here are samples and probably a good starting point. # Here are the current settings explanations. # '/tmp/', - so the script won't be triggered by changes to the tmp directories # '/mail/', - so the script won't notify me every time someone gets email # '/.spamassassin/' - so the script won't notify me when someone makes spamassassin changes @ignores = ( '/tmp/', '/mail/', '/.spamassassin/' ); # These are the file extensions that are being watched for. You can add as many as you want. # Files with no extension are automatically watched. # A future version will allow the choice to watch all extensions. @exts = ( 'cgi', 'pl', 'php', 'php3', 'php4', 'shtml', 'htm', 'html', 'xml', 'xhtml', 'db' ); # To edit either the @ignores or the @exts list, just be sure each value is between # single quotes and there is a comma after all but the last one. #------------------------------------------------------------------- #------------------------------------------------------------------- # UNLESS YOU KNOW CGI, DO NOT EDIT ANYTHING BELOW HERE # Feel free to study the code, but alterations are at your own risk #------------------------------------------------------------------- #------------------------------------------------------------------- open(datafile,"$path/directories.txt")||print "can't"; @dirs = ; close(datafile); system("mkdir $path/tempdata"); foreach $dir (@dirs) { chomp($dir); $dirpath = $dir; $dirpath =~ s/\//_/g; $report = ""; $count = 0; system("find $dir -type f -maxdepth $maxdepth -mmin -$minutes -fprint $path/tempdata/$dirpath.txt"); open (INF,"$path/tempdata/$dirpath.txt"); @files=; close(INF); system("chmod 0600 $path/tempdata/$dirpath.txt"); foreach $file (@files) { chomp($file); foreach $ext (@exts) { if ($file =~ /$ext$/i) { $nocount = ""; foreach $ignore (@ignores) { if ($file =~ /$ignore/i) {$nocount = "y"} } if ($nocount ne "y") { $count++; $report = $report . "$file\n"; } } } if ($file !~ /\./) { $nocount = ""; foreach $ignore (@ignores) { if ($file =~ /$ignore/i) {$nocount = "y"} } if ($nocount ne "y") { $count++; $report = $report . "$file\n"; } } } if ($count > 0) { # Open The Mail Program open(MAIL,"|$mailprog -t"); print MAIL "To: $sendto_email\n"; print MAIL "From: $sender_email\n"; print MAIL "Subject: A change has been made at $dir\n"; print MAIL "The following files have been modified:\n\n"; print MAIL "$report\n"; print MAIL "\n\n"; close (MAIL); } } system ("rm -Rf $path/tempdata"); exit;