...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#!/usr/bin/perl -w
use strict;
use Net::HTTP;
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");
#<add_order job_chain ="sos/notification/ResetNotifications"
# id ="OP5 MyServiceName acknowledegment"
# title ="OP5 MyServiceName acknowledegment">
# <params>
# <param name="service_name" value="MyServiceName" />
# <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."%20acknowledegment%22%20title=%22OP5%20".$opt_s."%20acknwoledgement%22%3E%3Cparams%3E%3Cparam%20name=%22syst
em_id%22%20value=%22op5%22/%3E%3Cparam%20name=%22service_name%22%20value=%22".$opt_s."%22/%3E%3Cparam%20name=%22
operation%22%20value=%22acknowledge%22/%3E%3C/params%3E%3C/add_order%3E";
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) {
my $buf;
my $n = $socket->read_entity_body($buf, 1024);
last unless $n;
$xmlAnswer .= $buf;
}
}
else {
_report('ERROR',"Connection to JobScheduler " . $host . ":" . $port . " failed: (" . $code . ") " .
$mess . "\n");
}
}
else {
_report('CRITICAL',"Connection to JobScheduler " . $host . ":" . $port . " failed: " . $@ . "\n");
}
return $xmlAnswer;
}
sub print_help () {
print $0. "\n";
print "Copyright (c) 2012 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
-h, --help
This help
";
}
sub print_usage () {
print "Usage: $0 -H <host> -p <port> [-t <timeout>]\n";
}
sub _report {
print $_[1];
if (defined($ERRORS{$_[0]})) { exit $ERRORS{$_[0]}; }
else { exit 0; }
}
|
...