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.
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.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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!
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
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
it's your script in this thread but I changed 7 to a 76!
Some things you can check:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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!
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
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....
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
PowerCLI 5.5 Release 1
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:
Please help me how to get ride of this error.
Environemnet details:
Thanks
Sravana
It looks as if the user/password you passed is not correct
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
using the same credentials,i'm able to login to the esx host through putty.
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
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"
You can use a Where-clause like this
$vms = Get-Vm | where {$_.PowerState -eq 'poweredon'}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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.
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.