Specifications
# Search for VMs configured with less than 1GB memory and retrieve
their total
# memory and memory usage at the same time
my @results = $client->searchCustom(
ciType => 'VirtualMachine',
query => 'vm.memory:[0 TO 1023]',
pageData => {
pageSize => $pageSize,
pageNumber => 1,
},
sort =>{
sortField => 'vm.memory',
sortDescending => 0,
},
paths => ['vm.memory', 'vm.memload.latest'], # these show up in
'values' on the result
);
The result of the search call is placed into an array called @results.
5. Insert the following code into the script to iterate the result items that are
VMs and print out how much memory they actually have:
# Print out the names of the VMs and their memory size, tab
delimited
print "------------------------------------\n";
print "Memory\tUsed\tVM Name\n";
print "------------------------------------\n";
foreach my $result (@results) {
if (defined $result->{ciRef}) {
my $memory = $result->{'values'}[0]->{'value'};
my $memoryUtil = $result->{'values'}[1]->{'value'};
$memoryUtil = int($memoryUtil) if $memoryUtil;
print $memory."MB\t".$memoryUtil."%\t".$result->{ciRef}->
{displayName}."\n";
}
elsif (defined $result->{'total'})
{
$total = $result->{'total'};
}
}
print "....Showing $pageSize of $total\n";
307
Writing your first script