#! /usr/bin/perl -w
#
use strict;
use vars qw($opt_V $opt_h $opt_S $opt_t $PROGNAME);
use lib "/usr/lib/nagios/plugins";
use utils qw(%ERRORS &print_revision &support &usage);

$PROGNAME="check_zoo";

use LWP::Simple;
use Getopt::Long;

sub print_help ();
sub print_usage ();

$ENV{'PATH'}='';
$ENV{'BASH_ENV'}='';
$ENV{'ENV'}='';


Getopt::Long::Configure('bundling');
GetOptions
        ("V"   => \$opt_V,   "version"    => \$opt_V,
         "h"   => \$opt_h,   "help"       => \$opt_h,
         "S=s" => \$opt_S,   "servername=s" => \$opt_S,
         "t=i" => \$opt_t, "timeout=i"  => \$opt_t);

if ($opt_V) {
        print_revision($PROGNAME,'1.1');
        exit $ERRORS{'OK'};
}

unless (defined $opt_t) {
        $opt_t = $utils::TIMEOUT ;      # default timeout
}


if ($opt_h) {print_help();exit $ERRORS{'OK'};}

unless (defined $opt_S) {
        print "Missing license server\n";
        print_usage();
        exit $ERRORS{'UNKNOWN'};
}
# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
        print "Timeout: No Answer from Server\n";
        exit $ERRORS{'UNKNOWN'};
};
alarm($opt_t);


($opt_S) || ($opt_S = shift) || usage("Server not specified\n");
my $server = $1 if ($opt_S =~ /^(.*)$/);
($server) || usage("Invalid Servername: $opt_S\n");

my %products;
my $total;
my $content;
my $limit;

{
        local $/ = undef;
        $content = get("http://$server/status");
        if (!$content) {print "Unable to connect to $server\n"; exit $ERRORS{'UNKNOWN'};}
}


while ($content =~ /<tr><td>\d*<\/td><td>([a-zA-Z0-9_\- \.\(\)]*)<\/td><td>[a-zA-Z0-9_\- \.\(\)]*<\/td><td>[a-zA-Z0-9_\- \.\(\)]*<\/td><td>(Available|In Use|Checked Out)/g)
{
if ($2 eq "Available")
{
        push(@{$products{$1}}, 0);
}
else
{
        push(@{$products{$1}}, 1);
}
}

$limit = 0;
$total = 0;
my $productusage;

for my $product (keys %products)
{
        my $ava = eval join '+', @{$products{$product}};
        my $tot = scalar(@{$products{$product}});
        $productusage .= "$product:$ava/$tot;\n";
		$limit = 1 if ($ava == $tot);
		$total += $tot;
}




if ($total > 0)
{
   print "$server Server running.";
}
else
{
   print "$server Server NOT running.";
}


# perfdata
print "\n|$productusage";

exit $ERRORS{'OK'} if ( ($total > 0) && ($limit == 0) );
exit $ERRORS{'WARNING'} if ( ($total == 0) || ($limit > 0) );
exit $ERRORS{'CRITICAL'};

sub print_usage () {
        print "Usage:
   $PROGNAME -S <servername> [-t] [-V] [-h]
   $PROGNAME --help
   $PROGNAME --version
";
}

sub print_help () {
        print_revision($PROGNAME,'1.0');
        print "Copyright (c) 2015 Maximilian Thumfart

Check Robert McNeel & Associates Zoo License Servers

";
        print_usage();
        print "
-S, --servername=SERVER
   Name or Address of license server
-t, --timeout
   Plugin time out in seconds (default = $utils::TIMEOUT )
-V, --version
   Show version and license information
-h, --help
   Show this help screen

";

        support();
}

