hrobinson
VMware Employee
VMware Employee

Adding Cluster Name to a VM

have a script, which reports me the diskusage of

the VMs. I want also see in which cluster the VMs

are. If it is sorted, that's fine, but not

neccesary.

So, I think it's just one line of code for you. I

think the simplest way is to print the clustername in

the same line of every VM, but that depends on you.

If you want, I can also send you the file, maybe

that's easier for you.

Thank you in advance

Christoph

Email: C.Petry@ing-diba.de

Here the code of the actual script:

#!/usr/bin/perl -w

use strict;

use warnings;

use Getopt::Long;

use VMware::VIRuntime;

my $username = 'vcbuser';

my $password = "pwd";

my $service_url = "https://vc/sdk/vimService";

Vim::login(service_url => $service_url, user_name =>

$username, password => $password);

print

"\n\/-------------------------------------------------

---------------------------------------
\n";

print "| \t\t Virtual Machine Name | Disk Volume |

Total DiskSize | Free DiskSpace |\n";

print

"|----------------------------------------------------

------------------------------------|\n";

my $vm_views = Vim::find_entity_views(view_type =>

'VirtualMachine',

filter => {

'runtime.powerState' => 'poweredOn',

'guest.toolsStatus' => 'toolsOk' },);

foreach my $thisVM (@$vm_views) {

my $vmname = $thisVM->config->name;

my $count = 0;

my $diskCapacity = 0;

my $diskCapacityMB = 0;

my $diskCapacityTotal = 0;

my $diskCapacityTotalMB = 0;

my $diskFree = 0;

my $diskFreeMB = 0;

my $diskFreeTotal = 0;

my $diskFreeTotalMB = 0;

my $diskFreePercent = 0;

my $diskSpaceAlertMB = 1500;

my $megaBytes = 1024*1024;

my $diskSpaceAlert = $diskSpaceAlertMB*$megaBytes;

while ($thisVM->guest->disk->\[$count])

{

print "| ";

printf '%37.35s', $vmname;

print "\t| ";

  1. Get the diskPath of the current disk

printf '%12.12s',

', $thisVM->guest->disk->\[$count]->diskPath;

print " | ";

  1. Get the freeSpace on the current disk

$diskFree =

= $thisVM->guest->disk->\[$count]->freeSpace;

$diskFreeMB = $diskFree/$megaBytes;

  1. Get the toatal capacity of the current disk

$diskCapacity =

= $thisVM->guest->disk->\[$count]->capacity;

$diskCapacityMB =

= $diskCapacity/$megaBytes;

$diskFreeTotal += $diskFree;

$diskCapacityTotal += $diskCapacity;

printf '%16.16s', int($diskCapacityMB) . " MB | ";

printf '%18.18s', int($diskFreeMB) . " MB |\n";

$count++;

}

}

print

"
---------------------------------------------------

-------------------------------------\/\n\n";

Vim::logout();

0 Kudos
0 Replies