Quantcast
Channel: VMware Communities : All Content - All Communities
Viewing all articles
Browse latest Browse all 195209

Cpu data gathering script - need help optimizing it.

$
0
0

Hello

 

This is a follow up on this topic:

http://communities.vmware.com/message/2192431

 

The script requires Powershell 3 and PowerCLI v5.1 Release 2.

 

Also advice from here was used:

 

http://vnugglets.com/2011/11/speed-up-first-powercli-5-cmdlet.html

 

The script connects to x number of hosts simultaneously, gathers cpu info for hosts and VMs and saves to csv files. It does this the $counter amount of times, the Start-Sleep -s 10 is to make the check less frequent.

Is there something I could remove or replace to make it less resource heavy? Please offer suggestions.
I know $metrics is currently
useless

 


$Hosts = @(
"xx.xx.xx.1",
"xx.xx.xx.2",
"xx.xx.xx.3")
$metrics = "cpu.usage.average"
$counter = 5
foreach ($Server in $Hosts){     Start-Job -ScriptBlock {           param ($Server, $metrics, $counter)          Add-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue          Connect-VIServer -Server $Server -user xxx -password "xxx" | Out-Null          $vms = Get-Vm -Server $Server | where {$_.PowerState -eq "PoweredOn"}          for ($i=0; $i -lt $counter; $i++){               $alldata = @()               $stats = Get-Stat -Entity $Server -Realtime -Stat $metrics -MaxSamples 1               $stats | Group-Object -Property Entity | %{                    $hoststat = "" | Select Date, HostName, VMName, CPUAvg                    $hoststat.Date =Get-date                    $hoststat.HostName = $_.name                    $cpu = $_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property value -Average                    $hoststat.CPUAvg = [int]$cpu.Average                    $hostname = $hoststat.HostName                    $alldata += $hoststat               }               $stats = Get-Stat -Entity $vms -Realtime -Stat $metrics -MaxSamples 1                $stats | Group-Object -Property Entity | %{                    $vmstat = "" | Select Date, HostName, VmName, CPUAvg                    $vmstat.Date = Get-date                     $vmstat.VmName = $_.name                    $cpu = $_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property value -Average                    $vmstat.CPUAvg = [int]$cpu.Average                    $vmstat.HostName = $hostname                    $alldata += $vmstat               }               $alldata | Export-Csv "c:\tmp\$Server.csv" -Append -noTypeInformation               Clear-Variable -Name alldata               Start-Sleep -s 10          }          Disconnect-VIServer * -Force -Confirm:$false     } -ArgumentList ($Server, $metrics, $counter)
}

 

Regards

Genoan


Viewing all articles
Browse latest Browse all 195209

Trending Articles



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