# -*- perl -*-
#
# $Revision: 1.1 $
package winvmware;

require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(&print_usage &print_version &service_test);
use lib $main::runtimedir;
use csl_utils qw($TIMEOUT %ERRORS);
use strict;
use Nagios::Config;

sub print_usage ();
sub print_version ();
sub service_test (%);

sub usage () {
    my $print = shift;
    print $print;
    print_usage();
}

sub print_usage () {

    print "
Usage:  Check windows service.
Options:
    -H <hostname> -P <portnumber> [-d <disk usage> -u <memory usage> -t <uptime min,max> --verbose]
               (uptime - warn if less than or greater than alloted time)
    --help
    --version
    --usage
";
    return $ERRORS{'OK'};
}

sub print_version () {
    my $version = '$Revision: 1.1 $';
    $version =~ s/^\$Revision: //;
    $version =~ s/ \$\s*$//;

    print "windows.pm - $version\n";
    return $ERRORS{'OK'};
}

sub service_test (%) {
    my %options = @_;

    my $bindir = "/s/nagios/libexec";
    my $check_nt = "$bindir/check_nt";

    my @disk = (101, 101);
    my @memory = (101, 101);
    my @uptime = (0, 10000);
    my $WhatData;

    if (! -f $check_nt) {
        print "Cannot find check_nt at $check_nt\n";
        return $ERRORS{'UNKNOWN'}
    }

    # Load up all the new values and test them


    if ($options{"verbose"}) {
        foreach my $opt (sort keys(%options)) {
            if ($options{$opt}) {
                print "$opt - $options{$opt}\n";
            }
        }
    }

    my $host = $options{"opt_H"};

    if ((!$host) || ($host eq "")) {
        print "\nHost name not specified.\n";
        &print_usage();
        return $ERRORS{'OK'};
    }

    my $port = $options{"opt_P"};

    ($port) || ($port = 1248);

    my @CHECKS;
    push @CHECKS, "$check_nt -H $host -p $port -v SERVICESTATE -l vspherewebclientsvc,ADAM_VMwareVCMSDS,vimPBSM,vctomcat,vpxd,VMUSBArbService,vimQueryService -t 30"; 

    my %revERRORS;

    foreach my $error (sort keys(%ERRORS)) {
        $revERRORS{$ERRORS{$error}} = $error;
    }

    my $output = "";
    my $progstatus = $ERRORS{'OK'};
    my $status;

    $SIG{'ALRM'} = sub {
        print "CRITICAL: Checks not responding: $output\n";
        exit $ERRORS{'CRITICAL'};
    };
    alarm($TIMEOUT-1);

    foreach my $check (@CHECKS) {
        my $com_output = `$check`;
        $status  = $? >> 8;

        if ($check =~ /UPTIME/) {
            (my $uptime = $com_output) =~ s/.*System Uptime : ([^#]*) #.*/$1/;
            $_ = $uptime;
            my ($day, $hour, $min) = /(\d+) day.+ (\d+) hour.+ (\d+) minute.+/;
            if ($min) {
                $uptime = ($day*24) + $hour;
            }
            $status = $ERRORS{"WARNING"} if (($uptime < $uptime[0]) || ($uptime > $uptime[1]));
        } else {
            ($com_output) = split ('\|', $com_output);
        }

        chomp $com_output;

        if ($status > $progstatus) {
            $progstatus = $status;
            $output = $revERRORS{$status} . ": $com_output #" . $output;
        } else {
            $output .= $revERRORS{$status} . ": $com_output #";
        }
    }

    # nagios perf log can no longer handle the '\' char - probably something
    # to do with the printf - don't care right now.
    $output =~ s/\\//g;
    $output =~ s/\|/#/g;

    print "$output\n";
    print "Exit status: $status\n" if $options{"verbose"};

    exit $progstatus;
}

