Get Datastore with Most Free Space & Check Datastore Meets Capacity Reservation

I do a lot of work that involves either creating new virtual hard disks or attaching existing ones to virtual machines in VMware vSphere. I do all of this through vRealize Orchestrator, written in JavaScript (yum).

As part of this task, I always ensure that the datastores that I am creating disks on have sufficient capacity for these new disks. I use two simple functions that I have written which perform these checks for me, which is ‘getDatastoreWithMostFreeSpace’ and ‘DoesDSMeetCapcityReserve’. An additional function ‘convertToGB’, which is used by these functions is also provided at the end.

Function : getDatastoreWithMostFreeSpace

Description
Return a single VcDatastore object that has the most free space from an Array of provided VcDatastore objects. The function will always return a VcDatastore, regardless of how much free space is found.

Parameters
– VcDatastores (Array of VcDatastore)

Return Type : VcDatastore

function getDatastoreWithMostFreeSpace(VcDatastores) {
    // Initilize variables.
    var DsWithMostFreeSpace = "";
	var maxFreeSpaceFound = 0;
	
	System.log("Selecting the datastore with the most free space:");
	// Loop through each VcDatastore in the Array.
	for (d in VcDatastores) {
	    // Set dsIndex to an increment of the array index (nicer than seeing numbers that start from 0).
	    var dsIndex = Number(d+1);
	    // Check if the Datastore is in maintenance mode.
	    if (VcDatastores[d].summary.maintenanceMode === 'normal') {
	        // Update free space and capacity usage information for the current Datastore.
            VcDatastores[d].refreshDatastore();
            // Output Datastore info.
		    System.log("Datastore " + dsIndex + " information:");
		    System.log("\tname: " + VcDatastores[d].name);
			System.log("\tID: " + VcDatastores[d].id);
			
			// Get Datastore Capacity value.
		    var DsCapacityGB = convertToGB(VcDatastores[d].summary.capacity);
		    System.log("\tCapacity: " + DsCapacityGB + " GB");
		
		    // Get Datastore Free Space value.
			var DsFreeSpaceGB = convertToGB(VcDatastores[d].summary.freeSpace);
		    System.log("\tFree Space: " + DsFreeSpaceGB + " GB");
		
		    // Get Datastore Used Space value.
		    var DsUsedSpaceGB = DsCapacityGB - DsFreeSpaceGB;
		    System.log("\tUsed Space: " + DsUsedSpaceGB + " GB");
		
		    // Get maximum supported VMDK size.
		    var DsMaxVMDKSizeGB = convertToGB(VcDatastores[d].info.maxVirtualDiskCapacity);
		    System.log("\tMaxVMDKSize: " + DsMaxVMDKSizeGB + " GB");
		
		    // Check if the current datastore has the most free space and remember this datastore.
		    if (DsFreeSpaceGB > maxFreeSpaceFound) {
		        maxFreeSpaceFound = DsFreeSpaceGB;
				DsWithMostFreeSpace = VcDatastores[d];
		    } // End if
        } else {
            // Output a warning if current datastore is currently in maintenance mode.
            System.warn("Datastore: '" + DsWithMostFreeSpace.name + "' in state: '" + VcDatastores[d].summary.maintenanceMode + "'. Skipping.");
            break;
        } //End if
	} // End for
	
    // Return a single VcDatastore object.
	return DsWithMostFreeSpace;
}

Function : DoesDSMeetCapcityReserve

Description
Checks that a given VcDatastore has enough free space in which to place the virtual disk, taking into account current usage and a defined reservation value (in percent) that should be guaranteed. Returns true if the Datastore passes the capacity checks.

Parameters
– datastore (VcDatastore)
– reservation (Number)
– totalSizeRequired (Number)

Return Type : Boolean

function DoesDSMeetCapcityReserve(datastore, reservation, totalSizeRequired) {
    // Set meetsReservation to default to false.
    var meetsReservation = false;
    var DsCapacityGB = convertToGB(datastore.summary.capacity);
    var DsFreeSpaceGB = convertToGB(datastore.summary.freeSpace);
    // Get the percentage of free space available.
    var DsPercentFree = Math.ceil((DsFreeSpaceGB/DsCapacityGB)*100);
    System.log("Datastore reservation is set to " + reservation + "%");
    System.log("Datastore has: " + DsPercentFree + "% free space");

    if (DsPercentFree > reservation) {
        System.log("Datastore meets " + reservation + "% reservation threshold.")
        System.log("Checking that enough storage is available for the disks");
        var reservationSizeGB = Math.ceil((DsCapacityGB/100)*reservation);

        System.log("The reserved size at " + reservation +  "% is: " + reservationSizeGB + " GB");
        
        var freeSpaceWithoutReserve = (DsFreeSpaceGB - reservationSizeGB);
        System.log("Free space available taking into account the reservation: " + freeSpaceWithoutReserve + " GB");

        var freeSpaceAfterCommit = (freeSpaceWithoutReserve - totalSizeRequired);
        System.log("The datastore will have " + freeSpaceAfterCommit + " GB of usable space remaining after the commit");

        if (freeSpaceAfterCommit > 0) {
            System.log("Datastore meets capacity requirements");
            meetsReservation = true;
        }
    };
    return meetsReservation;
}

Function : convertToGB

Description
Converts a value in Bytes to GB.

Parameters
– size (Number)

Return Type : Number

function convertToGB(size) {
    sizeGB = Math.ceil(size/1024/1024/1024);
    return sizeGB;
}

Example Output

[2017-09-18 15:09:37.459] [I] Selecting the datastore with the most free space:
[2017-09-18 15:09:37.714] [I] Datastore 1 information:
[2017-09-18 15:09:37.715] [I] name: DATASTORE1
[2017-09-18 15:09:37.717] [I] ID: datastore-99999
[2017-09-18 15:09:37.718] [I] Capacity: 2048 GB
[2017-09-18 15:09:37.719] [I] Free Space: 1762 GB
[2017-09-18 15:09:37.721] [I] Used Space: 286 GB
[2017-09-18 15:09:37.725] [I] MaxVMDKSize: 63488 GB
[2017-09-18 15:09:37.726] [I] Selected Candidate Datastore: DATASTORE1
[2017-09-18 15:09:37.728] [I] Performing capacity validation checks on the datastore.
[2017-09-18 15:09:37.729] [I] Datastore reservation is set to 10%
[2017-09-18 15:09:37.730] [I] Datastore has: 87% free space
[2017-09-18 15:09:37.733] [I] Datastore meets 10% reservation threshold.
[2017-09-18 15:09:37.735] [I] Checking that enough storage is available for the virtual disk(s)
[2017-09-18 15:09:37.737] [I] The reserved size at 10% is: 205 GB
[2017-09-18 15:09:37.738] [I] Free space available taking into account the reservation: 1557 GB
[2017-09-18 15:09:37.740] [I] The datastore will have 1556 GB of usable space remaining after the commit
[2017-09-18 15:09:37.741] [I] Datastore meets capacity requirements
[2017-09-18 15:09:37.744] [I] The Virtual Disk(s) will be located on: DATASTORE1

0 0 vote
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Rama
Rama
1 year ago

Awesome, thank you so much for putting this in one flow. You have made my day.