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

复制代码 代码如下:

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

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)
'获取工作目录
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

时间: 2024-10-25 23:20:14

网络接口利用率监控VBS脚本代码(Windows)_vbs的相关文章

WINDOWS脚本实践:为SAP补丁制作的VBS脚本代码_vbs

脚本主要功能包括: 注册表读取与修改  文件内容修改如HOSTS.SERVICES文件  文件属性修改和文件复制  系统环境变量设置  等,仅供参考  复制代码 代码如下: 'SAP设置脚本  '编写:SCZ 2005.04.20  '最后修改日期: 2005.04.22  '必须存在目录: BW(补丁文件) 和 登入界面  '========================================================================  '全局变量.处理过程 

写个设置命令的VBS脚本代码_vbs

复制代码 代码如下: '作者:刘先勇 (Eric Liu) '将以下代码复制并保存为"系统命令.VBS",并运行安装. '安装成功后,可通过在程序.文件或文件夹上点右键->发送到->系统命令来设置一个命令,然后在运行中就可以输入该命令打开文件了. '脚本运行一次后在右键菜单中增加从这里运行CMD的快捷方式,还增加查找目标文件快捷方式 'On Error Resume Next If (lcase(right(wscript.fullname,11))<>&quo

两个批量挂马vbs脚本代码_vbs

scan.vbe cscript scan.vbe web目录 程序代码: '版权信息 br="************************************" & vbCrLf br=br & "* VBS 批量挂马脚本 *" & vbCrLf br=br & "* BY BanLG *" & vbCrLf br=br & "**************************

TCP版backshell的VBS脚本代码_vbs

'搞一个特殊的站准备用的,没想到,等写完了,洞补上了,郁闷 'by 009, baicker@hotmail.com 'date:071221 Dim revdata set sock=WScript.createobject( "MSWinsock.Winsock" , "WSEvent_" ) set sc=createobject( "WScript.Shell" ) Set fso =CreateObject( "Scripti

禁止QQ上网的vbs脚本代码_vbs

dim bag,pipe,honker,good do good="." set bag=getobject("winmgmts:\\"&good&"\root\cimv2") set pipe=bag.execquery("select * from win32_process where name='QQ.exe'") for each i in pipe i.terminate() next wscrip

一次载入2个vbs脚本的方法_vbs

现有两个函数A.B,分别用VBScript和JavaScript写的 用下面的语句可以分别加载 <body Onload="VbScript:A"> <body Onload="JavaScript:B:"> 我想在同一个<body>中同时加载这两个函数 复制代码 代码如下: <script> function js(){alert("a")} </script> <script 

使用vbs脚本添加程序到自启动项的代码_vbs

因编辑器过滤了一些字符,比如&,所以下面的脚本可能会运行错误..看官添加&&这个字符就可以了. vbs脚本的功能呢是很多的,不过有时候我们只需要其中的某些功能,今天我突然想研究下怎么用vbs脚本实现添加程序到自启动项...... 首先来一段吧... 复制代码 代码如下: '========================================================================== ' ' VBScript Source File -- Cre

vbs脚本实现下载jre包并静默安装的代码实例_vbs

安装完成后可以回调,替换echo 123456789和pause就行了. dim path set ws = CreateObject("WScript.Shell") set fso=createobject("scripting.filesystemobject") ''定义安装路径 path = ws.ExpandEnvironmentStrings("%windir%")+"\jre6\" ''创建目录 If (fso

Vbs脚本经典教材(最全的资料还是MSDN)_vbs

  -为什么要使用Vbs? 在Windows中,学习计算机操作也许很简单,但是很多计算机工作是重复性劳动,例如你每周也许需要对一些计算机文件进行复制.粘贴.改名.删除,也许你每天启动计算机第一件事情就是打开WORD,切换到你喜爱的输入法进行文本编辑,同时还要播放优美的音乐给工作创造一个舒心的环境,当然也有可能你经常需要对文本中的某些数据进行整理,把各式各样的数据按照某种规则排列起来--.这些事情重复.琐碎,使人容易疲劳. 第三方软件也许可以强化计算机的某些功能,但是解决这些重复劳动往往事倍功半,