网络接口的监控工具带宽利用率

<注意> 此范例仅供说明之用,本公司不提供技术支持。Ipswitch 为内容对象、SNMP API 和脚本环璄提供技术支持服务,但不对 JScript、VBScript,或活动脚本监控工具或操作的开发及侦错提供支持服务。如需此范例或自行编写脚本方面的协助,请造访 WhatsUp Gold 用户社群论坛

此主动监控工具会轮询网络接口 MIB 的值,以监控网络接口的带宽总利用率 (In Octet 与 Out Octet)。

// Settings for this monitor:
// the interface index ifIndex:
var nInterfaceIndex = 65540;

// this monitor will fail if the interface utilization goes above this current ratio:
// current bandwidth / maxBandwidth > nMaxInterfaceUtilizationRatio
var nMaxInterfaceUtilizationRatio = 0.7; // Set to 70%

// Create an SNMP object, that will poll the device.
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");

// Get the device ID
var nDeviceID = Context.GetProperty("DeviceID");

// This function polls the device returns the ifSpeed of the inteface indexed by nIfIndex.
// ifSpeed is in bits per second.
function getIfSpeed(nIfIndex) {
var oResult = oSnmpRqst.Initialize(nDeviceID);
if (oResult.Failed) {
return null;
}
return parseInt(SnmpGet("1.3.6.1.2.1.2.2.1.5."+ nIfIndex)); // ifSpeed
}
// Function to get SNMP ifInOctets for the interface indexed by nIfIndex (in bytes).
// Returns the value polled upon success, null in case of failure.
function getInOctets(nIfIndex) {
var oResult = oSnmpRqst.Initialize(nDeviceID);
if (oResult.Failed) {
return null;
}
return parseInt(SnmpGet("1.3.6.1.2.1.2.2.1.10."+ nIfIndex)); // inOctets
}

// Function to get SNMP ifOutOctets for the interface indexed by nIfIndex (in bytes).
// Returns the value polled upon success, null in case of failure.
function getOutOctets(nIfIndex) {
var oResult = oSnmpRqst.Initialize(nDeviceID);
if (oResult.Failed) {
return null;
}
return parseInt(SnmpGet("1.3.6.1.2.1.2.2.1.16."+ nIfIndex)); // outOctets
}

// Helper function to get a specific SNMP object (OID in sOid).
// Returns the value polled upon success, null in case of failure.
function SnmpGet(sOid) {
var oResult = oSnmpRqst.Get(sOid);
if (oResult.Failed) {
return null;
}
else {
return oResult.GetPayload;
}
}

// Get the current date.It will be used as a reference date for the SNMP polls.
var oDate = new Date();
var nPollDate = parseInt(oDate.getTime()); // get the date in millisec in an integer.
// Do the actual polling:
var nInOctets = getInOctets(nInterfaceIndex);
var nOutOctets = getOutOctets(nInterfaceIndex);
var nIfSpeed = getIfSpeed(nInterfaceIndex);
if (nInOctets == null || nOutOctets == null || nIfSpeed == null) {
Context.SetResult(1, "Failure to poll this device.");
}
else {
var nTotalOctets = nInOctets + nOutOctets;
// Retrieve the octets value and date of the last poll saved in a context variable:
var nInOutOctetsMonitorPreviousPolledValue = Context.GetProperty("nInOutOctetsMonitorPreviousPolledValue");
var nInOutOctetsMonitorPreviousPollDate = Context.GetProperty("nInOutOctetsMonitorPreviousPollDate");
if (nInOutOctetsMonitorPreviousPolledValue == null || nInOutOctetsMonitorPreviousPollDate == null) {
// the context variable has never been set, this is the first time we are polling.
Context.LogMessage("This monitor requires two polls.");
Context.SetResult(0, "success");
}
else {
// compute the bandwidth that was used between this poll and the previous poll
var nIntervalSec = (nPollDate - nInOutOctetsMonitorPreviousPollDate) / 1000; // time since last poll in seconds
var nCurrentBps = (nTotalOctets - nInOutOctetsMonitorPreviousPolledValue) * 8 / nIntervalSec;
Context.LogMessage("total octets for interface " + nInterfaceIndex + " = " + nTotalOctets);
Context.LogMessage("previous value = " + nInOutOctetsMonitorPreviousPolledValue);
Context.LogMessage("difference:" + (nTotalOctets - nInOutOctetsMonitorPreviousPolledValue) + " bytes");
Context.LogMessage("Interface Speed:" + nIfSpeed + "bps");
Context.LogMessage("time elapsed since last poll:" + nIntervalSec + "s");
Context.LogMessage("Current Bandwidth utilization:" + nCurrentBps + "bps");
if (nCurrentBps / nIfSpeed > nMaxInterfaceUtilizationRatio) {
Context.SetResult(1, "Failure:bandwidth used on this interface " + nCurrentBps + "bps / total available:" + nIfSpeed + "bps is above the specified ratio:" + nMaxInterfaceUtilizationRatio);
}
else {
Context.SetResult(0, "Success");
}
}
// Save this poll information in the context variables:
Context.PutProperty("nInOutOctetsMonitorPreviousPolledValue", nTotalOctets);
Context.PutProperty("nInOutOctetsMonitorPreviousPollDate", nPollDate);
}

另请参阅

活动脚本主动监控工具范例

监控打印机墨水存量与利用率

在温度超过或低于范围时发出警报

判断无效的用户账号活动

监控在非标准端口上执行的 SNMP 代理程序

监控不明的 MAC 地址