【问题标题】:PowerShell: calculate delta between successive values of the rawvalue property for each counterPowerShell:计算每个计数器的 rawvalue 属性的连续值之间的增量
【发布时间】:2015-04-18 01:09:43
【问题描述】:

'晚上,

我是 PowerShell 脚本(以及本网站)的新手,需要编写一个脚本:

  • 多次收集性能计数器(在本例中为 TCP)
  • 计算每个计数器的 rawvalue 属性的连续值之间的差异

规定脚本每间隔T迭代性能计数器N次,表示性能计数器为“A”和“B”,我们从0开始计数,需要进行如下计算:

A[1st] - A[0th], 
A[2nd] - A[1st], 
A[3rd] - A[2nd]
...  

目前,脚本只遍历计数器两次(即本例中的 N = 2)。目标是能够“多次”(例如几百次)遍历这些计数器。

当前脚本将每个计数器的原始值读入单个数组。这里是:

$cntr = (get-counter -listset tcpv4).paths
$arry = @()

for ($i=0; $i -lt 2; $i++) {
    write-host "`nThis is iteration $i`n"
    foreach ($elmt in $cntr) {
        $z = (get-counter -counter $elmt).countersamples[0].rawvalue
        $arry = $arry + $z
        write-host "$elmt is: $z`n"
    }
}

当我运行这个脚本时,我得到如下输出:

This is iteration 0

\TCPv4\Segments/sec is: 24723

\TCPv4\Connections Established is: 27

\TCPv4\Connections Active is: 796

\TCPv4\Connections Passive is: 47

\TCPv4\Connection Failures is: 158

\TCPv4\Connections Reset is: 412

\TCPv4\Segments Received/sec is: 14902

\TCPv4\Segments Sent/sec is: 9822

\TCPv4\Segments Retransmitted/sec is: 199


This is iteration 1

\TCPv4\Segments/sec is: 24727

\TCPv4\Connections Established is: 27

\TCPv4\Connections Active is: 798

\TCPv4\Connections Passive is: 47

\TCPv4\Connection Failures is: 159

\TCPv4\Connections Reset is: 412

\TCPv4\Segments Received/sec is: 14903

\TCPv4\Segments Sent/sec is: 9824

\TCPv4\Segments Retransmitted/sec is: 200

例如“\TCPv4\Segments Retransmitted/sec”计数器的 rawvalue 属性的两个值分别是 $arry[8] 和 $arry[17]。为了得出我正在使用的两者之间的区别:

write-host "The difference between the successive counters for $($cntr[-1]) is $($($arry[17]) - $($arry[8]))."

任何帮助将不胜感激。

【问题讨论】:

  • 您希望这些数据如何显示?那么对于每个值,您想知道前一个的区别吗?基本上使你的输出翻倍/
  • @Matt,感谢您的回复。如何显示数据待定。是的,目前我只想知道每个计数器的当前值和前一个值之间的差异。
  • 希望我超出了您的预期。

标签: powershell powershell-2.0


【解决方案1】:

我戳了几下,结果出来了:

$cntr = (get-counter -listset tcpv4).paths
$LastValue = @{}

Get-Counter $cntr -SampleInterval 2 -MaxSamples 5 |
foreach {
  foreach ($Sample in $_.CounterSamples)
   {
     $ht = [Ordered]@{
        Counter   = $Sample.path.split('\')[-1]
        TimeStamp = $_.TimeStamp
        RawValue  = $Sample.RawValue
        LastValue = $LastValue[$Sample.Path]
        Change    = $Sample.RawValue - $LastValue[$Sample.Path]
        }
     if ($LastValue.ContainsKey($Sample.path))
       { [PSCustomObject]$ht }
     $LastValue[$Sample.Path] = $Sample.RawValue
   }
 } 

编辑:

这应该适用于 V2:

$cntr = (get-counter -listset tcpv4).paths
$LastValue = @{}

Get-Counter $cntr -SampleInterval 10 -MaxSamples 3 |
foreach {
  foreach ($Sample in $_.CounterSamples)
   {
     $Object = '' | 
      Select-Object Counter,TimeStamp,RawValue,LastValue,Change
      $Object.Counter   = $Sample.path.split('\')[-1]
      $Object.TimeStamp = $_.TimeStamp
      $Object.RawValue  = $Sample.RawValue
      $Object.LastValue = $LastValue[$Sample.Path]
      $Object.Change    = $Sample.RawValue - $LastValue[$Sample.Path]

     if ($LastValue.ContainsKey($Sample.path))
       { $Object }

     $LastValue[$Sample.Path] = $Sample.RawValue
   }
 } 

【讨论】:

  • 操作标记为 2.0,所以我认为 [Ordered] 不会起作用。
【解决方案2】:

好的。那么让我们来处理这个吧

$cntr = (get-counter -listset tcpv4).paths
$arry = @()
$maximumIterations = 2  # Variable based since you intended to change this. 

# Cycle the counters while recording the values. Once completed we will calculate change.
for ($i=1; $i -le $maximumIterations; $i++) {
    foreach ($elmt in $cntr) {
        $arry += New-Object -TypeName PsCustomObject -Property @{
            Iteration = $i
            Counter = $elmt
            RawValue = (get-counter -counter $elmt).countersamples[0].rawvalue
            Change = $null
        }
    }
}

# Now that we have all the values lets calculate the rate of change over each iteration.
$arry | Where-Object{$_.Iteration -gt 1} | ForEach-Object{
    $previousIteration = $_.Iteration - 1 
    $thisCounter = $_.Counter
    $thisValue = $_.RawValue
    $previousValue = ($arry | Where-Object{$_.Counter -eq $thisCounter -and $_.Iteration -eq $previousIteration}).RawValue
    $_.Change = $thisValue - $previousValue
}

$arry | Select Iteration,Counter,RawValue,Change

不是我们必须,但我收集了所有计数器数据以及它们的迭代值,就像你对Write-Host 提出的那样。您会注意到我为Change 创建了一个占位符,但没有填充它。在计算之前$arry 会有如下数据。注意:输出被截断

Iteration Change Counter                           RawValue
--------- ------ -------                           --------
        1        \TCPv4\Segments/sec               28324837
        1        \TCPv4\Connections Established         120
        .        ..............................       .....
        2        \TCPv4\Segments/sec               28325441
        2        \TCPv4\Connections Established         125

一旦将所有数据收集到$arry,我们就会得到所有不是第一次的迭代,并单独处理每个项目。使用管道中当前项目的数据,我们将其与之前的迭代值进行匹配。使用与上述相同的值,我们会得到您希望监控的更改

Iteration Counter                           RawValue Change
--------- -------                           -------- ------
        1 \TCPv4\Segments/sec               28324837       
        1 \TCPv4\Connections Established         120 
        . ..............................       .....         
        2 \TCPv4\Segments/sec               28325441 604   
        2 \TCPv4\Connections Established         125 5     

【讨论】:

  • mjolinor,马特:感谢你们每一个人的回答。他们都符合要求。对我的回复延迟表示歉意。
  • @tre2015 我现在可以睡觉了。希望我们能够提供帮助!
  • 哈哈。是的,先生,你们俩都很有帮助。离开网站几个月了,所以首要任务是感谢你们。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2021-04-07
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 2017-02-14
  • 2021-11-06
  • 1970-01-01
  • 2023-02-19
相关资源
最近更新 更多