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

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;

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

sub print_usage () {

    print "
Usage:  Check ilmd service.
Options:
    --help
    --version
    --usage
";
    return $ERRORS{'OK'};
}

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

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

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

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

    my $output = "";
    my $return = "";
    my $status = "CRITICAL";

    $ENV{"ILOG_LICENSE_FILE"} = "/var/adm/ilm/access.ilm";
    my $ILMLIST = "/s/cplex-7.1/ilm/ilmlist";

    open (IN, "$ILMLIST |") or die "cannot run $ILMLIST: $!\n";
    while (<IN>) {
        $output .= $_;

        if (/No TOKEN license found/) {
            $output = $_;
            $status = "CRITICAL";
        } elsif (/Token server on .* is UP/) {
            $status = "OK";
        }
    }
    close IN;

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

    exit $ERRORS{$status};
}

