VMware Cloud Community
salasos3
Enthusiast
Enthusiast

VM metrics I/O, CPU, MEM

Hi all,

I have the below script to get Avg CPU Usage and Avg Memory Usage from a VM:

 

Get-VM -Name vmname | Select Name, vmHost,

@{N="CPU Usage (Average), Mhz" ; E={[Math]::Round((($_ | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object Value -Average).Average),2)}}, `

@{N="Memory Usage (Average), %" ; E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object Value -Average).Average),2)}}

 

How can I add the below ones?

Peak CPU Usage

Peak Memory Usage

Avg Disk I/O Usage

Peak Disk I/O Usage

Labels (3)
0 Kudos
3 Replies
LucD
Leadership
Leadership

You can use the Get-StatType cmdlet to find the available metrics.

And you can consult the PerformanceManager page to find further info on the metrics.


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

0 Kudos
salasos3
Enthusiast
Enthusiast

Ok this is what I have now:

 

Get-VM -Name vmname | Select Name, vmHost,

@{N="CPU Usage (Average), Mhz" ; E={[Math]::Round((($_ | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object Value -Average).Average),2)}},
@{N="CPU Usage (Peak), %" ; E={[Math]::Round(($_ | Get-Stat -Stat cpu.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object -Property Value -Maximum | Select -ExpandProperty Maximum))}},

@{N="Memory Usage (Average), %" ; E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object Value -Average).Average),2)}},
@{N="Memory Usage (Peak), %" ; E={[Math]::Round(($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object -Property Value -Maximum | Select -ExpandProperty Maximum))}},

@{N="Disk Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat disk.usage.average -Start (Get-Date).AddDays(-30) -IntervalSecs 86400 | Measure-Object Value -Average).Average),2)}},
@{N="Disk Usage (Peak), KBps" ; E={[Math]::Round(($_ | Get-Stat -Stat disk.usage.average -Start (Get-Date).AddDays(-30) -IntervalSecs 86400 | Measure-Object -Property Value -Maximum | Select -ExpandProperty Maximum))}}

 

I'm not getting any errors, but do you see something wrong or any improvement area?

Just want to make sure I'm getting the right results.

0 Kudos
LucD
Leadership
Leadership

Your script will work, but there is some room for improvement.
You are calling Get-Stat for each VM for each metric.
A faster way would be to call Get-Stat only once for all metrics.

And the use Group-Object to split out the results per VM.

There are several examples of this method in this community, one of them is for example Solved: Re: Using PowerCLI to get VM Disk Stats - VMware Technology Network VMTN


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

0 Kudos