Scripts for Yellow Bricks’ advise: Thin Provisioning alarm & eagerZeroedThick
On the Yellow Bricks blog there was today a very interesting entry called Performance : Thin Provisioning. Besides the link to the excellent VMware document called Performance Study of VMware vStorage Thin Provisioning, Duncan also included some tips and tricks.
Since I’m in favour of automating as much as possible in my vSphere environment, I decided to have a look how all this could be scripted.
Alarm
The following script will create an alarm that will be triggered by the status of two metrics directly related to Thin Provisioning:
- Disk Overallocation
- Disk Usage
The script will define two actions on the alarm:
- Send an email
- Send a SNMP trap
You can of course adapt the triggers and the alarms.
$datacenterName = $mailTo = "luc.dekens@lucd.info" $alarmMgr = Get-View AlarmManager $entity = Get-Datacenter $datacenterName | Get-View # AlarmSpec $alarm = New-Object VMware.Vim.AlarmSpec $alarm.Name = "Thin Provisioning" $alarm.Description = "Thin Provisioning alarm by LucD" $alarm.Enabled = $TRUE # Action 1 - Send email $alarm.action = New-Object VMware.Vim.GroupAlarmAction $trigger1 = New-Object VMware.Vim.AlarmTriggeringAction $trigger1.action = New-Object VMware.Vim.SendEmailAction $trigger1.action.ToList = $mailTo $trigger1.action.Subject = "Thin Provisioning Alarm" $trigger1.Action.CcList = "" $trigger1.Action.Body = "" # Transition 1a - yellow --> red $trans1a = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec $trans1a.StartState = "yellow" $trans1a.FinalState = "red" # Transition 1b - red --> yellow $trans1b = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec $trans1b.StartState = "red" $trans1b.FinalState = "yellow" $trigger1.TransitionSpecs += $trans1a $trigger1.TransitionSpecs += $trans1b # Action 2 - Send SNMP trap $trigger2 = New-Object VMware.Vim.AlarmTriggeringAction $trigger2.action = New-Object VMware.Vim.SendSNMPAction # Transition 2a - yellow --> red $trans2a = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec $trans2a.StartState = "yellow" $trans2a.FinalState = "red" # Transition 2b - yellow --> red $trans2b = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec $trans2b.StartState = "red" $trans2b.FinalState = "yellow" $trigger2.TransitionSpecs += $trans2a $trigger2.TransitionSpecs += $trans2b $alarm.action.action += $trigger1 $alarm.action.action += $trigger2 # Expression 1 - Overallocation $expression1 = New-Object VMware.Vim.MetricAlarmExpression $expression1.Metric = New-Object VMware.Vim.PerfMetricId $expression1.Metric.CounterId = 196 $expression1.Metric.Instance = "" $expression1.Operator = " isAbove" $expression1.Red = 30000 $expression1.Yellow = 10000 $expression1.Type = "Datastore" # Expression 2 - Disk usage $expression2 = New-Object VMware.Vim.MetricAlarmExpression $expression2.Metric = New-Object VMware.Vim.PerfMetricId $expression2.Metric.CounterId = 165 $expression2.Metric.Instance = "" $expression2.Operator = " isAbove" $expression2.Red = 9000 $expression2.Yellow = 7500 $expression2.Type = "Datastore" $alarm.expression = New-Object VMware.Vim.OrAlarmExpression $alarm.expression.expression += $expression1 $alarm.expression.expression += $expression2 $alarm.setting = New-Object VMware.Vim.AlarmSetting $alarm.setting.reportingFrequency = 0 $alarm.setting.toleranceRange = 0 # Create alarm. $alarmMgr.CreateAlarm($entity.MoRef, $alarm)
Annotations
Line 5: the script defines the alarm on a Datacenter but you can of course change this to another entity (where the alarm makes sense)
Line 20-21: the SendEmailAction seems to require that the CcList and Body properties are explicitly blanked out
Line 23-31, 41-49: I set up the alarm to trigger the actions when going from yellow to red but also from red to yellow. That way you’re informed when the situation goes back to “nearly” normal. You can change this behavior or even add additional transition states.
Line 60, 70: the CounterId properties can be obtained from the PerformanceManager.
eagerZeroedThick
This script will convert an existing thick VMDK to eagerZeroedThick. As you can read in Duncan’s blog entry there is a serious performance improvement to be obtained by doing this.
Note that the guest needs to be powered off to be able to do the conversion ! This is in fact the case for most of the VirtualDiskManager methods. See also my Thick to Thin with PowerCLI and the SDK entry.
$vmName =
$vCenter =
$esxAccount =
$esxPasswd =
function Set-EagerZeroThick{
param($vcName, $vmName, $hdName)
# Find ESX host for VM
$vcHost = Connect-VIServer -Server $vcName
$vmImpl = Get-VM $vmName
if($vmImpl.PowerState -ne "PoweredOff"){
Write-Host "Guest must be powered off to use this script !" -ForegroundColor red
return $false
}
$vm = $vmImpl | Get-View
$esxName = (Get-View $vm.Runtime.Host).Name
# Find datastore path
$dev = $vm.Config.Hardware.Device | where {$_.DeviceInfo.Label -eq $hdName}
if($dev.Backing.thinProvisioned){
return $false
}
$hdPath = $dev.Backing.FileName
# For Virtual Disk Manager we need to connect to the ESX server
$esxHost = Connect-VIServer -Server $esxName -User $esxAccount -Password $esxPasswd
# Convert HD
$vDiskMgr = Get-View -Id (Get-View ServiceInstance).Content.VirtualDiskManager
$dc = Get-Datacenter | Get-View
$taskMoRef = $vDiskMgr.EagerZeroVirtualDisk_Task($hdPath, $dc.MoRef)
$task = Get-View $taskMoRef
while("running","queued" -contains $task.Info.State){
$task.UpdateViewData("Info")
}
Disconnect-VIServer -Server $esxHost -Confirm:$false
# Connect to the vCenter
Connect-VIServer -Server $vcName
if($task.Info.State -eq "success"){
return $true
}
else{
return $false
}
}
Set-EagerZeroThick $vCenter $vmName "Hard disk 1"
Annotations
Line 3-4: provide an account and password which can logon to the ESX server and which has root authority
Line 12-14: the guest needs to be powered off to use the function
Line 21-23: the function only works for thick VMDKs. This tests for thin VMDKs and returns immediatly
Line 31: since we’re connected to an ESX server there is only one datacenter with the default name ha-datacenter
Line 32: the core of the function is the EagerZeroVirtualDisk_Task method
As always, please test this thoroughly in your test environment before you use any of this in your production environment !!


Very cool stuff, love it
cool stuff Luc
Thanks Alan & Duncan.
Means a lot from 2 of the authors of the vSphere 4.0 Quick Start Guide
Hi, where did you get the values for the PerfMetricId CounterId ? Is there a list somewhere?
You can get the list of PerfMetricIds from the PerformanceManager object. I’m planning a follow-up blog entry on this.
Update: see my entry Alarm expressions – Part 1 : Metric alarms for a script to list all the metric and their corresponding counterId.
Luc,
Did you find this was not possible via vCenter? It seems you initially tried it this way based on the $dc.MoRef which only appears once.
Carter, thanks for spotting this error.
Line 31, for one reason or the other dropped off.
You have to pass the default datacenter (ha-datacenter) that is known under the ESX server. It doesn’t work with the VC datacenter afaik.
Hello,
I am having trouble with the eagerzero script, it fails on line 30 for me with error:
The argument cannot be null or empty.
At :line:30 char:24
+ $vDiskMgr = Get-View -Id <<<< (Get-View ServiceInstance).Content.VirtualDiskManager
I see there is a comment about passing the default datacenter, but im not quite sure what needs to be done. I am using vspher 4.0 update 1. Thank you.
Sean