-
Notifications
You must be signed in to change notification settings - Fork 489
/
getVMDiskFormat.pl
executable file
·123 lines (91 loc) · 3.07 KB
/
getVMDiskFormat.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/perl -w
# Author: William Lam
# Website: www.williamlam.com
# Reference: http://www.williamlam.com/2011/09/how-to-query-vm-disk-format-in-vsphere.html
use strict;
use warnings;
use VMware::VIRuntime;
use VMware::VILib;
$SIG{__DIE__} = sub{Util::disconnect();};
my %opts = (
output => {
type => "=s",
help => "[console|csv]",
required => 1,
},
filename => {
type => "=s",
help => "Name of output file",
required => 0,
default => 'vmDiskFormat.csv',
},
);
Opts::add_options(%opts);
# validate options, and connect to the server
Opts::parse();
Opts::validate();
Util::connect();
my $output = Opts::get_option("output");
my $filename = Opts::get_option("filename");
my ($vmname,$type,$diskLabel,$diskName) = ("VMNAME","DISKTYPE","DISKLABEL","DISKNAME");
if($output eq "console") {
format format =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$vmname,$type,$diskLabel,$diskName
--------------------------------------------------------------------------------------------------------------------------------------------------
.
$~ = 'format';
write;
} else {
print "Generating $filename ...\n";
open(VMDISK_REPORT,">$filename");
print VMDISK_REPORT "$vmname,$type,$diskLabel,$diskName\n";
close(VMDISK_REPORT);
}
my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', properties => ['name','config.hardware.device']);
foreach my $vm_view(sort{$a->name cmp $b->name} @$vm_views) {
$vmname = $vm_view->{'name'};
my $devices = $vm_view->{'config.hardware.device'};
foreach my $device (@$devices) {
if($device->isa('VirtualDisk')) {
if($device->backing->isa('VirtualDiskFlatVer2BackingInfo')) {
$diskLabel = $device->deviceInfo->label;
$diskName = $device->backing->fileName;
if($device->backing->thinProvisioned) {
$type = "thinProvisioned";
} elsif($device->backing->eagerlyScrub) {
$type = "eagerzeroedthick";
} else {
$type = "zeroedthick";
}
if($output eq "console") {
write;
} else {
open(VMDISK_REPORT,">>$filename");
print VMDISK_REPORT "$vmname,$type,$diskLabel,$diskName\n";
close(VMDISK_REPORT);
}
}
}
}
}
Util::disconnect();
=head1 NAME
getVMDiskFormat.pl - Script to query virtual machines disk format (zeroedthick,eagerzioerdthick or thin)
=head1 Examples
=over 4
=item Query virtual machine disk format and output to console
=item
./getVMDiskFormat.pl --server [VCENTER_SERVER|ESXi_SERVER] --username [USERNAME] --output console
=item
=item Query virtual machine disk format and output to csv file
=item
./getVMDiskFormat.pl --server [VCENTER_SERVER|ESXi_SERVER] --username [USERNAME] --output csv
./getVMDiskFormat.pl --server [VCENTER_SERVER|ESXi_SERVER] --username [USERNAME] --output csv --filename [CSV_FILENAME]
=item
=back
=head1 SUPPORT
vSphere 5.0
=head1 AUTHORS
William Lam, http://www.williamlam.com/
=cut