欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-02 08:57:47
核心代码 '程序名称:      btlwchk_diskio.vbs '版权信息:   &...

核心代码

'程序名称:      btlwchk_diskio.vbs
'版权信息:      copyright (c)  2011 guangzhou bluetech technology incorporated.all rights reserved
'程序用途:      监测windows主机io利用率
'创建日期:      2011-08-10
'作者信息:      zhangkai
'运行环境:      vbs
'处理参数
monsubject="diskio"
set args=wscript.arguments
if (args.count<1) then
 wscript.echo monsubject & " -1:command line error"
 wscript.quit(3)
end if
strcid=args(0)
'获取工作目录
tmparr=split(wscript.scriptfullname,"\",-1)
g_strworkdir=tmparr(0)
narr=ubound(tmparr,1)
for i=1 to narr-3
    g_strworkdir=g_strworkdir & "\" & tmparr(i)
next
'装载公共库
set g_filesys = createobject ("scripting.filesystemobject")
sub includefile (fspec)
    dim file, filedata
    set file = g_filesys.opentextfile (fspec)
    filedata = file.readall ()
    file.close
    executeglobal filedata
    set file = nothing 
end sub
includefile g_strworkdir & "\nagios\libexec\libcomm.vbs"

'输出版本和帮助信息
getverhelp strcid,"v1.00"," <cid>"

strcomputer = "."  
set objwmiservice = getobject("winmgmts:" & "{impersonationlevel=impersonate}!\\" & strcomputer & "\root\cimv2") 

'第1次采样io
set colitems = objwmiservice.execquery("select * from win32_perfrawdata_perfdisk_logicaldisk where name='_total' ",,48)
for each objitem in colitems
     disktime1 = objitem.percentdisktime
     basetime1 = objitem.percentdisktime_base
  diskbytes1 = objitem.diskbytespersec
  diskreadbytes1 = objitem.diskreadbytespersec
  diskwritebytes1 = objitem.diskwritebytespersec
  avgdisktransfer1 = objitem.avgdisksecpertransfer
  avgdisktransferbase1 = objitem.avgdisksecpertransfer_base
  avgdiskread1 = objitem.avgdisksecperread
  avgdiskreadbase1 = objitem.avgdisksecperread_base
  avgdiskwrite1 = objitem.avgdisksecperwrite
  avgdiskwritebase1 = objitem.avgdisksecperwrite_base
  timevalue1 = objitem.timestamp_perftime
  timebase = objitem.frequency_perftime

next

'第2次采样io
wscript.sleep 1000
set colitems = objwmiservice.execquery("select * from win32_perfrawdata_perfdisk_logicaldisk where name='_total' ",,48)
for each objitem in colitems
     disktime2 = objitem.percentdisktime
  basetime2 = objitem.percentdisktime_base
  diskbytes2 = objitem.diskbytespersec
  diskreadbytes2 = objitem.diskreadbytespersec
  diskwritebytes2 = objitem.diskwritebytespersec
  avgdisktransfer2 = objitem.avgdisksecpertransfer
  avgdisktransferbase2 = objitem.avgdisksecpertransfer_base
  avgdiskread2 = objitem.avgdisksecperread
  avgdiskreadbase2 = objitem.avgdisksecperread_base
  avgdiskwrite2 = objitem.avgdisksecperwrite
  avgdiskwritebase2 = objitem.avgdisksecperwrite_base
  timevalue2 = objitem.timestamp_perftime
        currentdiskqueuelength = objitem.currentdiskqueuelength
next

'获取io利用率
if basetime2 - basetime1 = 0 then
 strio ="name=_toltal"  & ",percentdisktime=0%"
else
 percentdisktime =(disktime2 - disktime1)/(basetime2 - basetime1)*100 
 strio = "name=_total"  & ",percentdisktime="  & percentdisktime
end if

'获取disk bytes/sec
if timevalue2 - timevalue1 = 0 then
    strdiskbyte = ",diskbytespersec=0"
else
    diskbytespersec = (diskbytes2 - diskbytes1) / ( (timevalue2 - timevalue1) / timebase)
    strdiskbyte = ",diskbytespersec="  & diskbytespersec
end if

'获取disk read bytes/sec
if timevalue2 - timevalue1 = 0 then
    strdiskreadbyte = ",diskreadbytespersec=0"
else
    diskreadbytespersec = (diskreadbytes2 - diskreadbytes1) / ( (timevalue2 - timevalue1) / timebase)
    strdiskreadbyte = ",diskreadbytespersec="  & diskreadbytespersec
end if

'获取disk write bytes/sec
if timevalue2 - timevalue1 = 0 then
    strdiskwritebyte = ",diskwritebytespersec=0"
else
    diskwritebytespersec = (diskwritebytes2 - diskwritebytes1) / ( (timevalue2 - timevalue1) / timebase)
    strdiskwritebyte = ",diskwritebytespersec="  & diskwritebytespersec
end if

'获取avg. disk sec/transfer
if avgdisktransferbase2 - avgdisktransferbase1 = 0 then
    stravgdisktran = ",avgdisksecpertransfer=0"
else
    avgdisksecpertransfer = ((avgdisktransfer2 - avgdisktransfer1) / timebase) / (avgdisktransferbase2 - avgdisktransferbase1)
    stravgdisktran = ",avgdisksecpertransfer="  & avgdisksecpertransfer
end if

'获取avg. disk sec/read
if avgdiskreadbase2 - avgdiskreadbase1 = 0 then
    stravgdiskread = ",avgdisksecperread=0"
else
    avgdisksecperread = ((avgdiskread2 - avgdiskread1) / timebase) / (avgdiskreadbase2 - avgdiskreadbase1)
    stravgdiskread = ",avgdisksecperread="  & avgdisksecperread
end if

'获取avg. disk sec/write
if avgdiskwritebase2 - avgdiskwritebase1 = 0 then
    stravgdiskwrite = ",avgdisksecperwrite=0"
else
    avgdisksecperwrite = ((avgdiskwrite2 - avgdiskwrite1) / timebase) / (avgdiskwritebase2 - avgdiskwritebase1)
    stravgdiskwrite = ",avgdisksecperwrite="  & avgdisksecperwrite
end if

'获取current disk queue length
strdisklength = ",currentdiskqueuelength="  & currentdiskqueuelength

wscript.echo monsubject & " 0:ok|" & strio & strdiskbyte & strdiskreadbyte & strdiskwritebyte & stravgdisktran & stravgdiskread & stravgdiskwrite & strdisklength

用到了wmi,需要的朋友可以参考一下

相关标签: VBS磁盘 利用率