主页 > 知识库 > 网络接口利用率监控VBS脚本代码(Windows)

网络接口利用率监控VBS脚本代码(Windows)

热门标签:呼叫中心市场需求 服务器配置 铁路电话系统 智能手机 检查注册表项 网站文章发布 美图手机 银行业务
复制代码 代码如下:

'程序名称: btlwchk_netinterface.vbs
'程序用途: 监测windows主机网络接口利用率
'创建日期: 2011-09-1
'作者信息: zhangkai
'运行环境: vbs
'处理参数

MonSubject="NetInterface"
Set Args=WScript.Arguments
If (Args.Count1) 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
您可能感兴趣的文章:
  • vbs定期监控 值个班,定期瞄一下
  • 磁盘IO利用率监控VBS脚本(windows)
  • VBScript 监控并结束指定进程的代码
  • VBScript 监控磁盘更改事件实现代码
  • VBS监控CPU的使用率(如占用率一直维持在80%超过30秒则运行某程序)
  • 进程监控实现代码[vbs+bat]
  • 用VBS调用程序并对程序的运行情况进行监控的两个代码
  • 如何使用vb 监控电脑活动记录

标签:沧州 红河 新疆 长治 河南 乐山 沈阳 上海

巨人网络通讯声明:本文标题《网络接口利用率监控VBS脚本代码(Windows)》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266