Copy codeThe Code is as follows: 'program name: btlwchk_netinterface.vbs
'Program purpose: monitor the Network Interface Usage of windows Hosts
'Creation Date: 2011-09-1
'Author information: zhangkai
'Runtime 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 the 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 Public Libraries
Set g_fileSys = createObject ("Scripting. FileSystemObject ")
Sub shortdefile (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 ob1_miservice = GetObject ("winmgmts:" & "{impersonationLevel = impersonate }! \ "& StrComputer &" \ root \ cimv2 ")
'100 sampling times
Set colItems = ob1_miservice. 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
'100 sampling times
WScript. Sleep 1000
Set colItems = ob1_miservice. ExecQuery ("Select * from Win32_PerfRawData_Tcpip_NetworkInterface", 48)
Dim BytesTotalPersec2 (20), TimeValue2 (20), Name2 (20), bytes (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
'Bandwidth utilization = (BytesReceivedPerSec + BytesSentPerSec) * 8*100/CurrentBandwidth
'Computing Utilization Rate
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)
'Calculate BytesReceivedPerSec
BytesReceivedPerSec (j) = (BytesReceivedPerSec2 (j)-BytesReceivedPerSec1 (j)/(TimeValue2 (j)-TimeValue1 (j)/TimeBase)/1024
BytesReceivedPerSec (j) = round (BytesReceivedPerSec (j), 2)
'Calculate BytesSentPerSec
BytesSentPerSec (j) = (BytesSentPerSec2 (j)-BytesSentPerSec1 (j)/(TimeValue2 (j)-TimeValue1 (j)/TimeBase)/1024
BytesSentPerSec (j) = round (BytesSentPerSec (j), 2)
'Calculate PacketsReceivedPerSec
PacketsReceivedPerSec (j) = (PacketsReceivedPerSec2 (j)-PacketsReceivedPerSec1 (j)/(TimeValue2 (j)-TimeValue1 (j)/TimeBase)
PacketsReceivedPerSec (j) = round (PacketsReceivedPerSec (j), 2)
'Calculate PacketsSentPerSec
PacketsSentPerSec (j) = (PacketsSentPerSec2 (j)-PacketsSentPerSec1 (j)/(TimeValue2 (j)-TimeValue1 (j)/TimeBase)
PacketsSentPerSec (j) = round (PacketsSentPerSec (j), 2)
'Calculate 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