Getting Average Usage from your hosts - PowerCLI
I was asked to provide a small report on what the average RAM usage was on each host over the past 30 days
I forgot to mention the code was inspired by the session that Luc Dekens and Alan Renouf gave at VMworld.
Of course you could do it with mouse clicks – but man I love PowerCLI. 10 lines of code. less than 3 minutes and BAM!
$start = (Get-Date).AddDays(-30)
$finish = Get-Date
$metrics = "mem.usage.average"
Get-Datacenter | ForEach-Object{
Get-VMHost -location $_ | Sort-Object Name | ForEach-Object {
Write-Host $_.Name
$stats = ""
$stats = get-stat -Entity $_ -Stat $metrics -Start $start -Finish $finish
Write-Host "$($_) average Memory Usage over the last month is: $("{0:N2}" -f ($stats | Measure-Object -Property Value -Average).Average)%"
}
}
1-2. Set start variable with the date I need (30 days ago) and finish with today’s date
3. This is the metric I am looking for.
5-6. Here we get each Host, I divided it up by datacenter/cluster to show that all hosts are (should be) balanced and using the same amount of resources and we perform the action on each host.
7-8. First clear the variable, from any previous runs, and set the stats variable with the information I want.
9. Here I used the .Net format for numbering “{0:N2}” -f otherwise I received a number which had 10 digits after the decimal point (which is pointless .. ;) )
And I got this output
Dev
esx2.maishsk.local average Memory Usage over the last month is: 82.46%
esx3.maishsk.local average Memory Usage over the last month is: 79.20%
esx5.maishsk.local average Memory Usage over the last month is: 79.04%
esx6.maishsk.local average Memory Usage over the last month is: 72.57%
esx7.maishsk.local average Memory Usage over the last month is: 77.97%
Of course you can do this with any metric as well.
Time to add more hosts to the Cluster (me thinks)….