管理器无法识别 SNMP 陷阱
我正在尝试使用 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
此外,我还有一个指向
SNMPc Manager 确实允许我使用一个小工具发送虚拟陷阱;然而,这些陷阱是SNMPv2,而WinSNMP使用的是v1,所以我很难比较。任何建议将不胜感激。
I'm attempting to create a sample application utilizing Microsoft's WinSNMP library to create an example of a trap. See the code sample below:
//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);
I can see that the trap is hitting the wire and is being received but unfortunately the SNMP manager is not notifying me of the trap. I have a good feeling the problem is with the formation of the trap my side on not a problem with the manager (SNMPc manager)?
This is the custom MIB that I have created with a dummy OID:
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
In addition, I have a link to the Wireshark dump of a capture of the Trap.
SNMPc Manager does allow me to send dummy traps using a little tool; however, these traps are SNMPv2 and WinSNMP is using v1, so it is difficult for me to compare. Any suggestions would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Lex Li 提议的那样,我添加了我的答案:
陷阱应该发送到端口号 162
As Lex Li proposed, I'm adding my answer:
The traps should be sent to port no 162