#!/usr/bin/perl -w # Copyright 2006 VMware, Inc. All rights reserved. This script prints all VM's # and hosts under a datacenter ############################################################################# # modified version of datacenterlisting.pl from the VMware VI-Perl toolkit # samples. It queries a host(s) to get storage info status. I believe it will # only work with ESX 3i not the full blown 3.5 version. This is a version # targeted for use within Nagios # CRT 2/22/08 carlos_tronco@mentor.com # use strict; use warnings; use VMware::VIRuntime; $Util::script_version = "1.0"; my %ERRORS=('GREEN'=>0,'YELLOW'=>1,'RED'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4); my %opts = ( datacenter => { type => "=s", help => "Datacenter name", required => 1, }, ); my $verbose = 0; # read/validate options and connect to the server Opts::add_options(%opts); Opts::parse(); Opts::validate(); Util::connect(); # find datacenter my $datacenter = Opts::get_option('datacenter'); if ($verbose) { print"Datacenter = $datacenter\n";} my $datacenter_view =Vim::find_entity_view(view_type => 'Datacenter', filter => { name => $datacenter }); if (!$datacenter_view) { die "Datacenter '" . $datacenter . "' not found\n"; } # get all hosts under this datacenter my $host_views = Vim::find_entity_views(view_type => 'HostSystem', begin_entity => $datacenter_view); # print hosts my $counter = 1; my $host; my $element; my $idx; my $hoststatus; my $hoststatusdesc; if ($verbose) {print "Hosts found:\n";} foreach (@$host_views){ $hoststatus = "Green" ; $hoststatusdesc = ""; if ($verbose) {print "$counter:" . $_->name . "\n";} $counter++; $host = $_; if ($verbose) {print "Boot time " . $_->runtime->bootTime . "\n";} $idx = 0; my $ary =$host->runtime->healthSystemRuntime->hardwareStatusInfo->storageStatusInfo; my $ub = $#$ary; my $worst_ele = "GREEN"; for ( $idx = 0; $idx <= $ub; $idx++){ $element =$host->runtime->healthSystemRuntime->hardwareStatusInfo->storageStatusInfo->[$idx]; my ($ele_name,$ele_status_summary, $ele_status_key, $ele_status_label, $ele_opinfo_prop, $ele_opinfo_val); $ele_name = $element->name; $ele_status_summary =$element->status->summary; $ele_status_label = $element->status->label; if ($ele_status_label ne "Green") { if ($ele_status_label eq "Red") { $worst_ele = uc($ele_status_label); } elsif ( ($worst_ele ne "RED") && ($ele_status_label eq "Yellow")){ $worst_ele = uc($ele_status_label); } elsif (( $worst_ele ne "YELLOW") && ($ele_status_label eq "Unknown")) { $worst_ele = uc($ele_status_label); } #$hoststatusdesc .= "$ele_name has status of $ele_status_label: #[$ele_status_summary]\n"; $hoststatusdesc .= "$ele_name has status of $ele_status_label\n"; }; if ($verbose) { print "$ele_name Has status of $ele_status_label\n";} } if ($worst_ele ne "GREEN") { print $hoststatusdesc; }else { print $host->name . " storage status is GREEN \n"; } exit $ERRORS{$worst_ele}; } exit 0; # disconnect from the server Util::disconnect();