Dear Monk,

I have written a crontab Perl program to check whether the company website is available for the customers. We would like to know if there is a problem before the users call to report it.

My code is running on Perl 5.6.1 under RedHat Linux AS v2.1. I am using lwp-request to hit the website.

I am finding that the response time varies at certain times of the day and my timeout causes false failures. I have increased the timeout, but I would like to capture the HTTP error code and know whether it is a timeout or something else.

Here is the code. Thanks in advance!

#!/usr/bin/perl -w # /usr/local/bin/webalert.pl [mail address] [web address] use strict; use LWP; local *MAIL; # get the mail address if there is one. my $mailaddr = shift @ARGV; # get the web address if there is one. my $webaddr = (shift @ARGV ||"www.perlmonks.org"); my $timeout = 15; if (system "/usr/bin/lwp-request -t $timeout $webaddr >/dev/null 2>&1" +) { sendmail_or_print() } sub sendmail_or_print { # get the mail address if there is one. my $hostname = "from ". (`hostname 2>/dev/null` ||"who"); # get the mail address if there is one. if ($mailaddr) { open MAIL, "|mail -s \"Web Alert $hostname\" $mailaddr"; } else { open MAIL, ">&STDOUT"; } print MAIL "\n\n$webaddr NOT available: " . localtime() ."\n\n"; print MAIL "$timeout second timeout used.\n\n"; close MAIL; }

In reply to website availability ping by Lhamo Latso

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.