...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#!/usr/bin/perl -w use strict; use LWP::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;} #<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 $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." Acknwoledgement\"><params><param name=\"system_id\" value=\"MonitorSystem\"/><param name=\"service_name\" value=\"".$opt_s."\"/><param name=\"operation\" value=\"acknowledge\"/></params></add_order>"; if($opt_f=~m/ACKNOWLEDGEMENT/){ send_request($message); } else{ print("Please set notification type to ACKNOWLEDGEMENT\n");} sub send_request { my $message = shift; my $userAgent = LWP::UserAgent->new(agent => 'perl post'); $userAgent->timeout($timeout); my $response = $userAgent->request(POST 'http://'.$host.':'.$port,Content_Type => 'text/xml',Content => $message); if ($response->is_success) { #$answer = $response->as_string; _report('OK', "OK: Service name: ".$opt_s."\nNotification type: ".$opt_f."\nRequest: ". $message."\n\nAnswer:\n".$response->as_string."\n"); } else { _report('ERROR',"ERROR: Service name: ".$opt_s."\nNotification type: ".$opt_f."\nRequest: ". $message."\n\nAnswer:\n".$response->error_as_HTML."\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 print_help () { print $0. "\n"; print "Copyright (c) 2015 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; } } |
...