Category: utility scripts
Author/Contact Info chimni@yahoo.com
Description: This script is used to check wether a url is availaible or not .
Perl's LWP module has been used
The script has functionality for direct access as well as authenticated access through a proxy server.
The HTTP get returns a status 200 if everything is ok .
THE TIMEOUT VALUE HAS BEEN SET AS 60 SECONDS IF URL DOES NOT RESPOND WITHIN THAT TIME IT WILL BE CONSIDERED A FAILURE
Pardon the lack of POD,used a general commenting style.
#!/usr/bin/perl  -w



#==========================USAGE======================================
# Usage: $0 URL
# Options: -p <proxy> Proxy Server, e.g. http://proxy.dlh.st.com:8080
#                -u <user>  Proxy Server username
#                -a <auth>  Proxy Server authentication
#                -t <sec>   Timeout in seconds (default is 60 secs)
#                -d <debug> Bebug code level
#                

#e.g. perl web.pl http:\\xxx.yy.zz.com (for intranet sites)

#=====================================================================
+=

#======================Perl module used===============================
+===
#used Getopt::Long also for parsing the command line parameters

use LWP;

#=====================================================================
+===

#======================Script Specific Variables======================
+===

#$recient is the person you want to send the message to
#$list is the list of people in cc
#alerts can be sent to multiple people

my $recipients = "";
my $list = "";
my $sender = "";

#=====================================================================
+===

my($DEBUG,$proxy,$user,$pwd,$timeout,$VPO)= &get_options;


#script works with or without proxy access depending on your environme
+nt.
#The C<LWP::UserAgent> is a class implementing a simple World-Wide Web
#user agent in Perl.
#The request() method can process the content of the response in one o
+f
#three ways: in core, into a file, or into repeated calls to a
#subroutine.  You choose which one by the kind of value passed as the
#second argument to request().
#The in core variant simply stores the content in a scalar 'content' a
+ttribute
#of the response object and is suitable for small
#HTML replies that might need further parsing

if ($proxy eq '') 
 {
  print " No Proxy Server specified\n" if ($DEBUG);
  $proxy='<noproxy>';
  $ua=LWP::UserAgent->new;
  $ua->timeout($timeout) if(defined($timeout));
  $req=HTTP::Request->new(GET=> $url);
  $res= $ua->request($req);
  print "status=",$res->status_line,"\n" if($DEBUG);
  print "is_success=",$res->is_success,"\n" if($DEBUG);
 }
else
#if a proxy server is specified then we use a http GET

 {
 print "Using $proxy as the proxy server \n" if ($DEBUG);
 $ua=LWP::UserAgent->new;
 $ua->proxy('http' => $proxy); 
 $ua->timeout($timeout) if(defined($timeout));
 $req=HTTP::Request->new(GET=> $url);
 $req->proxy_authorization_basic($user,$pwd) if($user ne '' && $pwd ne
+ '');
 $res= $ua->request($req);
 print "status=",$res->status_line,"\n" if($DEBUG); 
 print "is_success=",$res->is_success,"\n" if($DEBUG);
 print "GET result=",$res->as_string,"\n" if($DEBUG>1);
}

#checking if the result of the request was successful or not
if($res->is_success)
 {
  print "passed \$res->is_success\n" if ($DEBUG);
  print "$url,$proxy,SUCCESS\n";
  exit 0;
 }
 else
#if it is no successfult you can either call a mailing function with r
+ecipent,cc,sender,subject,message  as parameters
 {
 $err=$res->status_line;
 $notify_err="$url,$proxy,FAILED,ERROR:$err";
 print $notify_err."\n";
 
#======================Mail specific varables=========================
+=====

     my $sub = "URL $url is not available";
     my $msg = "URL $url is not available \n\n PROXY SERVER : $proxy \
+n\n STATUS = FAILED \n\n ERROR : $err\n\n Regards.";

#=====================================================================
+=====
     sendmail($recipients,$list,$sender,$sub,$msg);

   
 }
exit 0;

#=================Sub routine to parse the command line parameters====
+=============
#Default timeout has been specified as 60 seconds
#checking for blank proxy or username password is done
#Set code level to 1 ( -d 1) see usage if you want to see the options 
+taken in from the command line
#=====================================================================
+=============

sub get_options {
   my($DEBUG,$Proxy,$proxy,$user,$pwd,$timeout,$VPO);
   $proxy=$Proxy=$user=$pwd='';
   $timeout=60;
   $DEBUG=0;
   $VPO=0;
   
   use Getopt::Long;
   
   GetOptions ( "d=i" => \$DEBUG,
                "p=s" => \$proxy,
                "u=s" => \$user,
                "a=s" => \$pwd,
                "t=i" => \$timeout
 );
  
   print "proxy = $proxy ,user = $user pwd = $pwd \n" if($DEBUG);
   $url = $ARGV[0] || &usage;
 
   if( ( $user ne '' && $pwd eq '' ) || ( $pwd ne '' && $user eq ''))
      {
        print STDERR "If you specify a  user id  (-u) for the proxy se
+rver ,you must also specify  an authorization password (-a) and vice-
+verca\n";
        &usage;
        exit 1;
      }

   if(($pwd ne '' || $user ne '') && ($proxy eq '' && $Proxy eq '')) 
      {
        print STDERR "You specified user/password but no proxy server.
+\n";
        print STDERR "The user/password info will be ignored.\n";
      }
    if($DEBUG)
      {
        print "get_options:DEBUG=$DEBUG\n";
        print "            proxy=$proxy, user=$user, pwd=$pwd\n";
        print "            timeout=$timeout (sec)\n";
        print "            url=$url\n";
      }

 return($DEBUG,$proxy,$user,$pwd,$timeout,$VPO);
}

#========================Subroutine to send alert vie Sendmail========
+==========
#sends a mail to the id in $recipients with cc to the id's in $list 
#=====================================================================
+==========

sub sendmail
    {

  my($recipients,$list,$sender,$sub,$msg) = @_;
  open(MAIL, "|/usr/lib/sendmail -t");
  $old=select(MAIL) ;
  print<<"EOF" ;
To: $recipients
CC: $list
From: $sender
Subject: $sub

Body:

   $msg
EOF

close(MAIL);
select($old);
}


#=======================Sub routine to show usage of the script=======
+======================
#notice absolute path names for both the web page to be checked as wel
+l as the proxy server 
#=====================================================================
+=======================

sub usage {
       print "Usage: $0 URL \n";
       print <<'EOF';
       URL to be retrieved , e.g. http://www.yahoo.com
       Options: -p <proxy> Proxy Server, e.g. http://proxyabc.dlh.st.c
+om:8080
                -u <user>  Proxy Server username
                -a <auth>  Proxy Server authentication
                -t <sec>   Timeout in seconds (default is 60 secs)
                -d <debug> Bebug code level

       Prints URL ,Proxy Server (if used),Status
       Alert Notification scheme depends on the -m switch
       Sets exit code to 1 on error and 0 on success
EOF
exit 0;
}
Replies are listed 'Best First'.
Re: URL monitoring (on ur LAN or on the internet vie a proxy server)
by b10m (Vicar) on Jan 06, 2004 at 15:28 UTC

    TIMTOWDI, as usual. In this case, you can narrow the 197 lines of code down to ... ermmm 1 :)

    If you just want to print the output to the screen:

    $ URL="http://www.b10m.net";STATUS=$(HEAD -d $URL);echo "$URL: $STATUS +"

    This would result in:
    http://www.b10m.net: 200 OK

    But if you do like to email the output to, say, "john@doe.invalid", you could do it like this:

    $ URL="http://www.b10m.net";STATUS=$(HEAD -d $URL);echo "$URL: $STATUS +" | mail -s "$URL checked" john@doe.invalid

    This is without using a proxy, though. If you do want to use a proxy, check the manual page of HEAD:

           -p <proxy-url>
               Set the proxy to be used for the requests.  The pro­
               gram also loads proxy settings from the environment.
               You can disable this with the "-P" option.
    
    --
    b10m