exmultipathinfo.pl
Copyright © 2008 VMware, Inc. All rights reserved.
#!/usr/bin/perl -w
#
# Copyright 2008 VMware, Inc. All rights reserved.
#######################################################################################
# DISCLAIMER. THIS SCRIPT IS PROVIDED TO YOU "AS IS" WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, WHETHER ORAL OR WRITTEN, EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY
# DISCLAIMS ANY IMPLIED WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY
# QUALITY, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
#######################################################################################
use strict;
use warnings;
use VMware::VIM2Runtime;
use VMware::VILib;
sub create_hash;
sub get_host_info;
sub print_log;
#######################################################################################
#
# exmultipathinfo.pl
# Example script to get the Multipathing Info of ESX server
# Parameters:
# -- hostname Host name
# -- datacenter Name of the DataCentre
# -- folder Name of the Folder
# -- vmotion The flag to indicate whether or not VMotion is enabled on this host
# -- maintenancemode The flag to indicate whether or not the host is in maintenance mode
# This script works with VMware VirtualCenter 2.0 or later.
# This script works with VMware ESX Server 3.0 or later.
#######################################################################################
my %opts = (
'hostname' => {
type => "=s",
help => "Host name",
variable => "hostname",
required => 0,
},
'datacenter' => {
type => "=s",
help => "Name of the DataCentre",
variable => "datacenter",
required => 0,
},
'folder' => {
type => "=s",
help => "Name of the Folder",
variable => "folder",
required => 0,
},
'vmotion' => {
type => "=i",
help => "The flag to indicate whether or not VMotion is enabled on this host",
variable => "vmotion",
required => 0,
},
'maintenancemode' => {
type => "=i",
help => "The flag to indicate whether or not the host is in maintenance mode",
variable => "maintenancemode",
required => 0,
}
);
# validate options, and connect to the server
Opts::add_options(%opts);
Opts::parse();
Opts::validate();
Util::connect();
get_host_info();
# logout
Util::disconnect();
# This subroutine first retrieve the hosts based on the user criteria like
# datacenter, folder etc. and then for each host it displays
# the host information like BootTime, cpumodel, cpuspeed, filesystem,
# memoryusage, networkadapters etc.
# =========================================================================
sub get_host_info {
my $hostname = Opts::get_option('hostname');
my $datacenter = Opts::get_option('datacenter');
my $folder = Opts::get_option('folder');
my $vmotion = Opts::get_option('vmotion');
my $maintenancemode = Opts::get_option('maintenancemode');
my %filterHashHost = create_hash($vmotion, $maintenancemode, $hostname);
#######################################################################################
# Find the list of available hosts on the filter hash
#######################################################################################
my $host_views = get_hosts('HostSystem', $datacenter
, $folder, %filterHashHost);
if ($host_views) {
foreach (@$host_views) {
my $host_view = $_;
if (defined($host_view->config->storageDevice->multipathInfo->lun)){
my @arrlunInfo = @{$host_view->config->storageDevice->multipathInfo->lun};
#######################################################################################
# Print the multipath information
#######################################################################################
foreach (@arrlunInfo) {
my $lun_Info = $_;
Util::trace(0,"\nInformation of Lunid: ". $_->id."\n");
Util::trace(0,"\nInformation of Policy: ". $_->policy->policy."\n");
if (defined($_->path)){
my @arrHostMultipathInfoPath = @{$_->path};
Util::trace(0,"\nNo of paths: ". length(@arrHostMultipathInfoPath)."\n");
foreach (@arrHostMultipathInfoPath) {
my $HostMultipath_InfoPath = $_;
Util::trace(0,"\nThe key identifier of the Path: ". $_->key."\n");
Util::trace(0,"\nInformation of Path State: ". $_->pathState."\n");
}
}
}
}
}
}
}
# Finds the hosts based on the selection criteria and return an array of host.
# ============================================================================
sub get_hosts {
my ($entity, $datacenter, $folder, %filter_hash) = @_;
my $begin;
my $entityViews;
my %filter = %filter_hash;
if (defined $datacenter) {
$begin =
Vim::find_entity_views (view_type => 'Datacenter',
filter => {name => "^$datacenter\$"});
unless (@$begin) {
Util::trace(0, "Datacenter $datacenter not found.\n");
return;
}
if ($#{$begin} != 0) {
Util::trace(0, "Datacenter <$datacenter> not unique.\n");
return;
}
}
else {
@$begin = Vim::get_service_content()->rootFolder;
}
if (defined $folder) {
my $vms = Vim::find_entity_views (view_type => 'Folder',
begin_entity => @$begin,
filter => {name => "^$folder\$"});
unless (@$vms) {
Util::trace(0, "Folder <$folder> not found.\n");
return;
}
if ($#{$vms} != 0) {
Util::trace(0, "Folder <$folder> not unique.\n");
return;
}
@$begin = shift (@$vms);
}
$entityViews = Vim::find_entity_views (view_type => $entity,
begin_entity => @$begin,
filter => \%filter);
unless (@$entityViews) {
Util::trace(0, "No host found.\n");
return;
}
if ($entityViews) {return \@$entityViews;}
else {return 0;}
}
# Create hash for filter criteria
# ================================
sub create_hash {
my ($vmotion, $maintenancemode, $hostname) = @_;
my %filterHash;
if ($vmotion) {
$filterHash{'summary.config.vmotionEnabled'} = $vmotion;
}
if ( $maintenancemode) {
$filterHash{'runtime.inMaintenanceMode'} = $maintenancemode;
}
if ( $hostname) {
$filterHash{'name'} = '^' . $hostname . '$';
}
return %filterHash;
}
The sample code is provided "AS-IS" for use, modification, and redistribution in source and binary forms, provided that the copyright notice and this following list of conditions are retained and/or reproduced in your distribution. To the maximum extent permitted by law, VMware, Inc., its subsidiaries and affiliates hereby disclaim all express, implied and/or statutory warranties, including duties or conditions of merchantability, fitness for a particular purpose, and non-infringement of intellectual property rights. IN NO EVENT WILL VMWARE, ITS SUBSIDIARIES OR AFFILIATES BE LIABLE TO ANY OTHER PARTY FOR THE COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT, INDIRECT, OR SPECIAL DAMAGES, ARISING OUT OF THIS OR ANY OTHER AGREEMENT RELATING TO THE SAMPLE CODE.
You agree to defend, indemnify and hold harmless VMware, and any of its directors, officers, employees, agents, affiliates, or subsidiaries from and against all losses, damages, costs and liabilities arising from your use, modification and distribution of the sample code.
VMware does not certify or endorse your use of the sample code, nor is any support or other service provided in connection with the sample code.