• 售前

  • 售后

热门帖子
入门百科

磁盘IO利用率监控VBS脚本(windows)

[复制链接]
芊芊551 显示全部楼层 发表于 2021-8-19 18:54:05 |阅读模式 打印 上一主题 下一主题
核心代码
  1. '程序名称:      btlwchk_DiskIO.vbs
  2. '版权信息:      Copyright (c)  2011 guangzhou bluetech technology Incorporated.All rights reserved
  3. '程序用途:      监测windows主机IO利用率
  4. '创建日期:      2011-08-10
  5. '作者信息:      zhangkai
  6. '运行环境:      vbs
  7. '处理参数
  8. MonSubject="DiskIO"
  9. Set Args=WScript.Arguments
  10. If (Args.Count<1) Then
  11.  Wscript.Echo MonSubject & " -1:command line error"
  12.  WScript.Quit(3)
  13. End If
  14. strcid=Args(0)
  15. '获取工作目录
  16. tmparr=Split(Wscript.ScriptFullName,"",-1)
  17. g_strworkdir=tmparr(0)
  18. narr=UBound(tmparr,1)
  19. For i=1 to narr-3
  20.     g_strworkdir=g_strworkdir & "" & tmparr(i)
  21. Next
  22. '装载公共库
  23. set g_fileSys = createObject ("Scripting.FileSystemObject")
  24. Sub includeFile (fSpec)
  25.     dim file, fileData
  26.     set file = g_fileSys.openTextFile (fSpec)
  27.     fileData = file.readAll ()
  28.     file.close
  29.     executeGlobal fileData
  30.     set file = nothing 
  31. end sub
  32. includeFile g_strworkdir & "\nagios\libexec\libcomm.vbs"
  33. '输出版本和帮助信息
  34. getverhelp strcid,"v1.00"," <cid>"
  35. strComputer = "."  
  36. Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2") 
  37. '第1次采样IO
  38. Set colItems = objWMIService.ExecQuery("Select * from Win32_PerfRawData_PerfDisk_LogicalDisk where name='_Total' ",,48)
  39. For Each objItem in colItems
  40.      DiskTime1 = objItem.PercentDiskTime
  41.      BaseTime1 = objItem.PercentDiskTime_Base
  42.   DiskBytes1 = objItem.DiskBytesPerSec
  43.   DiskReadBytes1 = objItem.DiskReadBytesPerSec
  44.   DiskWriteBytes1 = objItem.DiskWriteBytesPerSec
  45.   AvgDiskTransfer1 = objItem.AvgDiskSecPerTransfer
  46.   AvgDiskTransferBase1 = objItem.AvgDiskSecPerTransfer_Base
  47.   AvgDiskRead1 = objItem.AvgDiskSecPerRead
  48.   AvgDiskReadBase1 = objItem.AvgDiskSecPerRead_Base
  49.   AvgDiskWrite1 = objItem.AvgDiskSecPerWrite
  50.   AvgDiskWriteBase1 = objItem.AvgDiskSecPerWrite_Base
  51.   TimeValue1 = objItem.Timestamp_PerfTime
  52.   TimeBase = objItem.Frequency_PerfTime
  53. Next
  54. '第2次采样IO
  55. WScript.Sleep 1000
  56. Set colItems = objWMIService.ExecQuery("Select * from Win32_PerfRawData_PerfDisk_LogicalDisk where name='_Total' ",,48)
  57. For Each objItem in colItems
  58.      DiskTime2 = objItem.PercentDiskTime
  59.   BaseTime2 = objItem.PercentDiskTime_Base
  60.   DiskBytes2 = objItem.DiskBytesPerSec
  61.   DiskReadBytes2 = objItem.DiskReadBytesPerSec
  62.   DiskWriteBytes2 = objItem.DiskWriteBytesPerSec
  63.   AvgDiskTransfer2 = objItem.AvgDiskSecPerTransfer
  64.   AvgDiskTransferBase2 = objItem.AvgDiskSecPerTransfer_Base
  65.   AvgDiskRead2 = objItem.AvgDiskSecPerRead
  66.   AvgDiskReadBase2 = objItem.AvgDiskSecPerRead_Base
  67.   AvgDiskWrite2 = objItem.AvgDiskSecPerWrite
  68.   AvgDiskWriteBase2 = objItem.AvgDiskSecPerWrite_Base
  69.   TimeValue2 = objItem.Timestamp_PerfTime
  70.         CurrentDiskQueueLength = objItem.CurrentDiskQueueLength
  71. Next
  72. '获取IO利用率
  73. If BaseTime2 - BaseTime1 = 0 Then
  74.  strio ="Name=_Toltal"  & ",PercentDiskTime=0%"
  75. Else
  76.  PercentDiskTime =(DiskTime2 - DiskTime1)/(BaseTime2 - BaseTime1)*100 
  77.  strio = "Name=_Total"  & ",PercentDiskTime="  & PercentDiskTime
  78. End If
  79. '获取Disk Bytes/sec
  80. If TimeValue2 - TimeValue1 = 0 Then
  81.     strdiskbyte = ",DiskBytesPerSec=0"
  82. Else
  83.     DiskBytesPerSec = (DiskBytes2 - DiskBytes1) / ( (TimeValue2 - TimeValue1) / TimeBase)
  84.     strdiskbyte = ",DiskBytesPerSec="  & DiskBytesPerSec
  85. End If
  86. '获取Disk Read Bytes/sec
  87. If TimeValue2 - TimeValue1 = 0 Then
  88.     strdiskreadbyte = ",DiskReadBytesPerSec=0"
  89. Else
  90.     DiskReadBytesPerSec = (DiskReadBytes2 - DiskReadBytes1) / ( (TimeValue2 - TimeValue1) / TimeBase)
  91.     strdiskreadbyte = ",DiskReadBytesPerSec="  & DiskReadBytesPerSec
  92. End If
  93. '获取Disk Write Bytes/sec
  94. If TimeValue2 - TimeValue1 = 0 Then
  95.     strdiskwritebyte = ",DiskWriteBytesPerSec=0"
  96. Else
  97.     DiskWriteBytesPerSec = (DiskWriteBytes2 - DiskWriteBytes1) / ( (TimeValue2 - TimeValue1) / TimeBase)
  98.     strdiskwritebyte = ",DiskWriteBytesPerSec="  & DiskWriteBytesPerSec
  99. End If
  100. '获取Avg. Disk sec/Transfer
  101. If AvgDiskTransferBase2 - AvgDiskTransferBase1 = 0 Then
  102.     stravgdisktran = ",AvgDiskSecPerTransfer=0"
  103. Else
  104.     AvgDiskSecPerTransfer = ((AvgDiskTransfer2 - AvgDiskTransfer1) / TimeBase) / (AvgDiskTransferBase2 - AvgDiskTransferBase1)
  105.     stravgdisktran = ",AvgDiskSecPerTransfer="  & AvgDiskSecPerTransfer
  106. End If
  107. '获取Avg. Disk sec/Read
  108. If AvgDiskReadBase2 - AvgDiskReadBase1 = 0 Then
  109.     stravgdiskread = ",AvgDiskSecPerRead=0"
  110. Else
  111.     AvgDiskSecPerRead = ((AvgDiskRead2 - AvgDiskRead1) / TimeBase) / (AvgDiskReadBase2 - AvgDiskReadBase1)
  112.     stravgdiskread = ",AvgDiskSecPerRead="  & AvgDiskSecPerRead
  113. End If
  114. '获取Avg. Disk sec/Write
  115. If AvgDiskWriteBase2 - AvgDiskWriteBase1 = 0 Then
  116.     stravgdiskwrite = ",AvgDiskSecPerWrite=0"
  117. Else
  118.     AvgDiskSecPerWrite = ((AvgDiskWrite2 - AvgDiskWrite1) / TimeBase) / (AvgDiskWriteBase2 - AvgDiskWriteBase1)
  119.     stravgdiskwrite = ",AvgDiskSecPerWrite="  & AvgDiskSecPerWrite
  120. End If
  121. '获取Current Disk Queue Length
  122. strdisklength = ",CurrentDiskQueueLength="  & CurrentDiskQueueLength
  123. Wscript.Echo MonSubject & " 0:OK|" & strio & strdiskbyte & strdiskreadbyte & strdiskwritebyte & stravgdisktran & stravgdiskread & stravgdiskwrite & strdisklength
复制代码
用到了WMI,必要的朋侪可以参考一下

帖子地址: 

回复

使用道具 举报

分享
推广
火星云矿 | 预约S19Pro,享500抵1000!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

草根技术分享(草根吧)是全球知名中文IT技术交流平台,创建于2021年,包含原创博客、精品问答、职业培训、技术社区、资源下载等产品服务,提供原创、优质、完整内容的专业IT技术开发社区。
  • 官方手机版

  • 微信公众号

  • 商务合作