磁碟IO效能監控(Linux 和 Windows)

來源:互聯網
上載者:User

磁碟的IO效能是衡量電腦總體效能的一個重要指標。Linux提供了iostat命令來獲卻磁碟輸入/輸出(即IO)統計資訊,Windows則提供了WMI介面,可以通過編寫一個簡單的指令碼來擷取與iostat相當的功能。

1、Linux下的iostat命令

iostat -d -k -t 2

每隔2秒統計一次磁碟IO資訊,直到按Ctrl+C終止程式,-d 選項表示統計磁碟資訊, -k 表示以每秒KB的形式顯示,-t 要求列印出時間資訊,2 表示每隔 2 秒輸出一次。第一次輸出的磁碟IO負載狀況提供了關於自從系統啟動以來的統計資訊。隨後的每一次輸出則是每個間隔之間的平均IO負載狀況。

運行該命令後,輸出:

Linux 2.6.9-67.0.7.ELsmp (localhost.localdomain)        11/19/2008

Time: 03:15:25 PM
Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               3.53        26.66        54.76   30122033   61864280
sda1              0.51         1.07         1.73    1207649    1949740
sda2              0.00         0.00         0.00        538        256
sda3             13.84        25.59        53.03   28913291   59914092

Time: 03:15:27 PM
Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda             275.38         0.00      1738.69          0       3460
sda1             14.57         0.00        58.29          0        116
sda2              0.00         0.00         0.00          0          0
sda3            419.60         0.00      1678.39          0       3340

...

每次輸出都會列印時間資訊, 接下來顯示磁碟IO情況列表。

Device: 顯示磁碟名稱
tps: 表示每秒鐘輸出到物理磁碟的傳輸次數。一次傳輸就是一個對物理磁碟的 I/O 請求。多個邏輯請求可被並為對磁碟的一個單一 I/O 請求。傳輸具有中等的大小。
kB_read/s: 每秒從磁碟讀取的資料量,單位為KB。
kB_wrtn/s: 每秒從寫入磁碟的資料量,單位為KB。
Kb_read: 讀取的 KB 總數。
Kb_wrtn: 寫入的 KB 總數。

2、WMI中的 Win32_PerfFormattedData_PerfDisk_LogicalDisk 對象

Win32_PerfFormattedData_PerfDisk_LogicalDisk 代表邏輯磁碟效能資料對象,利用該對象可以獲得磁碟的心能資訊。Win32_PerfFormattedData_PerfDisk_LogicalDisk對象有以下一些主要的屬性:

Name: 磁碟名稱
DiskTransfersPerSec:每秒磁碟傳輸次數。
DiskReadBytesPerSec:每秒從磁碟讀取得資料量,單位為Byte。
DiskWriteBytesPerSec:每秒從磁碟讀取得資料量,單位為Byte。
PercentFreeSpace:可用的磁碟百分比。

3、使用 Win32_PerfFormattedData_PerfDisk_LogicalDisk 的注意事項

在使用 Win32_PerfFormattedData_PerfDisk_LogicalDisk 時,需要注意:

(1)不能使用 objWMIService.ExecQuery 執行 Select 語句來擷取磁碟效能資料
(2)必須使用 WbemScripting.SWbemRefresher 將 Win32_PerfFormattedData_PerfDisk_LogicalDisk 加入,然後不斷調用 Refresh 方法重新整理資料來擷取效能資訊
(3)第一次重新整理的時候,並不能擷取有用的資料,從第二次開始,才能擷取到磁碟效能資料
(4)以上問題與 WMI 中效能監控使用計數器的機制有關

4、使用舉例

為了對監控磁碟效能提供一個良好的使用者介面,可以利用VBScript編寫指令碼來擷取磁碟效能資料。指令碼的代碼如下:

'Script File Name: DiskMonitor.vbs

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
Set colDisks = objRefresher.AddEnum(objWMIService, "Win32_PerfFormattedData_PerfDisk_LogicalDisk").objectSet

If Wscript.Arguments.Count = 0 Then
objRefresher.Refresh
For Each objDisk in colDisks
   Wscript.Echo objDisk.Name & " " & objDisk.DiskReadBytesPerSec & " " & objDisk.DiskWriteBytesPerSec
Next
End If

If Wscript.Arguments.Count = 1 Then
Interval = CInt(Wscript.Arguments(0)) * 1000
Do While True
   objRefresher.Refresh

   Wscript.Echo
   Wscript.Echo "Time: " & " " & Time()
   Wscript.Echo FormatStr("Device:", 15, 0) & FormatStr("tps", 7, 1) & FormatStr("    kB_read/s", 13, 1) & FormatStr("kB_wrtn/s", 13, 1) & FormatStr("Free Space", 13, 1)

   For Each objDisk in colDisks
    Wscript.Echo FormatStr(objDisk.Name, 15, 0) & FormatStr(objDisk.DiskTransfersPerSec, 7, 1) & FormatStr(objDisk.DiskReadBytesPerSec, 13, 1) & FormatStr(objDisk.DiskWriteBytesPerSec, 13, 1) & FormatStr(objDisk.PercentFreeSpace & "%", 13, 1)
   Next
   Wscript.Sleep Interval
Loop
End If

If Wscript.Arguments.Count = 2 Then
i = 0
Interval = CInt(Wscript.Arguments(0)) * 1000
Count = CInt(Wscript.Arguments(1))
Do While i < Count
   objRefresher.Refresh

   Wscript.Echo
   Wscript.Echo "Time: " & " " & Time()
   Wscript.Echo FormatStr("Device:", 15, 0) & FormatStr("tps", 7, 1) & FormatStr("    kB_read/s", 13, 1) & FormatStr("kB_wrtn/s", 13, 1) & FormatStr("Free Space", 13, 1)

   For Each objDisk in colDisks
    Wscript.Echo FormatStr(objDisk.Name, 15, 0) & FormatStr(objDisk.DiskTransfersPerSec, 7, 1) & FormatStr(objDisk.DiskReadBytesPerSec, 13, 1) & FormatStr(objDisk.DiskWriteBytesPerSec, 13, 1) & FormatStr(objDisk.PercentFreeSpace & "%", 13, 1)
   Next
   Wscript.Sleep Interval
   i = i + 1
Loop
End If

Function FormatStr(str, tLen, direction)
sLen = Len(str)
fStr = ""
num = tLen - sLen

j = 0
Do While j < num
   fStr = fStr & " "
   j = j + 1
Loop

If direction = 1 Then
   fStr = fStr & str
Else
   fStr = str & fStr
End If
FormatStr = fStr
End Function

使用舉例:

(1)CSCript DiskMonitor.vbs
止重新整理一次 Win32_PerfFormattedData_PerfDisk_LogicalDisk 對象,不會擷取到有用的資料。

(2)CSCript DiskMonitor.vbs 2
每隔 2 秒擷取一次磁碟效能資料並輸出,直到按 Ctrl+C 終止程式。

(3)CSCript DiskMonitor.vbs 2 100
每隔 2 秒擷取一次磁碟效能資料並輸出,總共擷取 100 次,然後退出。

該指令碼輸出的資訊包括 DiskTransfersPerSec、DiskReadBytesPerSec、DiskWriteBytesPerSec 和 PercentFreeSpace。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.