使用 SNMP GetNext

Note: 此範例僅供說明之用,本公司不提供技術支援。Ipswitch 為內容物件、SNMP API 和指令碼環璄提供技術支援服務,但不對 JScript、VBScript,或動態指令碼監控工具或動作的開發及偵錯提供支援服務。如需此範例或自行編寫指令碼方面的協助,請造訪 WhatsUp Gold 使用者社群論壇

此效能監控工具會遊走 (Walk) hrStorageType MIB,以尋找儲存表中的硬碟。找到硬碟後,監控工具會取得硬碟索引,並輪詢新的物件 (儲存空間大小與單位)。

// This scripts walks hrStorageType to find hard disks in the storage table.
// A hard disk as a hrStorageType of "1.3.6.1.2.1.25.2.1.4" (hrStorageFixedDisk).
// Then it gets the indexes of the hard disk in that table and for each index, it polls two new
// objects in that table, the storage size and the units of that entry.
// It adds everything up and converts it in Gigabytes.
var hrStorageType = "1.3.6.1.2.1.25.2.3.1.2";

// Create and initialize the snmp object
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");
var nDeviceID = Context.GetProperty("DeviceID");
var oResult = oSnmpRqst.Initialize(nDeviceID);

var arrIndexes = new Array(); // array containing the indexes of the disks we found
// walk the column in the table:
var oSnmpResponse = oSnmpRqst.GetNext(hrStorageType);
if (oSnmpResponse.Failed) Context.SetResult(1, oSnmpResponse.GetPayload);
var sOid = String(oSnmpResponse.GetOid);
var sPayload = String(oSnmpResponse.GetPayload);

while (!oSnmpResponse.Failed && sOid < (hrStorageType + ".99999999999"))
{
if (sPayload == "1.3.6.1.2.1.25.2.1.4") {
// This storage entry is a disk, add the index to the table.
// the index is the last element of the OID:
var arrOid = sOid.split(".");
arrIndexes.push(arrOid[arrOid.length - 1]);
}

oSnmpResponse = oSnmpRqst.GetNext(sOid);
if (oSnmpResponse.Failed) Context.SetResult(1, oSnmpResponse.GetPayload);
sOid = String(oSnmpResponse.GetOid);
sPayload = String(oSnmpResponse.GetPayload);
}
Context.LogMessage("Found disk indexes:" + arrIndexes.toString());
if (arrIndexes.length == 0) Context.SetResult(1, "No disk found");

// now that we have the indexes of the disks.Poll their utilization and units
var nTotalDiskSize = 0;
for (var i = 0; i < arrIndexes.length; i++) {

oSnmpResponse = oSnmpRqst.Get("1.3.6.1.2.1.25.2.3.1.5."+ arrIndexes[i])
if (oSnmpResponse.Failed) Context.SetResult(1, oSnmpResponse.GetPayload);
nSize = oSnmpResponse.GetPayload;
oSnmpResponse = oSnmpRqst.Get("1.3.6.1.2.1.25.2.3.1.4."+ arrIndexes[i])
if (oSnmpResponse.Failed) Context.SetResult(1, oSnmpResponse.GetPayload);
nUnits = oSnmpResponse.GetPayload;

nTotalDiskSize += (nSize * nUnits);
}
// return the total size in gigabytes.
Context.SetValue(nTotalDiskSize / 1024 / 1024 / 1024); // output in Gigabytes

另請參閱

動態指令碼效能監控工具範例

以圖形表示印表機墨水存量使用率

輪詢參考變數並計算

以圖形表示溫度監控工具

輪詢多個參考變數