I'm trying to determine the average VM resource usage for a bunch of VM's. My main goal is determine which VMs can have their cores/memory reduced. I'm trying to run the below script, but I'm getting the same error for each VM "The metric counter "disk.usage.average" doesn't exist for entity".
Any ideas on how I can correct the error or get average resource usage a different way?
Connect-VIServer <Server Name Redacted>
$allvms = @()
$vms = Get-Vm
$stats = Get-Stat -Entity $vms -start (get-date).AddDays(-7) -Finish (Get-Date)-MaxSamples 10000 -stat "cpu.usage.average","mem.usage.average"
$stats | Group-Object -Property Entity | %{
$vmstat = "" | Select VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
$vmstat.VmName = $_.name
$cpu = $_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property value -Average -Maximum -Minimum
$mem = $_.Group | where {$_.MetricId -eq "mem.usage.average"} | Measure-Object -Property value -Average -Maximum -Minimum
$vmstat.CPUMax = [int]$cpu.Maximum
$vmstat.CPUAvg = [int]$cpu.Average
$vmstat.CPUMin = [int]$cpu.Minimum
$vmstat.MemMax = [int]$mem.Maximum
$vmstat.MemAvg = [int]$mem.Average
$vmstat.MemMin = [int]$mem.Minimum
$allvms += $vmstat
}
$allvms | Select VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | Export-Csv "c:\VM_Resources\VMs.csv" -noTypeInformation
There can be a couple of reasons for this message.
- your VCSA doesn't retain the statistical data long enough
- the VM might have been powered off at the start of the specified interval
- the statistical level on your VCSA might not include a specific metric
Since you state you have the same error for each VM I suspect the 1st bullet might apply.
Can you make the requested interval shorter (for example 1 day)?
Does that return data?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
There can be a couple of reasons for this message.
- your VCSA doesn't retain the statistical data long enough
- the VM might have been powered off at the start of the specified interval
- the statistical level on your VCSA might not include a specific metric
Since you state you have the same error for each VM I suspect the 1st bullet might apply.
Can you make the requested interval shorter (for example 1 day)?
Does that return data?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The same error is present after changing the interval to 1 day.
Did you check what the retention period for statistical data is?
Are the statistical data aggregation jobs on the VCSA running correctly?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
These statistics levels look good (they are the defaults), meaning you should be able to get statistical data for the past week.
Do you see that data in the Web client for 1 of the VMs where the Get-Stat fails?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
After looking at it again, changing the interval from 30 to 7 days, it is now able to create the CSV file. The errors are only coming from VM's that are powered off. You have resolved my issue. Thanks for the help!