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

监测windows主机网络接口利用率的vbs代码

程序员文章站 2022-03-02 08:57:41
btlwchk_netinterface.vbs '程序名称:      btlwchk_netinterface.vbs...

btlwchk_netinterface.vbs

'程序名称:      btlwchk_netinterface.vbs

'版权信息:      copyright (c)  2011 guangzhou bluetech technology incorporated.all rights reserved
'程序用途:      监测windows主机网络接口利用率
'创建日期:      2011-09-1
'作者信息:      zhangkai
'运行环境:      vbs
'处理参数

monsubject="netinterface"
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次采样
set colitems = objwmiservice.execquery("select * from win32_perfrawdata_tcpip_networkinterface",,48)
dim bytestotalpersec1(20),timevalue1(20),name1(20),currentbandwidth(20),bytesreceivedpersec1(20),bytessentpersec1(20),packetsreceivedpersec1(20),packetssentpersec1(20),_
bytestotalpersec(20),percentnetwork(20),bytesreceivedpersec(20),bytessentpersec(20),packetsreceivedpersec(20),packetssentpersec(20)
n=0
for each objitem in colitems
  name1(n) = objitem.name
  bytestotalpersec1(n) = objitem.bytestotalpersec
  bytesreceivedpersec1(n) = objitem.bytesreceivedpersec
  bytessentpersec1(n) = objitem.bytessentpersec
  packetsreceivedpersec1(n) = objitem.packetsreceivedpersec
  packetssentpersec1(n) = objitem.packetssentpersec
  timevalue1(n) = objitem.timestamp_perftime
  currentbandwidth(n) = objitem.currentbandwidth
  timebase = objitem.frequency_perftime
  n=n+1
next

'第2次采样
wscript.sleep 1000
set colitems = objwmiservice.execquery("select * from win32_perfrawdata_tcpip_networkinterface",,48)
dim bytestotalpersec2(20),timevalue2(20),name2(20),bytesreceivedpersec2(20),bytessentpersec2(20),packetsreceivedpersec2(20),packetssentpersec2(20)
k=0
for each objitem in colitems
  name2(k) = objitem.name
     bytestotalpersec2(k) = objitem.bytestotalpersec
  bytesreceivedpersec2(k) = objitem.bytesreceivedpersec
  bytessentpersec2(k) = objitem.bytessentpersec
  packetsreceivedpersec2(k) = objitem.packetsreceivedpersec
  packetssentpersec2(k) = objitem.packetssentpersec
  timevalue2(k) = objitem.timestamp_perftime
  k=k+1
next

j=0
for i=0 to n-1
 if timevalue2(j) - timevalue1(j) = 0 then
  strnetwork = "bytestotalpersec=0"
 else
  '带宽利用率= (bytesreceivedpersec + bytessentpersec)*8*100/ currentbandwidth
  '计算利用率
  bytestotalpersec(j) = (bytestotalpersec2(j) - bytestotalpersec1(j)) / ((timevalue2(j) - timevalue1(j)) / timebase)
  percentnetwork(j) = bytestotalpersec(j)*8*100 / currentbandwidth(j)
  percentnetwork(j) = round(percentnetwork(j),2)
  
  '计算bytesreceivedpersec
  bytesreceivedpersec(j) = (bytesreceivedpersec2(j) - bytesreceivedpersec1(j)) / ((timevalue2(j) - timevalue1(j)) / timebase)/1024
  bytesreceivedpersec(j) = round(bytesreceivedpersec(j),2)
  
  '计算bytessentpersec
  bytessentpersec(j) = (bytessentpersec2(j) - bytessentpersec1(j)) / ((timevalue2(j) - timevalue1(j)) / timebase)/1024
  bytessentpersec(j) = round(bytessentpersec(j),2)
  
  '计算packetsreceivedpersec
  packetsreceivedpersec(j) = (packetsreceivedpersec2(j) - packetsreceivedpersec1(j)) / ((timevalue2(j) - timevalue1(j)) / timebase)
  packetsreceivedpersec(j) = round(packetsreceivedpersec(j),2)
  
  '计算packetssentpersec
  packetssentpersec(j) = (packetssentpersec2(j) - packetssentpersec1(j)) / ((timevalue2(j) - timevalue1(j)) / timebase)
  packetssentpersec(j) = round(packetssentpersec(j),2)
  
  '计算currentbandwidth
  currentbandwidth(j) = cint(currentbandwidth(j)/1000/1000)
 end if
 if j = 0 then
  wscript.echo monsubject & " 0:ok|name=" & name1(j) & ",percentnetwork=" & percentnetwork(j) & ",bytesreceivedpersec=" & bytesreceivedpersec(j) & ",bytessentpersec=" & bytessentpersec(j) _
  & ",packetsreceivedpersec=" & packetsreceivedpersec(j) & ",packetssentpersec=" & packetssentpersec(j) & ",currentbandwidth=" & currentbandwidth(j)
 end if
 'wscript.echo monsubject & " 0:ok|" & name1(j) & ",percentnetwork=" & percentnetwork
 j=j+1
next

这篇文章就介绍到这了