2015-05-15 222 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} –