VM Storage Balancer

One thing I’ve noticed in vCenter 4.0 is that it now has alarms to monitor datastore sizes and places little ‘warning’ or ‘alert’ icons.  I like it, but it also annoys me because I know I need to do clean up and make sure each of our datastores has a minimum of 20% free space.  So here we are: Move-VMStorageBalance.ps1

Summary:

Running this script will attempt to storage vMotion systems in a specified cluster around to datastores that have more than x% of free space before and after the move. Systems with specified DRS rules are omitted by default.  I’ve left some defaults defined in two variables that you will need to change to work for your environment.

Requirements:

  • Powershell 2.0
  • PowerCLI 4.0
  • vCenter 4.0 or ESX 4.0

Disclaimer/Caveats:

  • Use this script @ your own risk, I take no responsibility for any awesomeness that might occur.  Test, test, and test more.
  • Script may be SLOW as it works against 1 VM at a time.
    • Fast if the detected cluster datastores are already balanced.
  • Script is only as smart as what information you provide.
  • Script contains no logic to handle VM’s with vmdk’s located on multiple different datastores.
  • If disk performance is an issue in your enviro, then DO NOT RUN this script.

My Env Before:

BeforeBalance

My Env After:

 AfterBalance

Detailed Logic Walkthrough:

1. Prompts user for vCenter Name

  1. a. Checks if name is valid exits if not

2. Prompts for Cluster Name

  1. a. Check if name is valid exits if not

3. Gathers Datastore information and measures whether there is an aggregate of x% free space available.

  1. a. Excludes datastores that have ‘local’ or ‘nfs_share’ in their name.
  2. b. Exits if the aggregate shows less than x% available.
  3. c. Returns what the script calculated as aggregate datastore capacity.

4. Checks DRS Rules

5. Gathers VM’s in cluster and omits any that are apart of DRS Rules

6. Now it begins a loop per VM.

  1. a. Gathers current datastore data
  2. b. Finds datastores with more than x% free space
    1. i. If none found, assumes cluster has been balanced and exits
    2. ii. If number of datastores with free space is equal to total number of datastores, assumes cluster is balanced and exits.
  3. c. Calculates size of VM by adding all HDD’s and RAM.
  4. d. Subtracts datastore free space amount from calculated VM Size, divides result by datastore capacity to get a percentage of free space if VM moves to targeted datastore.
    1. i. If the calculated percentage is less than x%, VM does not move.
      1. 1. Reports back that VM cannot move to target datastore name.
    2. ii. If the calculated percentage is more than x%, VM moves to new datastore.
      1. 1. Reports back that it is moving VM to targeted datastore name.
    3. iii. If VM already resides on that datastore, nothing happens.
    4. iv. Loop

7. After loop is completed, script checks to see if there are any datastores with less than x% of free space.

  1. a. If found, reports that additional space may be required to balance datastores.

If none found, reports that datastores are balanced and should have @ least x% of free space on each datastore.

Script can be viewed and copied from here

Comments

Popular posts from this blog

NSX-T: vCenter and NSX-T Inventory out of Sync (Hosts in vSphere not showing up in NSX-T)

MacOS: AnyConnect VPN client was unable to successfully verify the IP forwarding table modifications.

vCenter: Cluster Skip Quickstart Workflow via API