Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languageperl
titlecheck_acknowledge.pl
collapsetrue
#!/usr/bin/perl -w

use strict;
use NetLWP::UserAgent;
use HTTP::Request::Common;
use Getopt::Long;
use vars qw($opt_H $opt_f $opt_s $opt_p $opt_t $opt_h);
use vars qw(%ERRORS &support);
my $host;
my $type;
my $service;
my $port;
my $timeout = 30;
our %ERROR;
%ERRORS =   (
            'OK'       => 0,
            'CRITICAL' => 2,
            'ERROR'    => 2,
            'UNKNOWN'  => 9,
              'WARNING'  => 1,
            );


sub print_help ();
sub print_usage ();

Getopt::Long::Configure('bundling');
GetOptions
   ("h"   => \$opt_h, "help"        => \$opt_h,
    "H=s" => \$opt_H, "hostname=s"  => \$opt_H,
    "f=s" => \$opt_f, 
    "s=s" => \$opt_s, "service=s"   => \$opt_s,
    "t=i" => \$opt_t, "timeout=i"   => \$opt_t,
    "p=i" => \$opt_p, "port=i"      => \$opt_p);

if ($opt_h) {print_help(); exit 0;}

if ($opt_H ) {
    if ( $opt_H =~ /([-.A-Za-z0-9]+)/ ) {
      $host = $opt_H;
    }
    ($host) || print("Invalid host: $opt_H\n");
}
else {
    print("Host name/address not specified\n");
} 

if ($opt_p ) {
    if ($opt_p =~ /([0-9]+)/) {
        $port = $1 if ($opt_p =~ /([0-9]+)/);
    }
    ($port < 0 || $port > 65535) && print("Invalid Port: $opt_p\n");
}
else {
    print("Port not specified\n");
}

if ($opt_t) { $timeout = $opt_t; }

if( !$host || !$port ) {
    print_usage();
    exit 1;
}

$opt_s=~ s/ /%20/g;
print("service name:$opt_s\n");

# $job_scheduler_request output example.
# "JobScheduler Monitoring Error" is the example of the service name $opt_s.
#<add_order  job_chain   ="sos/notification/ResetNotifications"
#            #<add_order  job_chain   ="/sos/notification/ResetNotifications"
#            id          ="op5 JobScheduler Monitoring Error acknowledgement"
#            title       ="op5 JobScheduler Monitoring Error acknowledgement">
#    <params>
#        <param name="service_name"  value="JobScheduler Monitoring Error" />
#        <param name="system_id"     value="op5"/>
#        <param name="operation"     value="acknowledge" />
#     </params>
#</add_order>
my $job_scheduler_request = "%3Cadd_order%20job_chain=%22sos/notification/ResetNotifications%22%20id=%22op5%20".$opt
_s."%20acknowledgement%22%20title=%22op5%20 $message = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><add_order job_chain=\"/sos/notification/ResetNotifications\" id=\"op5 ".$opt_s." Acknowledegment\" title=\"op5 ".$opt_s."%20acknowledgement%22%3E%3Cparams%3E%3Cparam%20name=%22syst
em_id%22%20value=%22op5%22/%3E%3Cparam%20name=%22service_name%22%20value=%22 Acknwoledgement\"><params><param name=\"system_id\" value=\"MonitorSystem\"/><param name=\"service_name\" value=\"".$opt_s."\"%22/%3E%3Cparam%20name=%22
operation%22%20value=%22acknowledge%22/%3E%3C/params%3E%3C><param name=\"operation\" value=\"acknowledge\"/></params></add_order%3Eorder>";

if($opt_f=~m/ACKNOWLEDGEMENT/){
    my $jobscheduler_answer_xml = get_answer($job_scheduler_request);
    my $jobscheduler_state_element = get_state_elem($jobscheduler_answer_xml);
    my $jobscheduler_state = get_attribute_value("state",$jobscheduler_state_element);
    _report('OK', "OK: Service Name is " . $opt_s . " and notification type is ". $opt_f ." and JS request is ".
 $job_scheduler_request. "\n");
}
else{print("Sorry, but this is not an acknowledgement\n");}

sub get_attribute_value {
    my ($attr_name, $elem_xml) = @_;
    $elem_xml =~ s/.*$attr_name\s*=\s*\"(.*?)\".*/$1/s;
    return $elem_xml;
}

sub get_state_elem {
    my $xml = shift;
    $xml =~ s/.*<spooler.*?>\s*<answer.*?>\s*(<state.*?>).*/$1/s;
    return $xml;
}

sub get_answer {
  my $request = shift;
  my $socket = Net::HTTP->new(Host => $host, PeerPort => $port, Timeout => $timeout);
  my $xmlAnswer = "";
    if ($socket) {
        $socket->write_request(GET => $request);
        my($code, $mess, %h) = $socket->read_response_headers;
        if ($code == 200) {
            while (1) send_request($message);
}
else{ print("Please set notification type to ACKNOWLEDGEMENT\n");}

sub send_request {
    my $message       my $buf;= shift;
  
    my $userAgent = LWP::UserAgent->new(agent => 'perl post');
  my $n = $socket->read_entity_body($buf, 1024$userAgent->timeout($timeout);
  
    my $response = $userAgent->request(POST 'http://'.$host.':'.$port,Content_Type  last unless $n;
    => 'text/xml',Content => $message);
    if ($response->is_success) {
        $xmlAnswer#$answer .= $buf$response->as_string;
            }
        }
    _report('OK', "OK: Service name: ".$opt_s."\nNotification type: ".$opt_f."\nRequest: ". $message); 
    } 
    else {
            _report('ERROR',"ConnectionERROR: toService JobSchedulername: " . $host . ":" . $port . " failed: (" . $code . ") " .
$mess . .$opt_s."\nNotification type: ".$opt_f."\nRequest: ". $message."\n\nAnswer:\n".$response->error_as_HTML."\n");
    }
}

sub get_attribute_value {
   }
  }
  else {my ($attr_name, $elem_xml) = @_;
    $elem_xml _report('CRITICAL',"Connection to JobScheduler " . $host . ":" . $port . " failed: " . $@ . "\n");
  }
  return $xmlAnswer;
}

  =~ s/.*$attr_name\s*=\s*\"(.*?)\".*/$1/s;
    return $elem_xml;
}

sub get_state_elem {
    my $xml = shift;
    $xml =~ s/.*<spooler.*?>\s*<answer.*?>\s*(<state.*?>).*/$1/s;
    return $xml;
}       

sub print_help () {
   print $0. "\n";
   print "Copyright (c) 20122015 SOS GmbH, info\@sos-berlin.com

This script tries to connect to given Job Scheduler

";
   print_usage();
   print "
-H, --hostname=HOST
   Name or IP address of host to check
-p, --port=INTEGER
   Port at host to check
-t, --timeout=INTEGER
   Timeout for HTTP connetion
-f =STRING
   Notification type, e.g. ACKNOWLEDGEMENT
-s, --service=STRING
   Service name, e.g. JobScheduler Errors
-h, --help
   This help
";
}

sub print_usage () {
   print  "Usage: $0 -H <host> -p <port> -f ACKNOWLEDGEMENT -s <service name> [-t <timeout>]\n";
}

sub _report { 
    print $_[1]; 
  if (defined($ERRORS{$_[0]})) { exit $ERRORS{$_[0]}; }
  else { exit 0; }
}

...