bottomofbarrel
Contributor
Contributor

powercli script to capture cpu & mem usage stats

i am new to powercli and am not a good scriptor yet however, i need a script that will capture the max, min, avg cpu & mem usage stats per esxhost & vm for the past month and import into excel so i can create a pivot chart. key is i need to see the cpu/mem usage data per vm per host. can anyone help me with this. i have spent the last two weeks trying to work with powercli to do this and i am unable produce the results i want.

171 Replies
LucD
Leadership
Leadership

I'm not sure I see the issue.

Let's take Server A, a VM to which you assigned 32GB of memory.

The mem.usage.average comes back with 9.99%.

That means that guest OS inside the VM is actively using 9.99%, or 3.2GB, of the configured memory.

memusage.png


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

0 Kudos
marianofmaquiei
Contributor
Contributor

Lucd!!

I can't thank you enough for all this help!!

You are definitely right, the thing is that in task manager I see 78% - 25GB used out of 32GB and I wanted to retrieve that value…

I have used them all, with no success

1-            Active: 2348808, 2013264 (kb)  2.23GB

2-            Usage: 6.99, 5.99 (%)

3-            Consumed: 33539760, 33539760 (kb) 31.98GB

4-            Granted: 33554432, 33554432 (kb)    32GB

5-            Active: 2348808, 2013264 (kb)   2.23GB

I believe I should be retrieving more like ‘demand’ values….

Can I use mem.demand.average? exists? I believe not…

Thanks once again for all the help!

0 Kudos

Hi LucD

I've just been pulling stats out of the vCenter Database, and need to go back, to 20 November 2014 - Present, to look at history and performance of a VM.

But I've noticed, that I can only retrieve data for the last 31 days.... and I need to go to 76 days!

this is the same with all VMs.

The data is present, when I look at charts in vCenter.

Any thoughts?

Andy

0 Kudos
LucD
Leadership
Leadership

If the data is visible in the vSphere Client, it is in the vCenter DB.

So you should be able to retrieve it.

Could you show me relevant code you are using to retrieve the statistical data ?


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

0 Kudos

it's your script in this thread but I changed 7 to a 76!

0 Kudos
LucD
Leadership
Leadership

Some things you can check:

  • in the vSphere client you see data for the actual metrics you are using in the script.
  • the statistics level for that Historical Interval cover the level required for the specific metrics you are using
  • there is data for the start of the interval in the vSphere client. Get-Stat skips an entity when it doesn't find data for the start, although there might be data for a day later


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

0 Kudos

Yes, to all those, it's odd that it starts collecting data after 04/01/2015 for all VMs, but not before.

just trying another site!

0 Kudos
LucD
Leadership
Leadership

You could try the Stats Toolbox from Stats Toolbox – A vSphere Server Performance Counter tool.

It contains an option to check the oldest available counters of a specific metric for a specific entity.


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

0 Kudos

very odd this....

this is my code

Connect-VIServer 172.16.138.110 -User Administrator -Password donkeykong

$allvms = @()

$vms = "vcenter"

$start = (Get-Date).AddDays(-76)

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

$stats = Get-Stat -Entity $vms -Start $start -Stat $metrics 

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

  $vmstat = "" | Select VmName, Day, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin

  $vmstat.VmName = $_.Values[1]

  $vmstat.Day = $_.Group[0].Timestamp.Date

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

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

  $vmstat.CPUMax = [int]$cpu.Maximum

  $vmstat.CPUAvg = [int]$cpu.Average

  $vmstat.CPUMin = [int]$cpu.Minimum

  $vmstat.MemMax = [int]$mem.Maximum

  $vmstat.MemAvg = [int]$mem.Average

  $vmstat.MemMin = [int]$mem.Minimum

  $allvms += $vmstat

}

$allvms |

Export-Csv "c:\utils\scripts\cyrus.csv" -noTypeInformation

it's just cut and paste of your code here, with days changed to 76.

I've now tried this on 10 different vCenter 5.5 Build 1750787 , and also looked with Toolbox Stats, you mentioned that data is there, BUT...

all start the first collected entry at 04/01/2015, although the start date is correct, and start date for collection is calculated as 20/11/2014!

and if I check on the system I want the data from the oldest data is the 20/11/2014, which is what we are trying to investigate, is the performance of the VM, from first implementation!

although this is for a single VM, it's the same for ALL VMs, all data starts at 04/01/2015!

very odd.....

Puzzled....

0 Kudos
LucD
Leadership
Leadership

I assume all vCenters and your station are configured for the culture EN-US ?

If the Stat Toolbox comes back with the correct oldest date, it means the data is actually there.

The only difference is that my Stats Toolbox uses the QueryPerf method instead of the Get-Stat cmdlet.

It could be a Get-Stat feature :smileygrin:

Which PowerCLI version are you running ?

Perhaps you could try with my Get-Stat2 function ?


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

0 Kudos

PowerCLI 5.5 Release 1

0 Kudos
Jellas
Enthusiast
Enthusiast

Hi Team,

Please help me here as I'm getting below error.

Connect-VIServer : 16/02/2015 15:50:25    Connect-VIServer        Cannot complete login due to an incorrect user name or password.

At C:\Users\Test\Desktop\test.ps1:1 char:17 + Connect-VIServer <<<<  esx1.local  -User root -Password mypassword    + CategoryInfo : NotSpecified: (:) [Connect-VIServer], InvalidLog

   in     + FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_Reconnect_Exception,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer

What I have tried:

  • Login to the Jump box and copy pasted the script and saved as test.ps1 on the desktop.
  • Now opened the vmwarepowercli as runasadministrator.
  • now drag and droped the test.ps1 file from desktop
  • hit the enter.got above mnentioned error.

Please help me how to get ride of this error.

Environemnet details:

  1. Vmwarevpsherepowercli 5.1
  2. ESXi host 4.1 and also tried on 5.x

Thanks

Sravana

0 Kudos
LucD
Leadership
Leadership

It looks as if the user/password you passed is not correct


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

0 Kudos
Jellas
Enthusiast
Enthusiast

using the same credentials,i'm able to login to the esx host through putty.

0 Kudos
LucD
Leadership
Leadership

Are there perhaps any special characters in the password ?

Try placing the password between single quotes.


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

0 Kudos
hareshn
Enthusiast
Enthusiast

Hello LucD,

can you help me to get stats for only Poweron VMs using below script?  when i run below script it getting stuck on one of the poweroff vm.

"

Connect-VIServer vcenter

$allvms = @()

$vms = Get-Vm

$stats = Get-Stat -Entity $vms -start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat "cpu.usage.average","mem.usage.average"

$stats | Group-Object -Property Entity | %{

  $vmstat = "" | Select VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin

  $vmstat.VmName = $_.name

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

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

  $vmstat.CPUMax = [int]$cpu.Maximum

  $vmstat.CPUAvg = [int]$cpu.Average

  $vmstat.CPUMin = [int]$cpu.Minimum

  $vmstat.MemMax = [int]$mem.Maximum

  $vmstat.MemAvg = [int]$mem.Average

  $vmstat.MemMin = [int]$mem.Minimum

  $allvms += $vmstat

}

$allvms | Select VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | Export-Csv "c:\ISO\VMs.csv" -noTypeInformation"

0 Kudos
LucD
Leadership
Leadership

You can use a Where-clause like this

$vms = Get-Vm | where {$_.PowerState -eq 'poweredon'}


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

0 Kudos
hareshn
Enthusiast
Enthusiast

Thanks a lot for above script.

one more request, i am using following script to generate Host utilization on monthly basis. Is there way where i can put or get only business hour utilization for the esxi host?

Connect-VIServer

$allvms = @()

$allhosts = @()

$hosts = Get-VMHost

$vms = Get-Vm

foreach($vmHost in $hosts){

  $hoststat = "" | Select HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin

  $hoststat.HostName = $vmHost.name

 

  $statcpu = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat cpu.usage.average

  $statmem = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat mem.usage.average

  $cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum

  $mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum

 

  $hoststat.CPUMax = $cpu.Maximum

  $hoststat.CPUAvg = $cpu.Average

  $hoststat.CPUMin = $cpu.Minimum

  $hoststat.MemMax = $mem.Maximum

  $hoststat.MemAvg = $mem.Average

  $hoststat.MemMin = $mem.Minimum

  $allhosts += $hoststat

}

$allhosts | Select HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | Export-Csv "c:\ISO\Hosts.csv" -noTypeInformation

0 Kudos
vijayrana968
Virtuoso
Virtuoso

This can be done by executing below script...

Get-VMHost 'Name of Host you want to check' | Get-VM |

Select Name, Host, NumCpu, MemoryMB, `

@{N="Cpu.UsageMhz.Average";E={[Math]::Round((($_ |Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}}, `

@{N="Mem.Usage.Average";E={[Math]::Round((($_ |Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}} `

| Export-Csv -Path c:\Reports\stats.csv

____________________________________________________________________________________________________________________________________________________________________________________________________

Note :

(Get-Date).AddHours(-24) - Add number of days per your requirement.

0 Kudos
hareshn
Enthusiast
Enthusiast

Thanks but where i should specify the time interval ?

like 9 AM to 7 PM ??

above script will give me 24hrs data. i need only utilization only for business hours for month.

0 Kudos