Network Interface Utilization Monitoring VBS script code (WINDOWS) _vbs

Source: Internet
Author: User
Copy Code code as follows:

' Program name: Btlwchk_netinterface.vbs
' Application: Monitor Windows Host network interface utilization
' Date Created: 2011-09-1
' Author info: Zhangkai
' Operating environment: VBS
' Processing parameters

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)
' Get working directory
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
' Load the Public library
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"


' Output version and Help information
Getverhelp strcid, "v1.00", "<cid>"

StrComputer = "."
Set objWMIService = GetObject ("winmgmts:" & "{impersonationlevel=impersonate}!\\" & StrComputer & "\root\ Cimv2 ")

' 1th Time sampling
Set colitems = objWMIService.ExecQuery ("SELECT * from Win32_perfrawdata_tcpip_networkinterface", 48)
Dim BYTESTOTALPERSEC1 (TimeValue1), Name1 (a), Currentbandwidth (a), BYTESRECEIVEDPERSEC1 (20), BYTESSENTPERSEC1 (PACKETSRECEIVEDPERSEC1), PACKETSSENTPERSEC1 (20), _
Bytestotalpersec (Percentnetwork), Bytesreceivedpersec (a), bytessentpersec (a), 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

' 2nd time Sampling
Wscript.Sleep 1000
Set colitems = objWMIService.ExecQuery ("SELECT * from Win32_perfrawdata_tcpip_networkinterface", 48)
Dim BytesTotalPersec2 (TimeValue2), Name2 (a), BYTESRECEIVEDPERSEC2 (a), BYTESSENTPERSEC2 (20), PACKETSRECEIVEDPERSEC2, 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
' Bandwidth utilization = (bytesreceivedpersec + bytessentpersec) *8*100/currentbandwidth
' Compute utilization
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)

' Compute bytesreceivedpersec
Bytesreceivedpersec (j) = (BYTESRECEIVEDPERSEC2 (j)-BYTESRECEIVEDPERSEC1 (J))/((TimeValue2 (j)-TimeValue1 (j))/ Timebase)/1024
Bytesreceivedpersec (j) = Round (Bytesreceivedpersec (j), 2)

' Compute bytessentpersec
Bytessentpersec (j) = (BYTESSENTPERSEC2 (j)-BYTESSENTPERSEC1 (J))/((TimeValue2 (j)-TimeValue1 (j))/timebase)/1024
Bytessentpersec (j) = Round (Bytessentpersec (j), 2)

' Compute packetsreceivedpersec
Packetsreceivedpersec (j) = (PACKETSRECEIVEDPERSEC2 (j)-PACKETSRECEIVEDPERSEC1 (J))/((TimeValue2 (j)-TimeValue1 (j))/ Timebase)
Packetsreceivedpersec (j) = Round (Packetsreceivedpersec (j), 2)

' Compute packetssentpersec
Packetssentpersec (j) = (PACKETSSENTPERSEC2 (j)-PACKETSSENTPERSEC1 (J))/((TimeValue2 (j)-TimeValue1 (j))/Timebase)
Packetssentpersec (j) = Round (Packetssentpersec (j), 2)

' Compute 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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.