VMware Cloud Community
scriptcaster
Contributor
Contributor

display Total / Used / Avail CPU and memory for ESX hosts?

I have found several ways to display cpu / memory average usage with get-stat, but is there any way to get something closer to what virtualcenter displays for hosts?

Ideally I would like to get the output in MHz and GB if possible too, so the output would be:

I would appreciate any suggestions or pointers to existing examples if this is already done.

Thank you!

0 Kudos
8 Replies
LucD
Leadership
Leadership

I'm not sure I what you mean with the "Avail CPU MHz" and "Avail Memory Gb" columns.

Could you perhaps give a screenshot from the VIC Performance tab what you mean.

For the other columns the following script should produce these values.

The values are exported to a CSV at the end.

$esxImpl = Get-VMHost <ESX-hostname>
$esx = $esxImpl | Get-View

$cpuMHz = ($esx.Hardware.CpuInfo.Hz * $esx.Hardware.CpuInfo.NumCpuCores)/1Mb
$memMb = $esx.Hardware.MemorySize /1Mb

$stats = Get-Stat -Entity $esxImpl -Stat cpu.usagemhz.average,mem.consumed.average -Start (Get-Date).addhours(-1) -IntervalMins 5
$gstats = $stats | Group-Object -Property Timestamp

$report = @()
foreach($entry in $gstats){
  $row = "" | select ESXHostName, Timestamp, TotalCPUMHz, UsedCPUMHz, TotalMemMb, UsedMemMb
  $row.ESXHostName = $esx.Name
  $row.Timestamp = $entry.Values[0]
  $row.TotalCPUMHz = "{0:N2}" -f $cpuMHz
  $row.UsedCPUMHz = "{0:N2}" -f ($entry.Group[0].Value)
  $row.TotalMemMb = "{0:N2}" -f $memMb
  $row.UsedMemMb = "{0:N2}" -f ($entry.Group[1].Value / 1Kb)
  $report += $row
}
$report | Export-Csv "C:\Stats.csv" -NoTypeInformation


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

0 Kudos
scriptcaster
Contributor
Contributor

I did not mean to infer it was column data, I should have stated it was the graphic you see in VC under the summary tab (see attachment) that displays resources.

In the example graphic attached:

117 MHz used of the 8 x 2.333 GHz on the box

523.00MB of Memory of the 4.00 GB

Thanks, and sorry for not making that clear initially.

0 Kudos
LucD
Leadership
Leadership

But that only shows 2 out of the 3 you asked for (I think).

I see what is there (Total ?) and what is used.

Example for the CPU:

Total 8 x 2.333 GHz

Used 177 MHz

What is the Available you mentioned ?


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

0 Kudos
scriptcaster
Contributor
Contributor

You are right, the available is not displayed numerically, only visually in the bar graph.

If I can get the other two (used and total) I should be able to calculate the available (total - used = available), right?

0 Kudos
LucD
Leadership
Leadership

Ok, I think I got it.

The first script will store the values in a CSV file and will capture the data for a specific time interval

$esxImpl = Get-VMHost <ESX-hostname>
$esx = $esxImpl | Get-View

$cpuMHz = ($esx.Hardware.CpuInfo.Hz * $esx.Hardware.CpuInfo.NumCpuCores)/1Mb
$memMb = $esx.Hardware.MemorySize /1Mb

$stats = Get-Stat -Entity $esxImpl -Stat cpu.usagemhz.average,mem.consumed.average -Start (Get-Date).addhours(-1) -IntervalMins 5
$gstats = $stats | Group-Object -Property Timestamp

$report = @()
foreach($entry in $gstats){
  $row = "" | select ESXHostName, Timestamp, TotalCPUMHz, UsedCPUMHz, AvailCPUMHz, TotalMemMb, UsedMemMb, AvailMemMb
  $row.ESXHostName = $esx.Name
  $row.Timestamp = $entry.Values[0]
  $row.TotalCPUMHz = "{0:N2}" -f $cpuMHz
  $row.UsedCPUMHz = "{0:N2}" -f ($entry.Group[0].Value)
  $row.AvailCPUMhz = "{0:N2}" -f ([float]$row.TotalCPUMHz - [float]$row.UsedCPUMHz)
  $row.TotalMemMb = "{0:N2}" -f $memMb
  $row.UsedMemMb = "{0:N2}" -f ($entry.Group[1].Value / 1Kb)
  $row.AvailMemMb = "{0:N2}" -f ([float]$row.TotalMemMb - [float]$row.UsedMemMb)
  $report += $row
}
$report | Export-Csv "C:\Stats.csv" -NoTypeInformation

If you want just 1 line of output with the current values use this script

$esxImpl = Get-VMHost <ESX-hostname>
$esx = $esxImpl | Get-View

$cpuMHz = ($esx.Hardware.CpuInfo.Hz * $esx.Hardware.CpuInfo.NumCpuCores)/1Mb
$memMb = $esx.Hardware.MemorySize /1Mb

$stats = Get-Stat -Entity $esxImpl -Stat cpu.usagemhz.average,mem.consumed.average -MaxSamples 1
$gstats = $stats | Group-Object -Property Timestamp

$report = @()
foreach($entry in $gstats){
  $row = "" | select ESXHostName, Timestamp, TotalCPUMHz, UsedCPUMHz, AvailCPUMHz, TotalMemMb, UsedMemMb, AvailMemMb
  $row.ESXHostName = $esx.Name
  $row.Timestamp = $entry.Values[0]
  $row.TotalCPUMHz = "{0:N2}" -f $cpuMHz
  $row.UsedCPUMHz = "{0:N2}" -f ($entry.Group[0].Value)
  $row.AvailCPUMhz = "{0:N2}" -f ([float]$row.TotalCPUMHz - [float]$row.UsedCPUMHz)
  $row.TotalMemMb = "{0:N2}" -f $memMb
  $row.UsedMemMb = "{0:N2}" -f ($entry.Group[1].Value / 1Kb)
  $row.AvailMemMb = "{0:N2}" -f ([float]$row.TotalMemMb - [float]$row.UsedMemMb)
  $report += $row
}
$report | ft


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

0 Kudos
scriptcaster
Contributor
Contributor

Thanks, this is great, but I having one error in both versions:

3/11/2009 3:21:03 PM Get-Stat The metric counter "mem.consumed.average" doesn't exist for entity "ds-tse-i116.dsl.test.com".

At :line:9 char:17

+ $stats = Get-Stat &lt;&lt;&lt;&lt; -Entity $esxImpl -Stat cpu.usagemhz.average,mem.consumed.average -Start (Get-Date).addhours(-1) -IntervalMins 5

and therefore my used memory field is always zero.

Something I am doing wrong? I appreciate your help and patience with this!

Thank you!

0 Kudos
LucD
Leadership
Leadership

Could be that the metric mem.consumed.average is not collected for your ESX server.

What version of VC and ESX are you running ?


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

0 Kudos
scriptcaster
Contributor
Contributor

I have ESX 3.5 update 1 and VC 2.5.

0 Kudos