#!/usr/bin/perl -w 

###################################
#
# Written by Ahsan Md Sajid Khan
#      
# This script will return ESXI Guest Host's Inventory.  
# 
#
###################################

use strict;
use Net::SNMP;
use Getopt::Long;

use lib "/usr/lib/nagios/plugins/";
use utils qw($TIMEOUT %ERRORS);


my $HOST_NAME = '1.3.6.1.4.1.6876.2.1.1.2';
my $HOST_OS = '1.3.6.1.4.1.6876.2.1.1.4';
my $ALLOCATED_RAM = '1.3.6.1.4.1.6876.2.1.1.5';
my $POWER_STATUS = '1.3.6.1.4.1.6876.2.1.1.6';
my $RUNNING_STATUS = '1.3.6.1.4.1.6876.2.1.1.8';
my $ALLOCATED_CPU = '1.3.6.1.4.1.6876.2.1.1.9';
my $TOTAL_MEM = '1.3.6.1.4.1.6876.3.2.1';
my $hrDeviceDescr = '1.3.6.1.2.1.25.3.2.1.3';
my $SOCKET ='1.3.6.1.4.1.6876.3.1.1';


my $returnstring = "";

my $returnvalue = $ERRORS{"OK"};

my $total_allocated_memory = 0;
my $total_allocated_cpu = 0;
my $total_memory = 0;
my $total_cpu = 0;
my $summary = "";
my $socket_num=0;


my $host = 	undef;
my $community = undef;
my $version = undef;
my $help = undef;

Getopt::Long::Configure ("bundling");
GetOptions(
	'h' => \$help,
	'help' => \$help,
	'H:s' => \$host,
	'host:s' => \$host,
	'C:s' => \$community,
        'v:s' => \$version,
	'community:s' => \$community
);

sub nonum
{
  my $num = shift;
  if ( $num =~ /^(\d+\.?\d*)|(^\.\d+)$/ ) { return 0 ;}
  return 1;
}
sub print_usage
{
    print "Usage: $0 -H <host> -C <COMMUNITY-STRING> -v <SNMP Version 1 or 2>\n";
}

# CHECKS
if ( defined($help) )
{
	print_usage();
	exit $ERRORS{"UNKNOWN"};
}
if ( !defined($host) )
{
	print "Need Host-Address!\n";
	print_usage();
	exit $ERRORS{"UNKNOWN"};
}
if ( !defined($community) )
{
	print "Need Community-String!\n";
	print_usage();
	exit $ERRORS{"UNKNOWN"};
}

if ( !defined($version) )
{
        print "Need SNMP Version 1 or 2\n";
        print_usage();
        exit $ERRORS{"UNKNOWN"};
}




my ($session, $error) = Net::SNMP->session( -hostname  => $host, -version   => $version, -community => $community, -timeout   => $TIMEOUT);

if (!defined($session)) {
   printf("ERROR opening session: %s.\n", $error);
   exit $ERRORS{"CRITICAL"};
}

my $host_name = $session->get_table(-baseoid => $HOST_NAME);
if (!defined($host_name))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}

my $host_os = $session->get_table(-baseoid => $HOST_OS);
if (!defined($host_os))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}

my $ram = $session->get_table(-baseoid => $ALLOCATED_RAM);
if (!defined($ram))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}

my $power_status = $session->get_table(-baseoid => $POWER_STATUS);
if (!defined($power_status))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}

my $running_status = $session->get_table(-baseoid => $RUNNING_STATUS);
if (!defined($running_status))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}

my $cpu = $session->get_table(-baseoid => $ALLOCATED_CPU);
if (!defined($cpu))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}

my $actual_mem = $session->get_table(-baseoid => $TOTAL_MEM);
if (!defined($actual_mem))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}

$total_memory = $$actual_mem{$TOTAL_MEM.".0"};
$total_memory = $total_memory/1024/1024;
$total_memory = sprintf "%.0f",$total_memory;

my $socket = $session->get_table(-baseoid => $SOCKET);
if (!defined($socket))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}

$socket_num = $$socket{$SOCKET.".0"};


my $device_descr = $session->get_table(-baseoid => $hrDeviceDescr);
if (!defined($device_descr))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}

########################################################################

my @device_pools = keys %$device_descr;

for (my $i =1; $i <= @device_pools; $i++)

{

 my @name = split (/ /,  $$device_descr{$device_pools[$i-1]});
 #printf($name[0]."\n"); 

  if ($name[0] eq 'CPU')
   
     {
      $total_cpu=$total_cpu+1;
     }

}



##########################################################################


my @host_pools = keys %$host_name;

for(my $pool = 1; $pool <= @host_pools; $pool++)
{




       # my @name = split (/:/,  $$storage_name{$storage_pools[$pool-1]});

        #     $returnstring .=" "."Memory Name : ".$name[0]; # delete it

        my $id = ((split(/\./, $host_pools[$pool - 1])) [-1]);
        my $os = $$host_os{$HOST_OS.".".$id};
        my $mem_size = $$ram{$ALLOCATED_RAM.".".$id};
        my $power = $$power_status{$POWER_STATUS.".".$id};
        my $running = $$running_status{$RUNNING_STATUS.".".$id};
        my $cpu_num = $$cpu{$ALLOCATED_CPU.".".$id};
        $mem_size = $mem_size/1024;
        
        if ($power eq 'powered on')
        {
        $total_allocated_cpu = $total_allocated_cpu + $cpu_num;
        $total_allocated_memory = $total_allocated_memory + $mem_size;
        }
        
 
        $returnstring .= "VM-HOST => [".$power."-".$running."] ".$$host_name{$host_pools[$pool-1]}.", HOST-OS => ".$os.", Allocated Memory => ".$mem_size."GB, Allocated CPU => ".$cpu_num." \n";


}

#$returnstring=$returnstring."|".$perf_data;

$summary = "Total Host=".@host_pools.", CPU[Allocated(PW-ON)=".$total_allocated_cpu.", Total CPU=".$total_cpu.", Socket=".$socket_num."], Memory[Allocated(PW-ON)=".$total_allocated_memory."GB, Total Memory=".$total_memory."GB ]";

if ($returnvalue == $ERRORS{"CRITICAL"})
{
	print "CRITICAL: ".$returnstring."\n";
	exit $returnvalue;
}


if ($returnvalue == $ERRORS{"WARNING"})
{
	print "WARNING: ".$returnstring."\n";
	exit $returnvalue;
}

print "Summary: ".$summary." \n".$returnstring."\n";
exit $returnvalue;
