#!/usr/bin/perl # WebPage WatchDog (WPWD) # Author: Oliver Gerards (ogerards@cc86.org) # # v1.0, 05.04.2002 (original version) # v1.1, 06.04.2002 (additional check for empty lines in config file) # # This script should be called as a cron job. # It will check all URLs specified by a user in a .wpwd file in his home # directory. If the corresponding web page has changed, the user is notified # by mail. # # Each new entry in the config file must be in a separate line and should have # the following form: # URL|0 (the URL followed by a pipe character and a zero) # e.g. http://www.cc86.org/|0 use LWP::Simple; use Mail::Mailer; $domain = "cc86.org"; # you may want to change this to your domain $cfgname = ".wpwd"; # the name of the config file # find all config files open CFGFILES, "find /home -name $cfgname |"; foreach $cfgfile ( ) { chomp $cfgfile; $cfgfile =~ /^\/home\/(.*)\/.*$/i; my $user = $1; print "[WPWD] Processing '$cfgfile': "; my $errurls = ""; my $errcount = 0; my ($url, %Date); # hash containing dates of web pages my ($url, %StoredDate); # hash containing locally stored dates of web pages my $err = 0; open CONFIGFILE, "<$cfgfile" or $err = 1; if ($err == 1) { print "failed\n"; next; } # read config file while ( ) { chomp; if (length $_ > 1) { my ($url, $value) = split(/\s*\|\s*/, $_, 2); $StoredDate{$url} = $value; } } close CONFIGFILE; # get last modification dates for all URLs foreach $url ( keys %StoredDate ) { $err = 0; @header = head($url) or $err = 1; if ($err == 0) { $Date{$url} = $header[2] || -1; # unknown } else { # an error occured when opening this URL $errurls .= $url."\n"; $errcount++; } } my $count = 0; my $changedurls = ""; # compare all dates, add corresponding URL to string if date has changed foreach $url ( keys %Date ) { if ( $Date{$url} > $StoredDate{$url} ) { $changedurls .= $url."\n"; $count++; $StoredDate{$url} = $Date{$url}; } } if ($count != 0) { # create notification mail... if ($count > 1) { $body = "Die folgenden Webseiten haben"; } else { $body = "Die folgende Webseite hat"; } $body .= " sich geändert:\n\n$changedurls\n\n"; if ($errcount > 0) { $body .= "Die folgende(n) URL(s) konnte(n) nicht gecheckt werden:\n\n$errurls\n"; } $body .= "---\n"; # ...and send it $mailer = Mail::Mailer->new(); $mailer->open({ From => "wpwd@$domain", To => "$user@$domain", Subject => "WPWD-Benachrichtigung", }) or die "Fatal Error! Can't open: $!\n"; print $mailer $body; $mailer->close(); # write changes to config file open CONFIGFILE, ">$cfgfile"; print CONFIGFILE map { "$_|$StoredDate{$_}\n" } keys %StoredDate; close CONFIGFILE; } print "done\n"; } close CFGFILES; # #