此物件可將 SNMP 要求傳送到遠端裝置。

必須先於任何其他成員之前呼叫 Initialize4Initialize2

CoreAsp.SnmpRqst 使用的程序有三個步驟:

  1. 呼叫 Initialize4Initialize2,以針對特定裝置初始化物件。
  2. 設定選用參數,例如逾時值、連接埠……等等。
  3. 對裝置執行任何次數的 GetGetNextGetMultipleSet 作業。這些作業會直接傳回含有作業狀態與值的 ComSnmpResponse 物件(使用 Failed/GetValue/GetOid),或以 XML 資料形式傳回 SNMP 變數繫結清單(使用 GetPayload)。
重要: 自 WhatsUp Gold 24.0.1 起,嘗試使用 SNMP API 的使用者應使用 Initialize4 方法。

方法

說明

傳回

Initialize4( sAddress, nSnmpVersion, sCommunity, sUsername, sContext, nAuthProto, sAuthPwd, nPrivProto, sPrivPwd)

依指定參數中指定的裝置 ID,為該裝置初始化 SnmpRqst 物件。

  • sAddress:供 SNMP 連線使用的裝置 IP 位址。
  • nSnmpVersion:1、2、3。
  • sCommunity:v1 或 v2 的純文字社群名稱。
  • sUsername:SNMP v3 使用者名稱。
  • sContext:SNMP v3 內容 (context)。
  • nAuthProto:SNMP v3 驗證通訊協定。
  • sAuthPwd:SNMP v3 驗證密碼。
  • nPrivProto:SNMP v3 加密通訊協定。
  • sPrivPwd:SNMP v3 加密密碼。

ComResult 物件

Initialize2( sDeviceAddress, nCredentialID )

使用裝置的 IP 位址與儲存在 WhatsUp Gold 中的認證建立與該裝置的連線,並據以初始化 SnmpRqst 物件。只要該裝置的認證已在認證資料庫中設定,即可使用此方法為未在 WhatsUp Gold 中設定的裝置初始化 SnmpRqst

  • sDeviceAddress:要查詢的裝置位址或主機名稱。
  • nCredentialID:認證 ID 對應的正整數值,此 ID 屬於已在 WhatsUp Gold 中設定的認證。

ComResult 物件

SetTimeoutMs( nTimeoutInMilliSec)

以毫秒數設定逾時值。未指定時,預設逾時值是 2000 毫秒。

  • nTimeoutInMilliSec:此正整數代表毫秒數;一旦過了這個時間,未解除要求就必須終止。
    註: 此方法失敗時會傳回值;您需要物件變數才能擷取此值。例如:varComResult = SnmpRqst.SetTimeoutMs(5000);其中 varComResult 是一個 ComResult 物件。

ComResult 物件

SetNumRetries( nNumberRetries)

設定逾時要求的重試次數。未指定時,失敗的要求會重試一次。

  • nNumberRetries:正整數,代表逾時要求的重試次數。

若要讓每個要求只傳送一個 SNMP 封包,請將 nNumberRetries 設為 0(零)。

ComResult 物件

SetPort( nPort)

設定要由 SnmpRqst 使用的 TCP/IP 連接埠。未指定時,使用連接埠 161。

  • nPort:1 ~ 65535 之間的正整數,代表要使用的連接埠。

ComResult 物件

Get( sOid)

發出 SNMP Get 命令,擷取指定物件的值。

  • sOid:含有效 OID 的字串。

ComSnmpResponse 物件

GetNext( sOid)

發出 SNMP GetNext 命令,依字典順序擷取指定物件後面的物件值。

  • sOid:含有效 OID 的字串。

ComSnmpResponse 物件

GetMultiple( sListOfOids)

為指定的每個物件發出 SNMP Get 命令。GetMultiple 以單一 SNMP 通訊協定資料單元發送所有命令,因此比分別發出多個 Get 命令更有效率。

  • sListOfOids:以逗號分隔的有效 OID 清單。

ComSnmpResponse 物件

Set( sOid, sType, sValue)

發出 SNMP Set 命令,在裝置上設定 OID 值。

  • sOid:此字串包含的有效物件 OID,屬於您要為其設定數值的物件。
  • sType:單一字元,代表您要設定的數值類型。

    i = 整數

    u = 不帶正負號的整數

    s = 字串

    x = 十六進位字串

    d = 十進位字串

    n = NULL 物件

    o = 物件識別碼

    t = timetick

    a = IPv4 位址

    b = 位元數

  • sValue:包含所要設定值的字串。

ComSnmpResponse 物件

註: 除非裝置的 MIB 物件和群體字串擁有讀寫權限,否則 Set 函數沒有作用。

以下為範例函式,會根據與該裝置關聯的 SNMP 認證自動設定 Initialize4 參數:

' *****************

' * ConnectSNMP *

' *****************

Sub ConnectSNMP()

Dim bCredential : bCredential = 0

'Get SNMP Credential Data

Dim sFirstCommunity : sFirstCommunity = Context.GetProperty("CredSnmpV1:ReadCommunity")

Dim sSecondCommunity : sSecondCommunity = Context.GetProperty("CredSnmpV2:ReadCommunity")

Dim sUsername : sUsername = Context.GetProperty("CredSnmpV3:Username")

Dim sContext : sContext = Context.GetProperty("CredSnmpV3:Context")

Dim sAuthPwd : sAuthPwd = Context.GetProperty("CredSnmpV3:AuthPassword")

Dim sPrivPwd : sPrivPwd = Context.GetProperty("CredSnmpV3:EncryptPassword")

Dim nAuthProto : nAuthProto = 0

Dim nPrivProto : nPrivProto = 0

'initialize snmp cred version

Dim nSnmpVersion : nSnmpVersion = 1

Dim sCommunity : sCommunity = sFirstCommunity

' determine snmp cred version

If Len(sFirstCommunity) = 0 Then

If Len(sSecondCommunity) > 0 Then

nSnmpVersion = 2

sCommunity = sSecondCommunity

Else

If Len(sUsername) > 0 Then

nSnmpVersion = 3

Else

Context.LogMessage "There are no SNMP credentials assigned to this device"

bCredential = 1

End If

End If

End If

If bCredential = 0 Then

Dim rc, sOID, sOIDCore, sOIDtoLoop, sInstance

'Initialize and Test SNMP Connection

Set rc = oSnmp.Initialize4(sAddress,nSnmpVersion,sCommunity,sUsername,sContext,nAuthProto,sAuthPwd,nPrivProto,sPrivPwd)

If rc.Failed Then

sErrorMsg = rc.GetErrorMsg

bSNMPResult = 1

bFail = 1

End If

End If

End Sub