VMware Cloud Community
snap972
Contributor
Contributor

usage CPU/RAM VM

Hi,

How to get back the use of the CPU and the ram of my VM on vcenter with a ps script?

Thanks

Best regards

snap

0 Kudos
27 Replies
nielse
Expert
Expert

Do you want the current or average CPU and RAM usage on your vCenter VM or do you want it on all VM's?

@nielsengelen - http://foonet.be - VCP4/5
0 Kudos
snap972
Contributor
Contributor

I want the current average CPU and RAM usage for each VM's

Thanks you

Snap

0 Kudos
LucD
Leadership
Leadership

Try something like this

Get-VM | 
Get-Stat -Stat "cpu.usage.average","mem.usage.average" -Realtime -MaxSamples 1 | 
Group-Object
-Property {$_.Entity.Name} |
Select @{N="VM";E={$_.Values[0]}},
@{N="Timestamp";E={$_.group[0].Timestamp}},
@{N="CPU";E={$_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Select -ExpandProperty Value}},
@
{N="Memory";E={$_.Group | where {$_.MetricId -eq "mem.usage.average"} | Select -ExpandProperty Value}}


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

0 Kudos
snap972
Contributor
Contributor

Thanks you I check this

Thanks

Snap

0 Kudos
snap972
Contributor
Contributor

HI,

I have this message

"The metric counter "mem.usage.average" doesn't exist for entity"

0 Kudos
LucD
Leadership
Leadership

Do you get that for VMs ? Or only some ?\

Are there any VMs that are powered off perhaps ?


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

0 Kudos
snap972
Contributor
Contributor

Hi !

This is my script

$stat = @()
foreach($vm in Get-VM){
Get-Stat -entity $vm -Stat "cpu.usage.average","mem.usage.average" -Realtime -MaxSamples 1 |
}

I wish export this in a csv ?

I use this

$stat | export-csv "d:\test.csv" -notypeinformation -useculture

but it's not work

0 Kudos
LucD
Leadership
Leadership

You can do it this way

Get-VM | 
Get-Stat -Stat "cpu.usage.average","mem.usage.average" -Realtime -MaxSamples 1 | 
Group-Object -Property {$_.Entity.Name} | 
Select
@{N="VM";E={$_.Values[0]}},
@
{N="Timestamp";E={$_.group[0].Timestamp}},
@{N="CPU";E={$_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Select -ExpandProperty Value}},
@{N="Memory";E={$_.Group | where {$_.MetricId -eq "mem.usage.average"} | Select -ExpandProperty Value}} |
Export-Csv
"d:\test.csv" -NoTypeInformation -UseCulture

Or if you prefer your method, you should do

$stat = @()
foreach($vm in Get-VM){
  $stat += Get-Stat -entity $vm -Stat "cpu.usage.average","mem.usage.average" -Realtime -MaxSamples 1
}
$stat | export-csv "d:\test.csv" -notypeinformation -useculture

Note that this will export all properties to the CSV file, not sure if you want all thos


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

snap972
Contributor
Contributor

Ok thank you a lot !

I check this

Thank you !

0 Kudos
snap972
Contributor
Contributor

It's Ok thank you LucD you're awesome

0 Kudos
snap972
Contributor
Contributor

Hi Lucd,

I wish add this

Get-HardDisk -vm $vm |Select-Object capacitygb

How I do this ?

0 Kudos
LucD
Leadership
Leadership

Try something like this

Get-VM  | 
Get-Stat -Stat "cpu.usage.average","mem.usage.average" -Realtime -MaxSamples 1 | 
Group-Object -Property {$_.Entity.Name} | 
Select @{N="VM";E={$_.Values[0]}}, 
@{N="Timestamp";E={$_.group[0].Timestamp}}, 
@
{N="CPU";E={$_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Select -ExpandProperty Value}},
@
{N="Memory";E={$_.Group | where {$_.MetricId -eq "mem.usage.average"} | Select -ExpandProperty Value}},
@
{N="HD Capacity";E={$_.Group[0].Entity.HardDisks | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum}} | Export-Csv "d:\test.csv" -NoTypeInformation -UseCulture


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

0 Kudos
snap972
Contributor
Contributor

It's OK

Thanks you for youre support

0 Kudos
snap972
Contributor
Contributor

Lucd,

I  try use this

@{N="Disk Use"       ;E={$_.Group[0].Entity.vm      | Measure-Object -Property UsedSpaceGB -sum | Select -ExpandProperty sum}}

for add this command

get-vm -name $vm| Select-Object UsedspaceGB

But it's doesn't Work

Can you help me (again)

Thank you !

0 Kudos
LucD
Leadership
Leadership

That property is not present on the VirtualMachine object.

Try is like this

Get-VM | 
Get-Stat -Stat "cpu.usage.average","mem.usage.average" -Realtime -MaxSamples 1 | 
Group-Object -Property {$_.Entity.Name} | 
Select @{N="VM";E={$_.Values[0]}}, 
@{N="Timestamp";E={$_.group[0].Timestamp}}, 
@
{N="CPU";E={$_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Select -ExpandProperty Value}},
@
{N="Memory";E={$_.Group | where {$_.MetricId -eq "mem.usage.average"} | Select -ExpandProperty Value}},
@
{N="HD Capacity (GB)";E={$_.Group[0].Entity.HardDisks | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum}},
@
{N="HD Uses (GB)";E={[math]::Round($_.Group[0].Entity.ExtensionData.Summary.Storage.Committed/1GB)}} |
Export-Csv
"d:\test.csv" -NoTypeInformation -UseCulture


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

0 Kudos
snap972
Contributor
Contributor

Thank you LuCD,

As you see it I begin in Powershell, can you explain me the script please ?

Thank for your support !

0 Kudos
LucD
Leadership
Leadership

Btw, this is a faster version of the script

$vms = Get-VM  
Get-Stat
-Entity $vms -Stat "cpu.usage.average","mem.usage.average" -Realtime -MaxSamples 1 | Group-Object -Property {$_.Entity.Name} | Select @{N="VM";E={$_.Values[0]}}, @{N="Timestamp";E={$_.group[0].Timestamp}}, @{N="CPU";E={$_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Select -ExpandProperty Value}}, @{N="Memory";E={$_.Group | where {$_.MetricId -eq "mem.usage.average"} | Select -ExpandProperty Value}}, @{N="HD Capacity (GB)";E={[math]::Round($_.Group[0].Entity.ProvisionedSpaceGB)}},
@
{N="HD Uses (GB)";E={[math]::Round($_.Group[0].Entity.UsedSpaceGB)}} |
Export-Csv "d:\test.csv" -NoTypeInformation -UseCulture

Some notes

Let me know if you have further questions concerning the script


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

0 Kudos
snap972
Contributor
Contributor

Hi LuCD,

I finaly understand the scrip and I thank you again and again for your help!

I have an another question,

How I can do To convert the result of this

@{N="Memory Use(GB)"        ;E={$_.Group | where {$_.MetricId -eq "mem.active.average"}   | Select -ExpandProperty Value}},

in GB ?

Thank you !

0 Kudos
LucD
Leadership
Leadership

The metric mem.active.average returns values in KB, so you will have to divide by 1MB to get GB.

Luckily PowerShell has builtin constants for these values.

So you can do

@{N="Memory Use(GB)";E={$_.Group | where {($_.MetricId -eq "mem.active.average"} | Select -ExpandProperty Value)/1MB}}, 

And that will probably produce a rather long number, so it would be better to round it.

Like this

@{N="Memory Use(GB)";E={$_.Group | where {[Math]::Round(($_.MetricId -eq "mem.active.average"} | Select -ExpandProperty Value)/1MB)}}, 


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

0 Kudos