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

时间:2021-05-22

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.ArgumentsIf (Args.Count<1) ThenWscript.Echo MonSubject & " -1:command line error"WScript.Quit(3)End Ifstrcid=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 = nothingend subincludeFile 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=0For Each objItem in colItemsName1(n) = objItem.NameBytesTotalPersec1(n) = objItem.BytesTotalPersecBytesReceivedPerSec1(n) = objItem.BytesReceivedPerSecBytesSentPerSec1(n) = objItem.BytesSentPerSecPacketsReceivedPerSec1(n) = objItem.PacketsReceivedPerSecPacketsSentPerSec1(n) = objItem.PacketsSentPerSecTimeValue1(n) = objItem.Timestamp_PerfTimeCurrentBandwidth(n) = objItem.CurrentBandwidthTimeBase = objItem.Frequency_PerfTimen=n+1Next'第2次采样WScript.Sleep 1000Set 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=0For Each objItem in colItemsName2(k) = objItem.Name BytesTotalPersec2(k) = objItem.BytesTotalPersecBytesReceivedPerSec2(k) = objItem.BytesReceivedPerSecBytesSentPerSec2(k) = objItem.BytesSentPerSecPacketsReceivedPerSec2(k) = objItem.PacketsReceivedPerSecPacketsSentPerSec2(k) = objItem.PacketsSentPerSecTimeValue2(k) = objItem.Timestamp_PerfTimek=k+1Nextj=0For i=0 to n-1If TimeValue2(j) - TimeValue1(j) = 0 Thenstrnetwork = "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)'计算BytesReceivedPerSecBytesReceivedPerSec(j) = (BytesReceivedPerSec2(j) - BytesReceivedPerSec1(j)) / ((TimeValue2(j) - TimeValue1(j)) / TimeBase)/1024BytesReceivedPerSec(j) = round(BytesReceivedPerSec(j),2)'计算BytesSentPerSecBytesSentPerSec(j) = (BytesSentPerSec2(j) - BytesSentPerSec1(j)) / ((TimeValue2(j) - TimeValue1(j)) / TimeBase)/1024BytesSentPerSec(j) = round(BytesSentPerSec(j),2)'计算PacketsReceivedPerSecPacketsReceivedPerSec(j) = (PacketsReceivedPerSec2(j) - PacketsReceivedPerSec1(j)) / ((TimeValue2(j) - TimeValue1(j)) / TimeBase)PacketsReceivedPerSec(j) = round(PacketsReceivedPerSec(j),2)'计算PacketsSentPerSecPacketsSentPerSec(j) = (PacketsSentPerSec2(j) - PacketsSentPerSec1(j)) / ((TimeValue2(j) - TimeValue1(j)) / TimeBase)PacketsSentPerSec(j) = round(PacketsSentPerSec(j),2)'计算CurrentBandwidthCurrentBandwidth(j) = CInt(CurrentBandwidth(j)/1000/1000)End IfIf j = 0 ThenWscript.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=" & PercentNetworkj=j+1Next

这篇文章就介绍到这了

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章