Board logo

标题: [原创] 网络接口利用率监控VBS脚本(Windows) [打印本页]

作者: zhangrenfang    时间: 2011-9-6 16:26     标题: 网络接口利用率监控VBS脚本(Windows)

本帖最后由 zhangrenfang 于 2012-2-28 14:57 编辑
  1. '程序名称:      btlwchk_netinterface.vbs
  2. '程序用途:      监测windows主机网络接口利用率
  3. '创建日期:      2011-09-1
  4. '作者信息:      zhangkai
  5. '运行环境:      vbs
  6. 'QQ:             276371791
  7. 'email:          zhangrenfang@sohu.com
  8. MonSubject="NetInterface"
  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次采样
  38. Set colItems = objWMIService.ExecQuery("Select * from Win32_PerfRawData_Tcpip_NetworkInterface",,48)
  39. Dim BytesTotalPersec1(20),TimeValue1(20),Name1(20),CurrentBandwidth(20),BytesReceivedPerSec1(20),BytesSentPerSec1(20),PacketsReceivedPerSec1(20),PacketsSentPerSec1(20),_
  40. BytesTotalPersec(20),PercentNetwork(20),BytesReceivedPerSec(20),BytesSentPerSec(20),PacketsReceivedPerSec(20),PacketsSentPerSec(20)
  41. n=0
  42. For Each objItem in colItems
  43.   Name1(n) = objItem.Name
  44.   BytesTotalPersec1(n) = objItem.BytesTotalPersec
  45.   BytesReceivedPerSec1(n) = objItem.BytesReceivedPerSec
  46.   BytesSentPerSec1(n) = objItem.BytesSentPerSec
  47.   PacketsReceivedPerSec1(n) = objItem.PacketsReceivedPerSec
  48.   PacketsSentPerSec1(n) = objItem.PacketsSentPerSec
  49.   TimeValue1(n) = objItem.Timestamp_PerfTime
  50.   CurrentBandwidth(n) = objItem.CurrentBandwidth
  51.   TimeBase = objItem.Frequency_PerfTime
  52.   n=n+1
  53. Next
  54. '第2次采样
  55. WScript.Sleep 1000
  56. Set colItems = objWMIService.ExecQuery("Select * from Win32_PerfRawData_Tcpip_NetworkInterface",,48)
  57. Dim BytesTotalPersec2(20),TimeValue2(20),Name2(20),BytesReceivedPerSec2(20),BytesSentPerSec2(20),PacketsReceivedPerSec2(20),PacketsSentPerSec2(20)
  58. k=0
  59. For Each objItem in colItems
  60.   Name2(k) = objItem.Name
  61.      BytesTotalPersec2(k) = objItem.BytesTotalPersec
  62.   BytesReceivedPerSec2(k) = objItem.BytesReceivedPerSec
  63.   BytesSentPerSec2(k) = objItem.BytesSentPerSec
  64.   PacketsReceivedPerSec2(k) = objItem.PacketsReceivedPerSec
  65.   PacketsSentPerSec2(k) = objItem.PacketsSentPerSec
  66.   TimeValue2(k) = objItem.Timestamp_PerfTime
  67.   k=k+1
  68. Next
  69. j=0
  70. For i=0 to n-1
  71. If TimeValue2(j) - TimeValue1(j) = 0 Then
  72.   strnetwork = "BytesTotalPersec=0"
  73. Else
  74.   '带宽利用率= (BytesReceivedPerSec + BytesSentPerSec)*8*100/ CurrentBandwidth
  75.   '计算利用率
  76.   BytesTotalPersec(j) = (BytesTotalPersec2(j) - BytesTotalPersec1(j)) / ((TimeValue2(j) - TimeValue1(j)) / TimeBase)
  77.   PercentNetwork(j) = BytesTotalPersec(j)*8*100 / CurrentBandwidth(j)
  78.   PercentNetwork(j) = round(PercentNetwork(j),2)
  79.   
  80.   '计算BytesReceivedPerSec
  81.   BytesReceivedPerSec(j) = (BytesReceivedPerSec2(j) - BytesReceivedPerSec1(j)) / ((TimeValue2(j) - TimeValue1(j)) / TimeBase)/1024
  82.   BytesReceivedPerSec(j) = round(BytesReceivedPerSec(j),2)
  83.   
  84.   '计算BytesSentPerSec
  85.   BytesSentPerSec(j) = (BytesSentPerSec2(j) - BytesSentPerSec1(j)) / ((TimeValue2(j) - TimeValue1(j)) / TimeBase)/1024
  86.   BytesSentPerSec(j) = round(BytesSentPerSec(j),2)
  87.   
  88.   '计算PacketsReceivedPerSec
  89.   PacketsReceivedPerSec(j) = (PacketsReceivedPerSec2(j) - PacketsReceivedPerSec1(j)) / ((TimeValue2(j) - TimeValue1(j)) / TimeBase)
  90.   PacketsReceivedPerSec(j) = round(PacketsReceivedPerSec(j),2)
  91.   
  92.   '计算PacketsSentPerSec
  93.   PacketsSentPerSec(j) = (PacketsSentPerSec2(j) - PacketsSentPerSec1(j)) / ((TimeValue2(j) - TimeValue1(j)) / TimeBase)
  94.   PacketsSentPerSec(j) = round(PacketsSentPerSec(j),2)
  95.   
  96.   '计算CurrentBandwidth
  97.   CurrentBandwidth(j) = CInt(CurrentBandwidth(j)/1000/1000)
  98. End If
  99. If j = 0 Then
  100.   Wscript.Echo MonSubject & " 0:OK|Name=" & Name1(j) & ",PercentNetwork=" & PercentNetwork(j) & ",BytesReceivedPerSec=" & BytesReceivedPerSec(j) & ",BytesSentPerSec=" & BytesSentPerSec(j) _
  101.   & ",PacketsReceivedPerSec=" & PacketsReceivedPerSec(j) & ",PacketsSentPerSec=" & PacketsSentPerSec(j) & ",CurrentBandwidth=" & CurrentBandwidth(j)
  102. End If
  103. 'Wscript.Echo MonSubject & " 0:OK|" & Name1(j) & ",PercentNetwork=" & PercentNetwork
  104. j=j+1
  105. Next
复制代码

作者: zhangop9    时间: 2021-1-2 19:01

'装载公共库




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2