Quantcast
Channel: VMware Communities : All Content - VMware vCenter™
Viewing all articles
Browse latest Browse all 24426

poweshell script to retrieve metrics from vCenter

$
0
0

Hi Community,

 

I am new to VMWare and my first task is to retrieve data from vCenters, CPU, Memory.

 

I would like to connect to vCenters every hour, retrieve the data, my data should be 5 minute aggreate and the column should be this:

 

TimeDate, VMName, Max_CPU, Max_Memory

 

Since this is a very hot topic, I was hoping that there is already a script out there to do this.  I do have a script but it is taking a very long time and the data is raw and it is for past day. I need this script to be last hour, with 5 minute aggregated data. I'd really appreciate any insight/help. Regards,

 

This is my existing script now:

 

============================

Connect-VIServer -server vCenter1.example.com -User userid123 -Password pass123

$allvms = @()

$allhosts = @()

$hosts = Get-VMHost

$vms = Get-VM | Where-Object {$_.PowerState -eq "PoweredOn"}

 

 

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(-1) -Finish (Get-Date)-MaxSamples 1 -stat cpu.usage.average

  $statmem = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 1 -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:\output\Hosts.csv" -noTypeInformation

 

 

foreach($vm in $vms){

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

  $vmstat.VmName = $vm.name

 

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

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

 

 

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

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

 

  $vmstat.CPUMax = $cpu.Maximum

  $vmstat.CPUAvg = $cpu.Average

  $vmstat.CPUMin = $cpu.Minimum

  $vmstat.MemMax = $mem.Maximum

  $vmstat.MemAvg = $mem.Average

  $vmstat.MemMin = $mem.Minimum

  $allvms += $vmstat

}

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


Viewing all articles
Browse latest Browse all 24426

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>