#! /opt/perl-5.8.6/bin/perl -w
use strict;

use Getopt::Long;
use vars qw($opt_V $opt_v $opt_h $opt_e $opt_H $opt_p $opt_r $opt_a $opt_c $PROGNAME);
use lib "/usr/local/nagios/libexec";
use utils qw(%ERRORS &print_revision &support &usage);
use XML::XPath;
use Net::HTTP;
use MIME::Base64;

my %config;                 # hash array for sections contained in configuration file $profile

#---------------------------------------------------------------------------------------------trim

sub trim
{
    my $str = shift;

    if( defined $str )
    {
        $str =~ s/^\s*//;
        $str =~ s/\s*\r?\n?$//;
    }

    return $str;
}

#--------------------------------------------------------------------------------------load_profile

sub load_profile
{
    my $section;

    open INI, $opt_c or die "could not open configuration file: $opt_c";

    while( my $line = <INI> )
    {
        $line = trim $line;

        if( $line =~ /^\[(.+)\]/ )
        {
            $section = lc $1;
        }
        elsif( defined $section  &&  $line =~ /=/ )
        {
            chomp $line;
            ( my $entry, my $value ) = split( /=/, $line );
            $config{ "[$section]" . lc(trim($entry)) } = trim( $value )   if defined $value;
            # $spooler_log->debug9("settings entry found: ".lc(trim($entry))." = ".trim($value)) if defined $value;
        }
    }

    close INI;
}

#--------------------------------------------------------------------------------------read_profile

sub read_profile
{
    my $section = shift;
    my $entry   = shift;

    return $config{ "[" . lc($section) . "]" . lc($entry) };
}

sub read_config
{
    load_profile();
    $opt_H || ($opt_H = read_profile('soap.request', 'host'));
    $opt_p || ($opt_p = read_profile('soap.request', 'port'));
    $opt_e || ($opt_e = read_profile('soap.request', 'endpoint'));
    $opt_a || ($opt_a = read_profile('soap.request', 'authorization'));
}

$PROGNAME = "check_scheduler.pl";

sub print_help ();
sub print_usage ();

Getopt::Long::Configure('bundling');
GetOptions
   ("V"   => \$opt_V, "version"     => \$opt_V,
    "v"   => \$opt_v, "verbose"     => \$opt_v,
    "h"   => \$opt_h, "help"        => \$opt_h,
    "e=s" => \$opt_e, "endpoint=s"  => \$opt_e,
    "H=s" => \$opt_H, "hostname=s"  => \$opt_H,
    "r"   => \$opt_r, "reset"       => \$opt_r,
    "c=s" => \$opt_c, "configuration=s"    => \$opt_c,
    "a=s" => \$opt_a, "authorization=s"    => \$opt_a,
    "p=i" => \$opt_p, "port=i"      => \$opt_p);

if ($opt_V) {
   print_revision($PROGNAME,'$Revision: 0.2 $');
   exit $ERRORS{'OK'};
}

if ($opt_h) {print_help(); exit $ERRORS{'OK'};}

if ($opt_c) {
    read_config();
}

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

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

($opt_e) || usage("Endpoint name/address not specified\n");
my $endpoint = $1 if ($opt_e =~ /([-_\/.A-Za-z0-9]+)/);
($endpoint) || usage("Invalid endpoint: $opt_e\n");

my $reset = '';
if ($opt_r) {
   $reset = '<param><name>type</name><value>reset</value></param>';
}

my $verbosity = 0;
if ($opt_v) {
   $verbosity = 1;
}

#print "opt_V:$opt_V,opt_v:$opt_v,opt_h:$opt_h,opt_e:$opt_e,opt_H:$opt_H,opt_r:$opt_r,opt_c:$opt_c,opt_a:$opt_a,opt_p:$opt_p\n";

sub print_usage () {
   print "Usage: $PROGNAME -H <host> -p <port> -e <endpoint> -c <config.ini> -a <user:pass> [--reset]\n";
}

sub print_help () {
   print_revision($PROGNAME,'$Revision: 0.2 $');
   print "Copyright (c) 2006 SOS GmbH, info\@sos-berlin.com

This plugin reports the state of the Job Scheduler

";
   print_usage();
   print "
-H, --hostname=HOST
   Name or IP address of host to check
-p, --port=INTEGER
   Port at host to check
-e, --endpoint=PATH
   Endpoint (ignored by scheduler)
-c, --configuration=FILE
   Read configuration from file
-a, --authorization=USER:PASS
   Use these credentials for authorization
-r, --reset
   Reset the schedulers error state
";
}

sub _report { print $_[1]; exit $ERRORS{$_[0]}; }

my $head = '<?xml version="1.0" encoding="ISO-8859-1"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header><wsa:To xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">http://www.sos-berlin.com/monitor_service</wsa:To><wsa:ReplyTo xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"><wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address></wsa:ReplyTo></soapenv:Header><soapenv:Body><addOrder xmlns="http://www.sos-berlin.com/scheduler">';
my $tail = '</addOrder></soapenv:Body></soapenv:Envelope>';
my $soap_request = $head . $reset . $tail;
my %header = ('User-Agent' => 'check_scheduler.pl/0.2');

if ($opt_a) {
    $header {'Authorization'} = 'Basic ' . encode_base64($opt_a, '');
}

my $buf;
my $soap_response = '';
my $socket = Net::HTTP->new(Host => $host, PeerPort => $port);

if (! $socket) {
   _report('CRITICAL', $@);
}

if (! $socket->write_request(POST => $endpoint, %header, $soap_request)) {
   _report('CRITICAL', $@);
}

my($code, $mess, %h) = $socket->read_response_headers;
if ($code < 200 || $code >= 300) {
   _report('CRITICAL', "$code $mess\n");
}

while (1) {
   my $n = $socket->read_entity_body($buf, 1024);
   die "read failed: $!" unless defined $n;
   last unless $n;
   $soap_response .= $buf;
}
#print $soap_response;
my $errors = 0;
my $warnings = 0;
my $service_status = 'INVALID';

my $xmlRoot = XML::XPath->new($soap_response);
my $nodeSet = $xmlRoot->find('//messages');

if ( $nodeSet-> size == 1) {
   my $result = 'UNKNOWN';
   my $node = $nodeSet->get_node(1);
   $errors = $node->find('@errors')->string_value;
   $warnings = $node->find('@warnings')->string_value;
   if ($errors > 0) {
      $result = 'CRITICAL';
      $service_status = 'ERRORS';
   } else {
      if ($warnings > 0) {
         $result = 'WARNING';
         $service_status = 'WARNINGS';
      } else {
         $result = 'OK';
         $service_status = 'OK';
      }
   }
   my $buf = '';
   if ($verbosity > 0) {
      my $messages = $xmlRoot->find('//message');
      my $size = $messages->size;
      for (my $i = 1; $i <= $size; $i++) {
         $buf .= "<br>" . $messages->get_node($i)->string_value;
      }
   }
   _report($result, "$service_status: Job Scheduler reports Errors: $errors, Warnings: $warnings $buf\n");
} else {
   _report('OK', "OK: Job Scheduler is up and running\n");
}