2015-05-15 149 views
0

我想通过执行PowerShell脚本通过nagios和nsclient ++获取接口性能指标。下面是PowerShell脚本的主要部分。Windows网络接口监控

Function netstat { 
    '{0:0}' -f (Get-WmiObject Win32_PerfRawData_Tcpip_NetworkInterface | 
    where{$_.Name -eq "vmxnet3 Ethernet Adapter"} | 
    select BytesReceivedPersec,BytesSentPersec)/1KB) 
} 

我想要的输出是在MB/s和改变头Tx MB/sRx MB/s。现在我得到以下输出。

PS C:\Users\Administrator> Get-WmiObject Win32_PerfRawData_Tcpip_NetworkInterface | where{$_.Name -eq "vmxnet3 Ethernet Adapter"} | select BytesReceivedPersec, BytesSentPersec 

       BytesReceivedPersec     BytesSentPersec 
       -------------------     --------------- 
         12720975895       438054511 

我也使用check_wmi模块nsclient ++这样的尝试。

check_nrpe -H 10.35.136.221 -c check_wmi -a 'query=select BytesReceivedPersec, BytesSentPersec from Win32_PerfRawData_Tcpip_NetworkInterface where name = "vmxnet3 Ethernet Adapter"' 

我得到以下输出。

12719626616, 437766199 

如何使输出的上述检查结果如下所示?

Rx MB/s : 12719626616 Tx MB/s : 437766199 

回答

1

使用calculated properties更改属性名称和/或值。如果你想要的是你不需要与计算性能打扰格式化输出字符串:更改此:

... | select BytesReceivedPersec, BytesSentPersec 

到这一点:

... | select @{n='Rx MB/s';e={$_.BytesReceivedPersec/1MB}}, 
      @{n='Tx MB/s';e={$_.BytesSentPersec/1MB}} 

编辑。只是建立格式化的字符串:

... | % { 
    'OK | Rx MB/s={0:0}; Tx MB/s={1:0}' -f ($_.BytesReceivedPersec/1MB), 
    ($_.BytesSentPersec/1MB) 
} 
+0

这是行之有效的,谢谢你,Ansgar。但是,我担心这是否是获取接口的网络统计信息的正确方法。我试图通过有问题的机器复制文件,并监视任务管理器和此命令的输出。他们似乎没有反映相同的事情,或者我错了吗? –

+0

@RajS您可能需要查询['Win32_PerfFormattedData_Tcpip_NetworkInterface'](https://msdn.microsoft.com/en-us/library/aa394293%28v=vs.85%29.aspx)class。 –

+0

函数netstat { $ nstat = Get-WmiObject Win32_PerfFormattedData_Tcpip_NetworkInterface |其中{$ _。名称-eq“vmxnet3以太网适配器”}}选择@ {n =“Rx MB/s”; e = {$ _。BytesReceivedPersec /1MB}},@ {n =“Tx MB/s”; e = {$ _。BytesSentPersec/1MB}} '好的| {0:0}'-f($ nstat) }。我试图将该函数的输出作为“OK |” Tx MB/s = ...,Rx MB/s = ...,但是我得到以下输出。 OK |'@ {Rx'= 0'MB/s'= 0'Tx'= 0'MB/s'= 0} –