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
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.
- Powershell 2.0
- PowerCLI 4.0
- vCenter 4.0 or ESX 4.0
- 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:
My Env After:
Detailed Logic Walkthrough:
1. Prompts user for vCenter Name
- a. Checks if name is valid exits if not
2. Prompts for Cluster Name
- 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.
- a. Excludes datastores that have ‘local’ or ‘nfs_share’ in their name.
- b. Exits if the aggregate shows less than x% available.
- 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.
- a. Gathers current datastore data
- b. Finds datastores with more than x% free space
- i. If none found, assumes cluster has been balanced and exits
- ii. If number of datastores with free space is equal to total number of datastores, assumes cluster is balanced and exits.
- c. Calculates size of VM by adding all HDD’s and RAM.
- 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.
- i. If the calculated percentage is less than x%, VM does not move.
- 1. Reports back that VM cannot move to target datastore name.
- ii. If the calculated percentage is more than x%, VM moves to new datastore.
- 1. Reports back that it is moving VM to targeted datastore name.
- iii. If VM already resides on that datastore, nothing happens.
- iv. Loop
7. After loop is completed, script checks to see if there are any datastores with less than x% of free space.
- 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.