Manager无法识别SNMP陷阱

时间:2010-12-21 11:11:19

标签: c++ snmp snmp-trap winsnmp

我正在尝试使用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

此外,我还有一个捕获陷阱Wireshark dump的链接。

SNMPc Manager允许我使用一个小工具发送虚拟陷阱;但是,这些陷阱是SNMPv2而WinSNMP正在使用v1,所以我很难比较。任何建议都会非常感激。

1 个答案:

答案 0 :(得分:1)

正如李克强提出的那样,我正在补充我的答案:

应将陷阱发送到162号港口