Jun/091
Examine VMware CPU Ready Times with Powershell
When your (VMware) consolidation ratios are becoming high, it might be smart to keep an eye on your vm’s CPU Ready Times. Unfortunately, by default, the VI Client will only show realtime ready time statistics. Plus you’d have to look at each vm individually. Thank God VMware for the PowerCLI! Read this document for more information on how to interpret the results.
# Variables $OutFile = "D:\Scripts\ReadyTimes.csv" $VIServer = "MyVIServer.domain.local" # Connect to Virtual Center $VI = Connect-VIServer $VIServer $myCol = @() ForEach ($VMHost in (Get-VMHost | Sort Name)) { ForEach ($VM in ($VMHost | Get-VM | Sort Name)) { # Gather Stats $Ready = $VM | Get-Stat -Stat Cpu.Ready.Summation -RealTime $Used = $VM | Get-Stat -Stat Cpu.Used.Summation -RealTime $Wait = $VM | Get-Stat -Stat Cpu.Wait.Summation -RealTime For ($a = 0; $a -lt $VM.NumCpu; $a++) { $myObj = "" | Select VMHost, VM, Instance, %RDY, %USED, %WAIT $myObj.VMHost = $VMHost.Name $myObj.VM = $VM.Name $myObj.Instance = $a $myObj."%RDY" = [Math]::Round((($Ready | Where {$_.Instance -eq $a} | Measure-Object -Property Value -Average).Average)/200,1) $myObj."%USED" = [Math]::Round((($Used | Where {$_.Instance -eq $a} | Measure-Object -Property Value -Average).Average)/200,1) $myObj."%WAIT" = [Math]::Round((($Wait | Where {$_.Instance -eq $a} | Measure-Object -Property Value -Average).Average)/200,1) $myCol += $myObj } Clear-Variable Ready -ErrorAction SilentlyContinue Clear-Variable Wait -ErrorAction SilentlyContinue Clear-Variable Used -ErrorAction SilentlyContinue Clear-Variable myObj -ErrorAction SilentlyContinue } } Disconnect-VIServer -Confirm:$false # Export and launch output $myCol | Export-Csv $OutFile Invoke-Item $OutFile
Oct/082
VMware Stats Oneliner
Today’s oneliner is a nifty little function that reports the average CPU and Memory usage for one or more of your VMs, calculated over the last x hours:
function Get-VMStat
{
param( $VM,[Int32]$Hours = 1 )
$VM | Sort Name |
Select Name, @{N=”CPU”;E={[Math]::Round((($_ |
Get-Stat -Stat cpu.usage.average -Start (Get-Date).AddHours(-$Hours) -IntervalMins 5 -MaxSamples ($Hours*12) |
Measure-Object Value -Average).Average),2)}}, @{N=”MEM”;E={[Math]::Round((($_ |
Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-$Hours) -IntervalMins 5 -MaxSamples ($Hours*12) |
Measure-Object Value -Average).Average),2)}}
}
A great way to use it is like this:
Get-VMStat (Get-VM) -Hours 4 | Where {$_.CPU -gt 50 -or $_.MEM -gt 75}
I’m sure you can figure out what that does. Be the first to give the correct answer in the comments to win everlasting glory! (or at least until the next oneliner…)
Enjoy!