0

我在网上找到了一个脚本,我认为它可以满足我的需要,但由于我的 PERL 技能很低,我无法让它工作。基本上,我需要在 apple.com 上监控这个 URL 并确保下载表单可用,如果它不可用,我需要收到一封电子邮件,说该表单不能从 $hostname 获得,这里是来自该主机的跟踪路由。traceroute 很重要,因为 Apple 使用 Akamai 和一些 GeoIP 魔法进行下载。

我愿意保留此脚本并添加到它或以其他方式进行。感谢您花时间帮我看这个。完成后,我一定会分享完成的结果。我很确定这个脚本不仅仅对我自己有用。;)

编辑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 = '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;
}
4

1 回答 1

2

好的,这里有一些观察:

  1. 始终使用:

    use strict; use warnings;

  2. 为什么chmod 0777?您的日志文件是否需要可执行?

  3. $statusfile不包含任何数据。

  4. $traceroute包含 traceroute 数据,但随后将数据替换为空字符串。

  5. 如果没有运行 traceroute,您将在 first 中打印一个未定义的值open(),这将导致 perl 中的警告。

  6. 您的第二个open()截断日志文件。也许是故意的,但值得一提。

  7. 执行的检查相当松散。首先,对页面执行的唯一有效检查是它包含"Quicktime 7.6.9 for Windows XP". 这可能在任何页面上,甚至是显示系统已关闭的页面。此外,$response检查字符串“WARNING”,这显然来自脚本本身,但检查时不区分大小写,这很奇怪。因此,不仅会在出现错误时发送邮件,而且在下载页面的任何位置出现“警告”一词时都会发送邮件。不是一个很好的检查,IMO。

  8. $response文本说已经向管理员发送了一封电子邮件,但它没有。

  9. "/bin/hostname"该应用程序未使用,仅将其名称添加到电子邮件的主题中。如果你想使用它,你需要使用像 traceroute 这样的反引号(我会告诉你,但显然反引号是这个文本字段中的一个元字符;))

该网页似乎可以通过,我无法测试 sendmail,因为我在 Windows 机器上,但它看起来还可以。

很难说这是否能解决您的问题,因为您没有具体说明您的问题是什么。不过,这是一个相当粗糙的脚本。

于 2011-05-07T23:18:13.253 回答