VMware {code} Community
pvjsingh
Contributor
Contributor
Jump to solution

list VMs with disks and other details

I am looking into having a comprehensive pl script to get:

1. Host in a Datacenter

2. List VMs found

3. List all files for the VMs found with SAN Paths

4. Show the drive letter for disk found along with disk size, and disk space free

I need to find out how to enumerate item number 4, and help greatly appreciated:

This is what I have come up with thus far (re-used and edited a couple of sample scripts found):

#!/usr/bin/perl -w

#

  1. Copyright 2006 VMware, Inc. All rights reserved.

#

  1. This script prints names of all VM's with Windows guest

use strict;

use warnings;

use Getopt::Long;

use VMware::VIM2Runtime;

use VMware::VILib;

use VMware::VIRuntime;

$Util::script_version = "1.0";

my %opts = (

datacenter => {

type => "=s",

help => "Datacenter name",

required => 1,

},

);

  1. read/validate options and connect to the server

Opts::add_options(%opts);

Opts::parse();

Opts::validate();

Util::connect();

  1. find datacenter

my $datacenter = Opts::get_option('datacenter');

my $datacenter_view = Vim::find_entity_view(view_type => 'Datacenter',

filter => { name => $datacenter });

if (!$datacenter_view) {

die "Datacenter '" . $datacenter . "' not found\n";

}

  1. get all hosts under this datacenter

my $host_views = Vim::find_entity_views(view_type => 'HostSystem',

begin_entity => $datacenter_view);

  1. print hosts

my $counter = 1;

print "Hosts found:\n";

foreach (@$host_views) {

print "$counter: " . $_->name . "\n";

$counter++;

}

  1. print vm's

$counter = 1;

print "\nVM's found:\n";

  1. get all VM's under this datacenter

my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine',

begin_entity => $datacenter_view);

foreach (@$vm_views) {

print "$counter: " . $_->name . "\n";

#######################################################################################

#

# Find the Virtual Machine

#######################################################################################

my $vm_name = $_->name;

my $vm_view = Vim::find_entity_view(view_type => 'VirtualMachine', filter => {'name' => "^$vm_name\$"});

Fail ("Virtual Machine $vm_name was not found.\n") unless ($vm_view);

my $files = $vm_view->layout;

foreach my $file ('configFile', 'disk', 'logFile', 'snapshot', 'swapFile') {

my $item = $files->$file;

next unless (defined $item);

print "$file\n";

#

  1. for snapshots, list the snapshot name, and the files of each snapshot

#

if ($file eq 'snapshot') {

foreach my $snapshot (@$item) {

my $i = 0;

my $list = $snapshot->snapshotFile;

my $name = $snapshot->key->value;

print " $name", "\n";

foreach my $element (@{$list}) {

print " ", $element, "\n";

$i++;

}

}

}

#

  1. for disk files, list the diskFile array

elsif ($file eq 'disk') {

my $i = 0;

foreach my $disk (@{$item}) {

foreach my $element (@{$disk->diskFile}) {

print " : ", $element, "\n";

$i++;

}

}

}

#

  1. the item may have an array of files ...

elsif (ref($item) eq 'ARRAY') {

my $i = 0;

foreach my $element (@{$item}) {

print " : ", $element, "\n";

$i++;

}

}

  1. the item is a simple item

else { print " ", $item, "\n"; }

}

$counter++;

}

  1. disconnect from the server

Util::disconnect();

sub Fail {

my ($msg) = @_;

Util::disconnect();

die ($msg);

exit ();

}

http://www.ttgapers.com
Reply
0 Kudos
1 Solution

Accepted Solutions
hrobinson
VMware Employee
VMware Employee
Jump to solution

Here is the updated script with the relevant lines added.

I added a loop that traces through the VirtualMachine.guest.disk[] array to get the information.

I also did the following.

- since you have already found the virtual machines (find_entity_views), then there is no need for you to get the VM data again. This should speed things up quite a bit.

- You didn't check for an "unknown" vm that could have incomplete information. I added the test to see if the "files" array had been populated.

- Finally, I sorted the VM alphabetically with (foreach my $vm_view (sort {$a->name cmp $b->name} @$vm_views)

Hope this helps.

Henry

View solution in original post

Reply
0 Kudos
16 Replies
StefanPahrmann
Jump to solution

Unfortunatly this is not possible. The only thing you can is to get the size(s) of the vmdk-files and how much they are filled.

From the SDK you can't which drive-letters are used or how much the partitions inside the vmdk-file is filled.

If you need to figure out the first i can dig out some code, or you can see here: http://www.run-virtual.com/?page_id=145

-Stefan

pvjsingh
Contributor
Contributor
Jump to solution

The only thing you can is to get the size(s) of the vmdk-files and how much they are filled.

do you know what I would have to do to my script to get the above details?

Thanks

Jason

http://www.ttgapers.com
Reply
0 Kudos
hrobinson
VMware Employee
VMware Employee
Jump to solution

Here is the updated script with the relevant lines added.

I added a loop that traces through the VirtualMachine.guest.disk[] array to get the information.

I also did the following.

- since you have already found the virtual machines (find_entity_views), then there is no need for you to get the VM data again. This should speed things up quite a bit.

- You didn't check for an "unknown" vm that could have incomplete information. I added the test to see if the "files" array had been populated.

- Finally, I sorted the VM alphabetically with (foreach my $vm_view (sort {$a->name cmp $b->name} @$vm_views)

Hope this helps.

Henry

Reply
0 Kudos
pvjsingh
Contributor
Contributor
Jump to solution

Hi hrobinson,

Your response has fixed the problem and more! I have renamed the file and will attach it here.

For other users this script does the following:

1. List hosts in a datacenter

2. List VMs found in the datacenter in alphabetical order

3. List all files associated with the VM in the following order:

i. List disks with drive letter, drive capacity, and percentage free space

ii. List config files

iii. List log files

iv. List swap files

v. List any snapshot files

Only other thing I can see to do to this is to add the sizes of the other files (especially the swap file), and also change the formatting so that it can be easily imported to Excel. I use this script for SAN planning.

I am a VBScript writer, PHP writer, but wrapping my head around Perl and the VMware API has not been nice thus far.

Thanks once again!

Jason

Jason

http://www.ttgapers.com
Reply
0 Kudos
hrobinson
VMware Employee
VMware Employee
Jump to solution

I added the facility to add the file size to all of your files.

Unfortunately, I used your old source to make the adds but it lists the file size for each supplied file.

Reply
0 Kudos
pvjsingh
Contributor
Contributor
Jump to solution

hi,

The new script generates the following error when run:

Global symbol "$name" requires explicit package name at listvmswithdisks.pl line185.

Global symbol "$pattern" requires explicit package name at listvmswithdisks.pl line 188.

Execution of exprint.pl aborted due to compilation errors.

Thanks,

Jason

http://www.ttgapers.com
Reply
0 Kudos
hrobinson
VMware Employee
VMware Employee
Jump to solution

Sorry, forgot to rerun after cleanup.

Reply
0 Kudos
pvjsingh
Contributor
Contributor
Jump to solution

This works. I will add some more descriptions in your script, and either show sizes in bytes, or GB.

thanks hrobinson, very much appreciated.

Jason

http://www.ttgapers.com
Reply
0 Kudos
shaka
Enthusiast
Enthusiast
Jump to solution

How would you change the script to input the ESX host name rather than a datacenter and then list the VMs under that Host?

Reply
0 Kudos
hrobinson
VMware Employee
VMware Employee
Jump to solution

May need some cleanup but here goes.

Reply
0 Kudos
meistermn
Expert
Expert
Jump to solution

Can the output be made in a csv format way, so that the storage teams can import the values ?

Reply
0 Kudos
meistermn
Expert
Expert
Jump to solution

hello hrobinson,

i tried to combined our script exprint and the diskfree script from .

I want the partition size of c: and d: of the exprint script to be in a custom field diskpartc and diskpartd.

Any Idee what is wrong with the script ?

Reply
0 Kudos
Ashwin89
Contributor
Contributor
Jump to solution

Hey

I am able to get the total size of the VMDK files.

How can I get the actual size(i.e. how much they are filled) ?

Thanks

AshWin

Reply
0 Kudos
tos2k
Expert
Expert
Jump to solution

Hi!

This is not possible currently. First you need to have VMware tools installed on a VM to see how much space is left on each partition. Got that?! Fine. Then you need to know which partition resides on which virtual disk - and exactly this link is missing, and currently not provided by VMware.

I was looking for a (very) long time to get this working, but did not find a suitable solution so long.

Read on:

http://communities.vmware.com/message/988597

http://communities.vmware.com/message/842741

http://communities.vmware.com/message/842815

Tos2k

Reply
0 Kudos
stavener
Contributor
Contributor
Jump to solution

Ive done a VB ASP script which i run from an IIS server... it queries a DSN setup on the IIS server, which points to the VCDB on a SQL server.

it dumps the VMFS paritions , and then lists all the VMs with the following details... (which can also be filtered to reduce the output listing)

172.20.30.68

SRVCOMDR01

Hot Standby Server

srvvm02

1048

1

C:\=10.73Gb D:\=10.73Gb

SG3_Raid5_544GB

you'll need to..

1. setup IIS with ASP.

2. setup a DSN and ammend this line in the script... for your vpxadmin password.

VCdbConnection.Open "DSN=VCDB; UID=vpxadmin; Password=PASSWORD"

copy this file onto the IIS server and access it via a browser over http.

regards,

Stuart.

Reply
0 Kudos
raghavendrats
Contributor
Contributor
Jump to solution

This script is really good. but it would be good if it is possible to add Annotations (Owner,Project Details of the VM) info in this script. This helps us to have a complete list of VMs with respective Owner and Project info.

Thanks.

Reply
0 Kudos