#!/usr/bin/perl -w

# check_atmoss plugin for NetSaint
# Written by Stanley Hopcroft (Stanley.Hopcroft@ipaustralia.gov.au)
#
# This plugin conducts simulated user search of a TradeMark database
# via the web by getting a cookie and then posting to a fill-out form.
#
# This plugin requires Perl 5 and various perl modules

# $Id: check_atmoss,v 1.1 1999-11-06 18:09:02+11 root Exp root $
# $Log: check_atmoss,v $
# Revision 1.1  1999-11-06 18:09:02+11  root
# Initial revision
#
# Revision 1.3  1999-10-16 17:12:58+10  root
# Same as 1.2 but uses POST and /atmoss/Falcon_Sub.Show_TM_Details
# to get all possible details from the application.
#

# NetSaint Plugin return codes http://www.netsaint.org/dosc/pluginhowto.html

# 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 HTTP::Request::Common qw(GET POST) ;
use LWP::UserAgent ;
use strict ;

my ($tmno, @d, $res, $req, $ok, $debug, $content, $url, $headers, $code,
$message) ;
my ($status_line, $request, $response, $password_string, @tmarks, $i) ;

@tmarks = (3, 100092, 200099, 300006, 400075, 500067, 600076, 700066, 800061) ;

$debug = 0 ;

my $ua = new LWP::UserAgent ;

$ua->timeout(5) ;

# since ATMOSS phase IV (15/10/1999), a user must supply a valid cookie
# with each search request.

# Hence the following transaction to post a request to for a new cookie
# that identifies a non-persistent anonymous user

# see the HTTP::Common class for the definitions of the methods POST & GET
# these are documented in *lwpcook*

$res = $ua->request(POST
'http://external/atmoss/Falcon_Users_Cookies.Run_Create',
          [p_Anon => 'ANON', p_user_type => 'Create New Connection']) ;

$response = $res->as_string if $res->is_success ;
($password_string) = $response =~ m#(PASSWORD=.*)\n# ;

# the cookie looks like EXTID=ANONYMOUS; PASSWORD=\d+\n


$i = 0 ;
$ok = 0 ;
foreach $tmno (@tmarks) {

  $url = "http://external/atmoss/Falcon_Sub.Show_TM_Details",

  push @d, "-- trying $url\n" ;

  # Pass request to the user agent and get a response back
  # NB the form values *must* precede the cookie

  $res = $ua->request(POST $url,
          [p_tmno => "${tmno}", p_ExtDisp => "D", p_Detail => "DETAILED"],
          Cookie => "EXTID=ANONYMOUS; $password_string") ;

  # Check the outcome of the response
  if ($res->is_success) {
    $content =  $res->content;
  }

  $headers = $res->as_string ;
  $status_line = $res->status_line ;

  if ( !defined($content) ) {
    $d[$i] .= "..get returned undef\n..headers..\n$headers\n" ;
  }
  elsif ( $content !~ /we were unable to process your request at this time/i ) {
    $d[$i] .= "$content\n" ;
    $ok = 1 ;
  }
  else {
# $d =~ /we were unable to ./
    $d[$i] .= "$content\n..headers..\n$headers\n" ;
  }
  $i++ ;
}

print @d if !$ok ;

if (!$ok) {
  print "## ATMOSS search failure ## " ;
  exit 2 ;                     # +++ Critical +++
}
print "Searched ATMOSS Ok " ;
exit 0 ;                  #     Ok

