#!/usr/bin/perl -w

# check_internet plugin for NetSaint
# Written by Stanley Hopcroft (Stanley.Hopcroft@ipaustralia.gov.au)
#
# This plugin uses a proxy web server to determine the availability of our
# access to the 'Web by getting start pages from famous search engines.
#
# This code relies on the LWP Perl module to do the work.
#

# Numeric Value Service Status Status Description
# 0             Ok
#               The plugin was able to check the service and it appeared to
#               be functioning properly

# 1             Warning
#               The plugin was able to check the service, but it appeared
#               to be above some "warning" threshold or did not appear to
#               be working properly

# -1            Unknown
#               Invalid command line arguments were supplied to the
#               plugin or the plugin was unable to check the status of the
#               given hosts/service

# 2             Critical
#               The plugin detected that either the service was not running
#               or it was above some "critical" threshold


use strict ;
use LWP::UserAgent ;
use HTTP::Request::Common ;

my $test = 0 ;

my @h = ("www.patents.ibm.com", "www.altavista.com", "www.whitepages.com.au",
     "www.wipo.org", "www.lycos.com", "www.netscape.com",
     "www.hotbot.com", "google.com", "www.anzwers.com",
     "www.uspto.gov", "www.nlsearch.com", "www.dogpile.com") ;

my (@hu_t, @hu_p) ;

# ibm & netscape via Melbourne
# others & indeed half the world via Sydney

foreach (@h) {
  push(@hu_t, "NonExistentPage.html") ;
  push(@hu_p, "index.html") ;
}

my @hu = $test ? @hu_t : @hu_p ;

my $h_r = \@h ;
my $h_ur = \@hu ;


my %hs = ( "internet" => {
          "http" => $h_r,
          "url" => $h_ur,
          "check" => \&check_internet,
          },
) ;

my $ok = 1 ;

my $ua = new LWP::UserAgent;

$ua->timeout(5) ;

$ua->proxy(['http', 'ftp'] => 'http://firewall.aipo.gov.au');


foreach my $to_check ( keys %hs ) {
  my $narrative = "" ;

  ($ok, $narrative) = &{ $hs{$to_check}->{"check"} }($to_check) ;

}


if (!$ok) {
#              +++ Critical +++
  print "Failed to get start pages from Internet Search engines\n" ;
  print "narrative\n" ;
  exit 2 ;
}
#                  Ok
print "Internet Ok\n" ;
exit 0 ;


sub check_internet {

#  print "check with check_internet" ;

my ($to_check) = @_ ;
my ($narrative, $http, $url, $uri, $status_line, $http_headers) ;
my (@http, @url, $req, $res, $content) ;
my $ok = 0 ;

$narrative = "" ;

@http = @{ $hs{$to_check}->{"http"} } ;
@url = @{ $hs{$to_check}->{"url"} } ;

foreach $http (@http) {
  $url = shift @url ;
  $uri = "$http/$url" ;
  print " .. trying to head $uri ..\n" if $test ;

  $req = HEAD "http://$uri",
               Pragma => 'no-cache',
               Host => "$http" ;

# Pragma 'no-cache' ensures that intermediate caches do not supply the
# URI.

  $req->proxy_authorization_basic("Proxy_UserName", "Proxy_PassWord");

  $res = $ua->request($req);
  $content = $res->content ;
  $http_headers = $res->headers->as_string ;
  $status_line = $res->status_line ;
  print " .. got $status_line ..\n" if $test ;

  $narrative .= join("\n", $uri, $http_headers, $status_line, "\n" ) ;

  $res->is_success && ($ok = 1) ;
}

return ($ok, $narrative) ;

}