SNMP trap not recognized by Manager

2019-08-09 14:40发布

问题:

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.

回答1:

As Lex Li proposed, I'm adding my answer:

The traps should be sent to port no 162