我正在尝试从vcenter为主机收集性能数据。我已经创建了这个powershell脚本。它有效,但需要很长时间才能完成。是否有更好的方法从vcenters中检索数据。
Connect-VIServer "vcenter.server.com" -User user123 -Password testpassword
$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(-1) -Finish (Get-Date)-MaxSamples 10 -stat cpu.usage.average
$statmem = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 10 -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
答案 0 :(得分:1)
请考虑以下代码,该代码将处理移至PowerShell后台作业:
Connect-VIServer "vcenter.server.com" -User user123 -Password testpassword
$allvms = @()
$allhosts = @()
$hosts = Get-VMHost;
$vms = Get-Vm;
# Define the code that will be executed as part of the background job
$Job = {
$hoststat = @{};
$statcpu = Get-Stat -Entity $args[0] -start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 10 -stat cpu.usage.average;
$statmem = Get-Stat -Entity $args[0] -start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 10 -stat mem.usage.average;
$cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum;
$mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum;
$hoststat.HostName = $args[0];
$hoststat.CPUMax = $cpu.Maximum;
$hoststat.CPUAvg = $cpu.Average;
$hoststat.CPUMin = $cpu.Minimum;
$hoststat.MemMax = $mem.Maximum;
$hoststat.MemAvg = $mem.Average;
$hoststat.MemMin = $mem.Minimum;
return $hoststat;
}
$JobList = @();
foreach($vmHost in $hosts){
$JobList += Start-Job -ScriptBlock $Job -Name $vmHost.Name -ArgumentList $vmHost.Name;
}
Wait-Job -Job $JobList;
$JobList | % { $allhosts += Receive-Job -Job $PSItem; };
$allhosts | Select HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | Export-Csv "c:\output\Hosts.csv" -noTypeInformation
答案 1 :(得分:1)
您可以通过一次get-stat调用从多个主机实体收集多个指标,如
get-stat -Entity $hosts -MaxSamples 10 -Stat "cpu.usage.average","mem.usage.average"
然后使用Group-Object
来解析结果。 LucD的帖子here可能会有所帮助。
我想知道您是否需要$vms = Get-Vm
,因为您发布的代码并未提及此内容。
您的PS会话应与vcenter db位于同一LAN上。另请参阅kb.vmware.com/kb/2007388。