I found a script online that I thought was going to do what I needed, but I can't get it to work as my PERL skills are pretty low. Basically, I need to monitor this URL on apple.com and make sure the download form is available, and if it isn't available, I need to receive an email saying that the form isn't available from $hostname, here is the traceroute from that host. The traceroute is important because Apple uses Akamai and some GeoIP magic for their downloads.
I'm open to keeping this script and adding on to it or doing it another way. Thanks for taking the time to look at this for me. I'll be sure to share the finished result when I'm done. I'm pretty sure this script will be useful to more than just myself. ;)
EDIT 5/8/2011 I just updated the script to reflect my recent changes.
#!/usr/bin/perl
use strict; use warnings;
# local hostname
my $hostname = `/bin/hostname`;
# setup array of servers/websites to check
my @sitestocheck = ('swdlp.apple.com');
# the relative url of the website response script in each site
my $responseprogram = "/cgi-bin/WebObjects/SoftwareDownloadApp.woa/wa/getProductData?localang=en_us&grp_code=quicktime&returnURL=http://www.apple.com/quicktime/download";
# path to the log file with the response data
my $statusdir = "./tmp";
# mail feature
my $mailprog ='/usr/sbin/sendmail';
my $adminmail = 'root@localhost';
my $frommail = 'root@$hostname';
###############################################################
# End Configuration #
###############################################################
# main program
use Crypt::SSLeay;
use LWP::UserAgent;
# now check each url in your array
foreach my $sitetocheck (@sitestocheck)
{
my $ua = new LWP::UserAgent;
my $req = new HTTP::Request 'GET',"https://$sitetocheck$responseprogram";
my $res = $ua->request($req);
if ($res->is_success)
{
if ($res->content =~ m/Quicktime/i)
{
my $response = "SERVER OK:$sitetocheck:".$res->content;}
else
{
my $response = "Our apologies but there was an unexpected error with the application. This problem has been noted, and an email has been sent to the administrators. Please check back in a few hours to try the download again. ";
}
}
else
{
my $timestamp = localtime;
my $response = "WARNING! $hostname UNABLE TO CONNECT TO $sitetocheck at $timestamp";
my $traceroute = `/usr/sbin/traceroute $sitetocheck`;
}
# write server status to the main log file
open(FILE,">>$statusdir/statuslog.txt");
flock(FILE, 2);
print FILE "$response\n$traceroute\n\n";
flock(FILE, 8);
# write to a current status file for each server or website
# being monitored
open(FILE,">$statusdir/$sitetocheck");
flock(FILE, 2);
print FILE $response;
flock(FILE, 8);
}
# if there is an error mail the administrator
if (my $response =~ m/apologies/i)
{
open( MAIL, "|$mailprog -t" );
print MAIL "Subject: $hostname unable to connect to $sitetocheck\n";
print MAIL "From: $frommail\n";
print MAIL "To: $adminmail\n";
print MAIL "Reply-to: $frommail\n\n";
print MAIL "$response\n$traceroute";
print MAIL "\n\n";
close MAIL;
}
Ok, here's some observations:
Always use:
use strict;
use warnings;
Why chmod 0777
? Does your logfile need to be executable?
$statusfile
does not contain any data.
$traceroute
contains traceroute data, but the data is then replaced with an empty string.
If no traceroute is run, you will have a print with an undefined value in the first open()
, which will cause a warning in perl.
Your second open()
truncates the logfile. Perhaps intentional, but worth mentioning.
The checks that are performed are rather loose. First, the only check performed on the page being valid is that it contains "Quicktime 7.6.9 for Windows XP"
. That could be on any page, even a page saying the system is down. Also, the $response
is checked for the string "WARNING", which obviously comes from the script itself, but is checked case-insensitively, which is just strange. So, a mail is sent out not only if there is an error, but if the word "warning" appears anywhere on the download page. Not really a very good check, IMO.
The $response
text says an email has been sent to the administrators, which it has not.
"/bin/hostname"
the application is not used, only it's name is added to the Subject of the email. If you want it used, you need to use backticks like with traceroute (I would show you, but apparently backticks are a metacharacter in this textfield ;))
The webpage seems to come through ok, I can't test the sendmail since I am on a windows machine, but it looks ok.
It is hard to tell if this fixes your problems, since you do not specify what your problems are. It is a rather crude script, though.