我在網上發現了一個腳本,我認爲它會做我所需要的,但是由於我的PERL技能很低,所以無法工作。基本上,我需要在apple.com上監控這個URL,並確保下載表單可用,如果它不可用,我需要收到一封電子郵件,表明該表單不能從$ hostname獲得,這裏是來自該主機的traceroute。 traceroute非常重要,因爲Apple使用Akamai和一些GeoIP魔術來下載它。如何監控一個URL,然後給自己發送一個跟蹤路由,如果出現問題?
我打算保留這個腳本,並添加到它或做另一種方式。感謝您花時間爲我着想。當我完成後,我一定會分享完成的結果。我很確定這個腳本對我來說不僅僅是有用的。 ;)
編輯 5/8/2011 我剛剛更新了腳本以反映我最近的更改。
#!/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 = '[email protected]';
my $frommail = '[email protected]$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;
}
你說得對,劇本通過電子郵件發送給我,無論頁面是否正常或不好。 ;)另外,目前在日誌文件中顯示的唯一東西是我正在檢查的頁面的源代碼。 1)添加。但是,這會產生大量「全局符號需要顯式包名」的錯誤。 ;)2)刪除。這是來自原始腳本。 3)我沒有看到腳本中的$ statusfile。 4)我認爲這現在是固定的。 5)好的。 6)目前還不確定。 7)見上文。 8)電子郵件在我的Linux機器上工作。無論網站是否正常,我都會收到一封電子郵件。 ;)9)修正。 – geeknik 2011-05-08 01:01:56
1)'use strict'必須聲明變量,例如'我的$ statusdir;'一定要把聲明放在你打算使用它的範圍內,這很可能只是在文件的頂部。 2&3)由於沒有使用,所以最有可能安全移除。 4&5)如果你在上面這樣聲明,你將刪除錯誤:'my $ traceroute =「」;'用'if($ response =〜/^WARNING /)替換郵件檢查'蘋果頁面,不會出現錯誤。或者你知道的東西會出現在錯誤頁面上。 – TLP 2011-05-08 11:14:48
連接到http://工作,但連接到https://沒有。否則,我認爲我已經得到了應有的腳本。 – geeknik 2011-05-08 19:08:20