VMware Cloud Community
steve31783
Enthusiast
Enthusiast
Jump to solution

Script to determine disk space saved by thin provisioning

Hello..

I was wondering if anyone had a script that would allow me to output a list of VMs, their provisioned space (as a #, with no gb after it), and their used space (as a #, no gb after it). Id then like to take this data into excel, add it all up and see how much disk space we've saved on our SAN by using thin provisioning.

Does anyone have any idea? I think a script to do something like this could be quite useful for a lot of folks who can go back to management and show how much $ they've saved in SAN disk by using this "new" feature.

Thanks!

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The only way I could get the "Divide by zero" error was when I made one or more of the datastores unavailable.

The following script excludes the unavailable datatsores

$report = @()
$vms = Get-View -ViewType VirtualMachine
foreach($vm in $vms){
	foreach($perDS in $vm.Storage.PerDatastoreUsage){
		if($perDS -ne $null){
			$row = "" | Select VMname, DSname, ThinProvisioned, ThinUsed, SavedPerc
			$row.VMname = $vm.Name
			$row.DSname = (Get-View -Id $perDS.Datastore).Name
			$row.ThinProvisioned = "{0:f2}" -f (($perDS.Committed + $perDS.Uncommitted) / 1GB)
			$row.ThinUsed = "{0:f2}" -f ($perDS.Committed / 1GB)
			$row.SavedPerc = "{0:f0}" -f ($perDS.Uncommitted / ($perDS.Committed + $perDS.Uncommitted) * 100)
			$report += $row
		}
	}
}
$report | ft


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
17 Replies
lamw
Community Manager
Community Manager
Jump to solution

Just wrote this after reading this post: , it's in Perl but I'm sure one of the PowerCLI gurus will be able to provide you with the snippet of PS code.

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Comuunity

Twitter: @lamw

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
a2alpha
Expert
Expert
Jump to solution

I would start here, with some modifications it might be what you are after:

  1. FindThinDisks.ps1

  2. Identifies VMs and templates that are using thin-provisioned virtual disks.

  3. Version 1.0 January 14, 2009

  4. Eric Gray

$vmtp = Get-VM

$vmtp += Get-Template

foreach($vm in $vmtp | Get-View){

foreach($dev in $vm.Config.Hardware.Device){

if(($dev.GetType()).Name -eq "VirtualDisk"){

if($dev.Backing.ThinProvisioned -eq $true) {

$vm.Name + "`t" + $dev.Backing.FileName

}

}

}

}

Hope this helps I got it from here.

Dan

Reply
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

As of vSphere, it's easier than that. If you look at each VM, whether or not it's thin provisioned, you'll get provisioned space and used space. This data is provided at each VM and aggregates the total amount that is committed and uncommitted for each datastore a VMDK from a given VM is stored in. You just need to extract this information and output it in the format you would like

http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.StorageInfo.html

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Comuunity

Twitter: @lamw

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
steve31783
Enthusiast
Enthusiast
Jump to solution

Thanks.. I'll try to figure something out using that. My PS knowledge is

limited, so if anyone feels like throwing together a snippet of code,

please feel free.

lamw

<communities-emai

ler@vmware.com> To

Steve <sdion@apc.com>

09/27/2009 11:49 cc

AM

Subject

New

message: "Script to determine disk

space saved by thin provisioning"

Steve,

A new message was posted in the thread "Script to determine disk space

saved by thin provisioning":

http://communities.vmware.com/message/1374998#1374998

Author : lamw

Profile : http://communities.vmware.com/people/lamw

Message:

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Freely borrowed from Wiliam's Perl script, my attempt

$report = @()
$vms = Get-View -ViewType VirtualMachine
foreach($vm in $vms){
	foreach($perDS in $vm.Storage.PerDatastoreUsage){
		$row = "" | Select VMname, DSname, ThinProvisioned, ThinUsed, SavedPerc
		$row.VMname = $vm.Name
		$row.DSname = (Get-View -Id $perDS.Datastore).Name
		$row.ThinProvisioned = "{0:f2}" -f (($perDS.Committed + $perDS.Uncommitted) / 1GB)
		$row.ThinUsed = "{0:f2}" -f ($perDS.Committed / 1GB)
		$row.SavedPerc = "{0:f0}" -f ($perDS.Uncommitted / ($perDS.Committed + $perDS.Uncommitted) * 100)
		$report += $row
	}
}
$report | ft


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
steve31783
Enthusiast
Enthusiast
Jump to solution

I seem to be getting a lot of errors when running that "$row.DSname =

(Get-View -Id <<<< $perDS.Datstore).Name Attempted to dvide by 0"

LucD

<communities-emai

ler@vmware.com> To

Steve <sdion@apc.com>

09/28/2009 05:13 cc

PM

Subject

New

message: "Script to determine disk

space saved by thin provisioning"

Steve,

A new message was posted in the thread "Script to determine disk space

saved by thin provisioning":

http://communities.vmware.com/message/1376134#1376134

Author : LucD

Profile : http://communities.vmware.com/people/LucD

Message:

Reply
0 Kudos
steve31783
Enthusiast
Enthusiast
Jump to solution

Thanks for looking at this BTW, I appreciate your effort.

LucD

<communities-emai

ler@vmware.com> To

Steve <sdion@apc.com>

09/28/2009 05:13 cc

PM

Subject

New

message: "Script to determine disk

space saved by thin provisioning"

Steve,

A new message was posted in the thread "Script to determine disk space

saved by thin provisioning":

http://communities.vmware.com/message/1376134#1376134

Author : LucD

Profile : http://communities.vmware.com/people/LucD

Message:

Reply
0 Kudos
scott_herold
Enthusiast
Enthusiast
Jump to solution

Here is a snippet I have been using. it does NOT use the vSphere specific SDKs, so if there are thin-provisioned templates in a VI3 environment, it can be slightly modified to include those as well. I basically take the standard VMware.Vim.VirtualDevice object type and extend its properties.

Get-VM | Get-View | ForEach-Object {
	$vmname = $_.Name
	$uncommittedKB = ($_.Summary.Storage.Uncommitted/1024)
	$_.Config.Hardware.Device | Where {($_.GetType()).Name -eq "VirtualDisk"} | ForEach-Object {
		if($_.Backing.ThinProvisioned -eq $true){
			$_.PSObject.TypeNames.Insert(0,"$($_.PSObject.TypeNames[0])_Thin#VmwarePowerPackExtension")
			$_ `
				| Add-Member -MemberType NoteProperty -Name ThinProvisioned -Value $_.Backing.ThinProvisioned -PassThru `
				| Add-Member -MemberType NoteProperty -Name FileName -Value $_.Backing.FileName -PassThru `
				| Add-Member -MemberType NoteProperty -Name DeviceName -Value $_.DeviceInfo.Label -PassThru `
				| Add-Member -MemberType NoteProperty -Name VMName -Value $vmname -PassThru `
				| Add-Member -MemberType NoteProperty -Name UncommittedKB -value $uncommittedKB -PassThru
		}
	}
}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That should be $perDS.Datastore.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
steve31783
Enthusiast
Enthusiast
Jump to solution

I must be missing something.... I do see $perDS.Datastore in your script...

$row.DSname = (Get-View -Id $perDS.Datastore).Name

Reply
0 Kudos
steve31783
Enthusiast
Enthusiast
Jump to solution

Hi LucD, could you explain what you meant? Thanks in advance.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think you might have a typo in the script.

Your error message says


$row.DSname =(Get-View -Id <<<< $perDS.Datstore).Name

That should be


$row.DSname = (Get-View -Id $perDS.Datastore).Name


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
steve31783
Enthusiast
Enthusiast
Jump to solution

Ah, sorry.. that only appears in the output of the error, it is not contained in the script. Any other ideas?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The only way I could get the "Divide by zero" error was when I made one or more of the datastores unavailable.

The following script excludes the unavailable datatsores

$report = @()
$vms = Get-View -ViewType VirtualMachine
foreach($vm in $vms){
	foreach($perDS in $vm.Storage.PerDatastoreUsage){
		if($perDS -ne $null){
			$row = "" | Select VMname, DSname, ThinProvisioned, ThinUsed, SavedPerc
			$row.VMname = $vm.Name
			$row.DSname = (Get-View -Id $perDS.Datastore).Name
			$row.ThinProvisioned = "{0:f2}" -f (($perDS.Committed + $perDS.Uncommitted) / 1GB)
			$row.ThinUsed = "{0:f2}" -f ($perDS.Committed / 1GB)
			$row.SavedPerc = "{0:f0}" -f ($perDS.Uncommitted / ($perDS.Committed + $perDS.Uncommitted) * 100)
			$report += $row
		}
	}
}
$report | ft


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
steve31783
Enthusiast
Enthusiast
Jump to solution

That worked perfectly.. thank you!!!

Two last questions..

1. Is there an easy way to export this to CSV? (so I can put the data in excel, total things up, do whatever else).

2. Can this be applied to paritcular clusters only?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure.

1) You can pipe the Get-Cluster and Get-Vm cmdlets together to get only the VMs on a specific cluster

2) Pipe the report to an Export-Csv cmdlet

$report = @()
$vms = Get-Cluster <your-cluster-name> | Get-Vm | Get-View
foreach($vm in $vms){
	foreach($perDS in $vm.Storage.PerDatastoreUsage){
		if($perDS -ne $null){
			$row = "" | Select VMname, DSname, ThinProvisioned, ThinUsed, SavedPerc
			$row.VMname = $vm.Name
			$row.DSname = (Get-View -Id $perDS.Datastore).Name
			$row.ThinProvisioned = "{0:f2}" -f (($perDS.Committed + $perDS.Uncommitted) / 1GB)
			$row.ThinUsed = "{0:f2}" -f ($perDS.Committed / 1GB)
			$row.SavedPerc = "{0:f0}" -f ($perDS.Uncommitted / ($perDS.Committed + $perDS.Uncommitted) * 100)
			$report += $row
		}
	}
}
$report | Export-Csv "C:\VM-DS-Usage.csv" -NoTypeInformation


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Ankojirao
Contributor
Contributor
Jump to solution

Hi Lucd,

Thanks for script .

I have run the script and its getting Thick disk also in report.

How to exclude Thick disk from script ?

Reply
0 Kudos