我正在尝试使用 Microsoft 的 WinSNMP 库创建一个示例应用程序来创建一个陷阱示例。请参阅下面的代码示例:
//Send a trap;
LPSTR strSrcAddr = "10.12.0.21";
LPSTR strDstAddr = "10.2.255.8";
UINT32 nDstPort = 462;
smiINT snmpPduType = SNMP_PDU_TRAP;
HSNMP_VBL snmpVarBindList = SnmpCreateVbl(snmpSession, NULL, NULL);
assert(snmpVarBindList != SNMPAPI_FAILURE);
//Copy the src address to src entity;
HSNMP_ENTITY snmpSrcEntity = SnmpStrToEntity(snmpSession, strSrcAddr);
assert(snmpSrcEntity != SNMPAPI_FAILURE);
//Copy the dst address to dst entity;
HSNMP_ENTITY snmpDstEntity = SnmpStrToEntity(snmpSession, strDstAddr);
assert(snmpDstEntity != SNMPAPI_FAILURE);
//Assign the dst entity the trap port;
snmpStatus = SnmpSetPort(snmpDstEntity, nDstPort);
assert(snmpStatus != SNMPAPI_FAILURE);
//Create the pdu, assigning it the TRAP_TYPE;
HSNMP_PDU snmpPdu = SnmpCreatePdu(snmpSession, snmpPduType, NULL, NULL, NULL, snmpVarBindList);
assert(snmpPdu != SNMPAPI_FAILURE);
//Send the msg;
smiBYTE pByteData[] = "public\0";
smiOCTETS snmpCtxOctects = {6, (smiLPBYTE)pByteData};
HSNMP_CONTEXT snmpCtxt = SnmpStrToContext(snmpSession, &snmpCtxOctects);
assert(snmpCtxt != SNMPAPI_FAILURE);
//////////////////////////////////
//
// Create the PDU and set Data;
//
//////////////////////////////////
/////
//-- Set the sysUpTime;
/////
CHAR pOidSysUpTimeArray[] = "1.3.6.1.2.1.1.3.0\0";
smiOID snmpOidSysUpTime = {0};
snmpStatus = SnmpStrToOid(pOidSysUpTimeArray, &snmpOidSysUpTime);
assert(snmpStatus != SNMPAPI_FAILURE);
smiVALUE smiValueSysUpTime = {0};
smiValueSysUpTime.syntax = SNMP_SYNTAX_TIMETICKS;
smiValueSysUpTime.value.uNumber = 40000; //Random up-time;
snmpStatus = SnmpSetVb(snmpVarBindList, 0, &snmpOidSysUpTime, &smiValueSysUpTime);
assert(snmpStatus != SNMPAPI_FAILURE);
/////
//-- Now set the snmpTrapOid;
/////
CHAR pOidSnmpTrapOid[] = "1.3.6.1.6.3.1.1.4.1.0\0";
CHAR pOidSnmpOidOfEntTrap[] = "1.3.6.1.4.1.37086.2.0.1\0";
smiOID snmpOidOfEntTrap = {0};
smiOID snmpOidTrapOid = {0};
snmpStatus = SnmpStrToOid(pOidSnmpTrapOid, &snmpOidTrapOid);
assert(snmpStatus != SNMPAPI_FAILURE);
snmpStatus = SnmpStrToOid(pOidSnmpOidOfEntTrap, &snmpOidOfEntTrap);
assert(snmpStatus != SNMPAPI_FAILURE);
smiVALUE smiValueEntTrap = {0};
smiValueEntTrap.syntax = SNMP_SYNTAX_OID;
smiValueEntTrap.value.oid = snmpOidOfEntTrap;
snmpStatus = SnmpSetVb(snmpVarBindList, 0, &snmpOidTrapOid, &smiValueEntTrap);
assert(snmpStatus != SNMPAPI_FAILURE);
/////
//-- Now add Vb for 1.3.6.1.4.1.37086.1.1.0
/////
CHAR pOidSnmpTrapValueOid[] = "1.3.6.1.4.1.37086.1.1.0\0";
smiOID snmpOidTrapValueOid = {0};
snmpStatus = SnmpStrToOid(pOidSnmpTrapValueOid, &snmpOidTrapValueOid);
assert(snmpStatus != SNMPAPI_FAILURE);
smiVALUE smiValueTrapValue = {0};
smiValueTrapValue.syntax = SNMP_SYNTAX_INT;
smiValueTrapValue.value.sNumber = 2; //failure;
snmpStatus = SnmpSetVb(snmpVarBindList, 0, &snmpOidTrapValueOid, &smiValueTrapValue);
assert(snmpStatus != SNMPAPI_FAILURE);
//////////////////////////////////
//
// Finished PDU creation;
//
//////////////////////////////////
//Attempt to send the message;
snmpStatus = SnmpSendMsg(snmpSession, snmpSrcEntity, snmpDstEntity, snmpCtxt, snmpPdu);
assert(snmpStatus != SNMPAPI_FAILURE);
snmpStatus = SnmpFreePdu(snmpPdu);
assert(snmpStatus != SNMPAPI_FAILURE);
snmpStatus = SnmpFreeVbl(snmpVarBindList);
assert(snmpStatus != SNMPAPI_FAILURE);
snmpStatus = SnmpFreeDescriptor(SNMP_SYNTAX_OID, (smiLPOPAQUE)&snmpOidSysUpTime);
assert(snmpStatus != SNMPAPI_FAILURE);
snmpStatus = SnmpFreeDescriptor(SNMP_SYNTAX_OID, (smiLPOPAQUE)&snmpOidTrapOid);
assert(snmpStatus != SNMPAPI_FAILURE);
snmpStatus = SnmpFreeDescriptor(SNMP_SYNTAX_OID, (smiLPOPAQUE)&smiValueEntTrap.value.oid);
assert(snmpStatus != SNMPAPI_FAILURE);
snmpStatus = SnmpFreeEntity(snmpSrcEntity);
assert(snmpStatus != SNMPAPI_FAILURE);
snmpStatus = SnmpFreeEntity(snmpDstEntity);
assert(snmpStatus != SNMPAPI_FAILURE);
我可以看到陷阱正在连接并被接收,但不幸的是 SNMP 管理器没有通知我陷阱。我有一种很好的感觉,问题是与我这边形成的陷阱有关,不是与管理器(SNMPc管理器)有问题吗?
这是我使用虚拟 OID 创建的自定义 MIB:
MARINA-MIB DEFINITIONS ::= BEGIN
IMPORTS
enterprises
FROM RFC1155-SMI
OBJECT-TYPE, NOTIFICATION-TYPE
FROM RFC-1212
;
epilogue OBJECT IDENTIFIER ::= {enterprises 37086}
marina OBJECT IDENTIFIER ::= {epilogue 1}
marinaNotices OBJECT IDENTIFIER ::= {epilogue 2}
marinaStatus OBJECT-TYPE
SYNTAX INTEGER {
ok (1),
failure (2)
}
MAX-ACCESS accessible-for-notify
STATUS current
DESCRIPTION
"The status of marina."
::= {marina 1}
marinaStatusNotification NOTIFICATION-TYPE
OBJECTS { marinaStatus }
STATUS current
DESCRIPTION
"This variable notifies listeners of the status of marina."
::= {marinaNotices 1}
END
此外,我还有一个指向捕获 Trap的Wireshark 转储的链接。
SNMPc 管理器允许我使用一个小工具发送虚拟陷阱;但是,这些陷阱是 SNMPv2,而 WinSNMP 使用的是 v1,所以我很难比较。任何建议将不胜感激。