如何监控 URL,然后在出现问题时通过电子邮件给自己发送跟踪路由?

发布于 2024-11-06 04:32:51 字数 2874 浏览 0 评论 0原文

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

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

编辑 2011 年 5 月 8 日 我刚刚更新了脚本以反映我最近的更改。

#!/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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾`听者〃 2024-11-13 04:32:51

好的,这里有一些观察结果:

  1. 始终使用:

    使用严格;
    使用警告;

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

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

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

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

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

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

  8. $response 文本表示电子邮件已发送给管理员,但实际上尚未发送。

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

网页似乎通过正常,我无法测试sendmail,因为我在 Windows 机器上,但看起来没问题。

很难判断这是否解决了您的问题,因为您没有指定您的问题是什么。不过,这是一个相当粗糙的脚本。

Ok, here's some observations:

  1. Always use:

    use strict;
    use warnings;

  2. Why chmod 0777? Does your logfile need to be executable?

  3. $statusfile does not contain any data.

  4. $traceroute contains traceroute data, but the data is then replaced with an empty string.

  5. 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.

  6. Your second open() truncates the logfile. Perhaps intentional, but worth mentioning.

  7. 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.

  8. The $response text says an email has been sent to the administrators, which it has not.

  9. "/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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文