#! /usr/bin/perl -w

# check_esxvm - Checks the VMs registered and their state on an ESX host
#
# License Information:
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# $Id: check_esxvm.pl,v 1.0 2007/09/11
#
############################################################################

#use Data::Dumper;
use strict;
use POSIX;
use Getopt::Long;
use lib  "/usr/local/nagios/libexec" ;
use utils qw(%ERRORS &print_revision &support &usage );

use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c $opt_t $opt_M $mailq $status $state $msg $msg_q $msg_p $opt_W $opt_b @lines $vmlist %vmlist @vmlist $blacklist @blacklist $NagiosState $blackliststatus);

sub print_help ();
sub print_usage ();
sub process_arguments ();

$state = $ERRORS{'UNKNOWN'}; 
$ENV{'PATH'}='';
$ENV{'BASH_ENV'}='';
$ENV{'ENV'}='';

$blackliststatus=1;

my $message = "List of VMs: ";
  
###################
sub print_usage () {
        print "Usage:   check_esxvm [-b <blacklisted VMS>] [-t <timeout>] [-v verbose]\n";
	print "Example: check_esxvm -b VM-w2000,VM-Debian\n\n"
}

sub print_help () {
        print "Copyright (c) 2007 Gallig Renaud (HP)\n";
        print "\n";
        print_usage();
        print "\n";
        print "Checks the VMs registered and their state on an ESX host\n";
        print "  -b (--blacklist) = Blacklisted VMs (the ones you don't really want to monitor)\n";
        print "  -t (--timeout)   = Plugin timeout in seconds (default = 15)\n";
        print "  -h (--help)\n";
        print "  -V (--version)\n";
        print "\n\n";
}

sub process_arguments(){
	my $version="v1.0";
	my $i=0;
	my $j=0;

        GetOptions
                ("V"   => \$opt_V, "version"     => \$opt_V,
                 "h"   => \$opt_h, "help"        => \$opt_h,
                 "b=s" => \$opt_b, "blacklist=i" => \$opt_b,
                 );

        if ($opt_V) {
                print "Revision: $version\n";
                exit $ERRORS{'OK'};
        }

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

        if (defined $opt_b) {
	my @wishlist=split(',', $opt_b);
	foreach(@wishlist){
                if (grep(/$wishlist[$i]/i, @vmlist)) {
                        $blacklist[$j]= $wishlist[$i];
			$j=$j+1;
                }else{ 
			$blackliststatus=0;
                }
		$i=$i+1;
		}
        }
        return $ERRORS{'OK'};
}

###################
	
  my $OverallStatus = 1;
  my $j=0;
  my $i=0;
  my @vms;
  my $showvms;

  $showvms = `/usr/bin/vmware-cmd -l 2>&1`;
  foreach (split(/\n/, $showvms)) {
    @vms=split ("/",$_);
    $vmlist[$j]=$vms[-1];
    $j=$j+1;
  }

  Getopt::Long::Configure('bundling');
  $status = process_arguments();
  if ($status){
        print "ERROR: processing arguments\n";
        exit $ERRORS{"UNKNOWN"};
  }

  $showvms = `/usr/bin/vmware-cmd -l 2>&1`;
  foreach (split(/\n/, $showvms)) {
    my $vmstate = `/usr/bin/vmware-cmd $_ getstate 2>&1`;
    chomp($vmstate);
    @vms=split ("/",$_);
    my @state=split (" = ",$vmstate);

    if ($state[-1] eq 'off') {
      my ($v)=split(/\./,$vms[-1]);
      if (grep (/$v/i,@blacklist)) {
	$message = "$message / $vms[-1] : $state[-1] (OK-BL)";
      } else {
	$message = "$message / $vms[-1] : $state[-1] (KO)";
	$OverallStatus=0;
      }
    } else {
	$message = "$message / $vms[-1] : $state[-1] (OK)";
    }
  }

  if ( ($OverallStatus == 0) && ($blackliststatus == 0)) {
    print "VMs WARNING - Blacklist Warning - $message";
    $NagiosState = $ERRORS{'CRITICAL'};
  } elsif ( ($OverallStatus == 0) && ($blackliststatus == 1))  {
    print "VMs WARNING - $message";
    $NagiosState = $ERRORS{'WARNING'};
  } elsif ( ($OverallStatus == 1) && ($blackliststatus == 1))  {
    print "VMs OK - $message";
    $NagiosState = $ERRORS{'OK'};
  } else {
    print "You should'nt see this message.";
    $NagiosState = $ERRORS{'UNKNOWN'};
  }

exit $NagiosState;
