VMware Cloud Community
amor123
Enthusiast
Enthusiast
Jump to solution

powercli cpu,mem

Attempt to save vm cpu,mem min, average, max.

I want to do this.

January vm cpu - min,average,max mem - min average max

February vm cpu - min,average,max mem - min average max

March vm cpu - min,average,max mem - min average max

...

December - vm cpu - min,average,max mem - min average max

I've made this right now.

Please amend me.

$report = @()
$metrics = "cpu.usagemhz.average","mem.active.average"
$vms = Get-Vm | where {$_.PowerState -eq "PoweredOn"}
$start = (get-date).AddDays(-365)

Get-Stat -Entity ($vms) -start $start -stat $metrics | `
  Group-Object -Property EntityId | %{
    $row = ""| Select VmName, vCPU, MinCpu,AvgCpu,MaxCpu,MemAlloc,MinMem,AvgMem,MaxMem
    $row.VmName = $_.Group[0].Entity.Name
    $row.vCPU = $_.Group[0].Entity.NumCpu
    $cpuStat = $_.Group | where {$_.MetricId -eq "cpu.usagemhz.average"} | Measure-Object -Property Value -Minimum -Maximum -Average
    $row.MinCpu = "{0:f2}" -f ($cpuStat.Minimum)
    $row.AvgCpu = "{0:f2}" -f ($cpuStat.Average)
    $row.MaxCpu = "{0:f2}" -f ($cpuStat.Maximum)
    $row.MemAlloc = $_.Group[0].Entity.MemoryMB
    $memStat = $_.Group | where {$_.MetricId -eq "mem.active.average"} | Measure-Object -Property Value -Minimum -Maximum -Average
    $row.MinMem = "{0:f2}" -f ($memStat.Minimum)
    $row.AvgMem = "{0:f2}" -f ($memStat.Average)
    $row.MaxMem = "{0:f2}" -f ($memStat.Maximum)
    $report += $row
}
$report | Export-Csv "C:\VM-stats1.csv" -NoTypeInformation -UseCulture

Please help me.

Is there a way?

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Add a 2nd property on the Group-Object cmdlet.

Something like this

$report = @()

$metrics = "cpu.usagemhz.average","mem.active.average"

$vms = Get-Vm | where {$_.PowerState -eq "PoweredOn"}

$start = (get-date).AddDays(-365)

Get-Stat -Entity ($vms) -start $start -stat $metrics |

Group-Object -Property {$_.Entity.Name},{$_.Timestamp.Month} | %{

    $row = ""| Select VmName, Month, vCPU, MinCpu,AvgCpu,MaxCpu,MemAlloc,MinMem,AvgMem,MaxMem

    $row.Month = (Get-Culture).DateTimeFormat.GetMonthName($_.Name.Split(',')[1])

    $row.VmName = $_.Name.Split(',')[0]

    $row.vCPU = $_.Group[0].Entity.NumCpu

    $cpuStat = $_.Group | where {$_.MetricId -eq "cpu.usagemhz.average"} | Measure-Object -Property Value -Minimum -Maximum -Average

    $row.MinCpu = "{0:f2}" -f ($cpuStat.Minimum)

    $row.AvgCpu = "{0:f2}" -f ($cpuStat.Average)

    $row.MaxCpu = "{0:f2}" -f ($cpuStat.Maximum)

    $row.MemAlloc = $_.Group[0].Entity.MemoryMB

    $memStat = $_.Group | where {$_.MetricId -eq "mem.active.average"} | Measure-Object -Property Value -Minimum -Maximum -Average

    $row.MinMem = "{0:f2}" -f ($memStat.Minimum)

    $row.AvgMem = "{0:f2}" -f ($memStat.Average)

    $row.MaxMem = "{0:f2}" -f ($memStat.Maximum)

    $report += $row

}

$report | Export-Csv "C:\VM-stats1.csv" -NoTypeInformation -UseCulture


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

View solution in original post

21 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$report = @()

$metrics = "cpu.usagemhz.average","mem.active.average"

$vms = Get-Vm | where {$_.PowerState -eq "PoweredOn"}

$start = (get-date).AddDays(-365)

Get-Stat -Entity ($vms) -start $start -stat $metrics |

Group-Object -Property {$_.Timestamp.Month} | %{

    $row = ""| Select VmName, Month, vCPU, MinCpu,AvgCpu,MaxCpu,MemAlloc,MinMem,AvgMem,MaxMem

    $row.Month = (Get-Culture).DateTimeFormat.GetMonthName($_.Name)

    $row.VmName = $_.Group[0].Entity.Name

    $row.vCPU = $_.Group[0].Entity.NumCpu

    $cpuStat = $_.Group | where {$_.MetricId -eq "cpu.usagemhz.average"} | Measure-Object -Property Value -Minimum -Maximum -Average

    $row.MinCpu = "{0:f2}" -f ($cpuStat.Minimum)

    $row.AvgCpu = "{0:f2}" -f ($cpuStat.Average)

    $row.MaxCpu = "{0:f2}" -f ($cpuStat.Maximum)

    $row.MemAlloc = $_.Group[0].Entity.MemoryMB

    $memStat = $_.Group | where {$_.MetricId -eq "mem.active.average"} | Measure-Object -Property Value -Minimum -Maximum -Average

    $row.MinMem = "{0:f2}" -f ($memStat.Minimum)

    $row.AvgMem = "{0:f2}" -f ($memStat.Average)

    $row.MaxMem = "{0:f2}" -f ($memStat.Maximum)

    $report += $row

}

$report | Export-Csv "C:\VM-stats1.csv" -NoTypeInformation -UseCulture


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

amor123
Enthusiast
Enthusiast
Jump to solution

pastedImage_0.png

One virtual machine

pastedImage_1.png

two virtual machine

Why does the month come in April?

how so?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Add a 2nd property on the Group-Object cmdlet.

Something like this

$report = @()

$metrics = "cpu.usagemhz.average","mem.active.average"

$vms = Get-Vm | where {$_.PowerState -eq "PoweredOn"}

$start = (get-date).AddDays(-365)

Get-Stat -Entity ($vms) -start $start -stat $metrics |

Group-Object -Property {$_.Entity.Name},{$_.Timestamp.Month} | %{

    $row = ""| Select VmName, Month, vCPU, MinCpu,AvgCpu,MaxCpu,MemAlloc,MinMem,AvgMem,MaxMem

    $row.Month = (Get-Culture).DateTimeFormat.GetMonthName($_.Name.Split(',')[1])

    $row.VmName = $_.Name.Split(',')[0]

    $row.vCPU = $_.Group[0].Entity.NumCpu

    $cpuStat = $_.Group | where {$_.MetricId -eq "cpu.usagemhz.average"} | Measure-Object -Property Value -Minimum -Maximum -Average

    $row.MinCpu = "{0:f2}" -f ($cpuStat.Minimum)

    $row.AvgCpu = "{0:f2}" -f ($cpuStat.Average)

    $row.MaxCpu = "{0:f2}" -f ($cpuStat.Maximum)

    $row.MemAlloc = $_.Group[0].Entity.MemoryMB

    $memStat = $_.Group | where {$_.MetricId -eq "mem.active.average"} | Measure-Object -Property Value -Minimum -Maximum -Average

    $row.MinMem = "{0:f2}" -f ($memStat.Minimum)

    $row.AvgMem = "{0:f2}" -f ($memStat.Average)

    $row.MaxMem = "{0:f2}" -f ($memStat.Maximum)

    $report += $row

}

$report | Export-Csv "C:\VM-stats1.csv" -NoTypeInformation -UseCulture


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

amor123
Enthusiast
Enthusiast
Jump to solution

Why only April data?

Why isn't there data on March or February?

pastedImage_1.png

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Works for me.

Must be something with the regional settings you are using.

Does this return all the 12 months correctly?

1..12 | %{

    (Get-Culture).DateTimeFormat.GetMonthName($_)   

}


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

amor123
Enthusiast
Enthusiast
Jump to solution

pastedImage_0.png

Exactly

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you actually keep the statistical data for 12 months?

Check under vCenter Server Settings - General - Statistics

stat.png


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

amor123
Enthusiast
Enthusiast
Jump to solution

I can't find the route.

I am using vcenter 6.0.

Can you tell me where menu is?

and

pastedImage_0.png

Can I ignore it?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

In the vSphere Client, the path is Administration - vCenter Server Settings - Statistics.

In the Web Client the path is Select the vCenter in the Navigator frame, then Actions - Settings - General - Statistics.

The warning indicates that the memory related counter is not available for the requested time interval.

This is most probably caused by the Statistics Level that is defined in the same dialog as the one above (which is also for the Retention period ('Save For'))

This could also be caused by the fact that some VMs were powered off for a certain time in the past, and hence there is no statistical data.

You can add 'ErrorAction SilentlyContinue' on the Get-Stat cmdlet to suppress the message.

A quick check, in the vSphere Client or the Web Client, do you see data for the memory counter for the past year? For all VMs?


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

amor123
Enthusiast
Enthusiast
Jump to solution

I'm sorry.

Run on vm without performance data

It runs normally.

thank you

Have something to ask

From the performance data listed below

Is the memory unit in kilobytes?

cpu mhz?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The unit for each metric is in the returned object.

See the Unit property.

$vm = Get-VM

Get-Stat -Entity $vm -Realtime -MaxSamples 1 |

Select *


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

amor123
Enthusiast
Enthusiast
Jump to solution

sorry but

I have one more question to ask.

Not a vm.

I want to view the cpu of the host server, memory, and so on.

How should I change it?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Just pass VMHost objects on the Entity parameter.

$esx = Get-VMHost

Get-Stat -Entity $esx -Realtime -MaxSamples 1


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

amor123
Enthusiast
Enthusiast
Jump to solution

$report = @()

$metrics = "cpu.usage.average","mem.usage.average"

$esx = Get-VMHost

$start = (get-date).AddDays(-365)

Get-Stat -Entity $esx -Realtime -MaxSamples 1 |

Group-Object -Property {$_.Entity.Name},{$_.Timestamp.Month} | %{

    $row = ""| Select HostName, Month, vCPU, MinCpu,AvgCpu,MaxCpu,MemAlloc,MinMem,AvgMem,MaxMem

    $row.Month = (Get-Culture).DateTimeFormat.GetMonthName($_.Name.Split(',')[1])

    $row.HostName = $_.Name.Split(',')[0]

    $row.vCPU = $_.Group[0].Entity.NumCpu

    $cpuStat = $_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property Value -Minimum -Maximum -Average

    $row.MinCpu = "{0:f2}" -f ($cpuStat.Minimum)

    $row.AvgCpu = "{0:f2}" -f ($cpuStat.Average)

    $row.MaxCpu = "{0:f2}" -f ($cpuStat.Maximum)

    $row.MemAlloc = $_.Group[0].Entity.MemorytotalMB

    $memStat = $_.Group | where {$_.MetricId -eq "mem.usage.average"} | Measure-Object -Property Value -Minimum -Maximum -Average

    $row.MinMem = "{0:f2}" -f ($memStat.Minimum)

    $row.AvgMem = "{0:f2}" -f ($memStat.Average)

    $row.MaxMem = "{0:f2}" -f ($memStat.Maximum)

    $report += $row

}

$report | Export-Csv "C:\VM-statstest.csv" -NoTypeInformation -UseCulture

Is it correct?

Unable to confirm because there are no test servers present at this time.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You will only need to change the MemAlloc line

$row.MemAlloc = $_.Group[0].Entity.MemoryTotalGB


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

amor123
Enthusiast
Enthusiast
Jump to solution

$report = @()

$metrics = "cpu.usage.average","mem.usage.average"

$esx = Get-VMHost

$start = (get-date).AddDays(-365)

Get-Stat -Entity $esx -Realtime -MaxSamples 1 |

Group-Object -Property {$_.Entity.Name},{$_.Timestamp.Month} | %{

    $row = ""| Select HostName, Month, vCPU, MinCpu,AvgCpu,MaxCpu,MemAlloc,MinMem,AvgMem,MaxMem

    $row.Month = (Get-Culture).DateTimeFormat.GetMonthName($_.Name.Split(',')[1])

    $row.HostName = $_.Name.Split(',')[0]

    $row.vCPU = $_.Group[0].Entity.NumCpu

    $cpuStat = $_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property Value -Minimum -Maximum -Average

    $row.MinCpu = "{0:f2}" -f ($cpuStat.Minimum)

    $row.AvgCpu = "{0:f2}" -f ($cpuStat.Average)

    $row.MaxCpu = "{0:f2}" -f ($cpuStat.Maximum)

    $row.MemAlloc = $_.Group[0].Entity.MemoryTotalGB

    $memStat = $_.Group | where {$_.MetricId -eq "mem.usage.average"} | Measure-Object -Property Value -Minimum -Maximum -Average

    $row.MinMem = "{0:f2}" -f ($memStat.Minimum)

    $row.AvgMem = "{0:f2}" -f ($memStat.Average)

    $row.MaxMem = "{0:f2}" -f ($memStat.Maximum)

    $report += $row

}

$report | Export-Csv "C:\VM-stats(test).csv" -NoTypeInformation -UseCulture

I took a test today

The result is only the result of April.

Why is that?

I want to see the result every month like a vm.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

In that case the Get-Stat line should read

Get-Stat -Entity $esx -Start $start |


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

Reply
0 Kudos
amor123
Enthusiast
Enthusiast
Jump to solution

$report = @()

$metrics = "cpu.usage.average","mem.usage.average"

$esx = Get-VMHost

$start = (get-date).AddDays(-365)

Get-Stat -Entity $esx -Start $start |

Group-Object -Property {$_.Entity.Name},{$_.Timestamp.Month} | %{

    $row = ""| Select HostName, Month, vCPU, MinCpu,AvgCpu,MaxCpu,MemAlloc,MinMem,AvgMem,MaxMem

    $row.Month = (Get-Culture).DateTimeFormat.GetMonthName($_.Name.Split(',')[1])

    $row.HostName = $_.Name.Split(',')[0]

    $row.vCPU = $_.Group[0].Entity.NumCpu

    $cpuStat = $_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property Value -Minimum -Maximum -Average

    $row.MinCpu = "{0:f2}" -f ($cpuStat.Minimum)

    $row.AvgCpu = "{0:f2}" -f ($cpuStat.Average)

    $row.MaxCpu = "{0:f2}" -f ($cpuStat.Maximum)

    $row.MemAlloc = $_.Group[0].Entity.MemoryTotalGB

    $memStat = $_.Group | where {$_.MetricId -eq "mem.usage.average"} | Measure-Object -Property Value -Minimum -Maximum -Average

    $row.MinMem = "{0:f2}" -f ($memStat.Minimum)

    $row.AvgMem = "{0:f2}" -f ($memStat.Average)

    $row.MaxMem = "{0:f2}" -f ($memStat.Maximum)

    $report += $row

}

$report | Export-Csv "C:\VM-stats(test).csv" -NoTypeInformation -UseCulture

Is this right?

I want to test, but I don't have a current environment.

I need a definite answer.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That should work as far as I can tell


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

Reply
0 Kudos