
FishMonger
Veteran
Dec 21, 2008, 6:53 AM
Views: 4020
|
|
Re: [sat110] Limit alerts to 3 max
|
|
|
Kevin did a good job of showing how to limit it to 3 emails. So, now lets look at improving the overall quality of the script. To begin with, every Perl script you write should include the warnings and strict pragmas which will point out common mistakes that are sometimes difficult to track down. You could simplify the retrieval of the web page with LWP::Simple which uses LWP::UserAgent under the hood. There really isn't any need to use the $found when checking for the date in the web page. Instead, move your checking of the count and email section into that spot. Using the code Kevin gave is ok and is a common approach, but I think it could be improved with the use of the File::CounterFile module.
#!/usr/bin/perl use strict; use warnings; use LWP::Simple; use File::CounterFile; # Store the page in the string $webpage my $webpage = get('http://www.yourdomain/test.html'); # Check if the requested webpage is there unless ( $webpage ) { # you might want to create a log entry exit; } # the date is passed to the script as an argument # and I really should add some error handling to this part my $date = shift; if ( $webpage =~ m|$date| ) { my $counter = File::CounterFile->new('/fullpathto/alert-count.txt', 0); if ( $counter->value <= 3 ) { $counter->inc; # personally, I'd use MIME::Lite to send the email # but this method is common and ok. my $mailprog = '/usr/sbin/sendmail -oeq -t'; my $recipient = 'test@yahoo.net'; my $sender = 'test@gmail.com'; open (MAIL, "|$mailprog"); print MAIL "To: $recipient\n"; print MAIL "From: $sender\n"; print MAIL "Subject: Schedule Page Updated or down\n\n"; print MAIL "NEW Basketball schedule may be posted."; print MAIL "Must Call ASAP."; close (MAIL); } } Keep in mind that when you modify the date that you search for, you also need to reset the counter either by manually editing the counter file or simply by deleting the file.
(This post was edited by FishMonger on Dec 21, 2008, 7:13 AM)
|