Send an SNMP Trap with Custom Objects


When sending SNMP traps with custom variable bindings, required system objects are not added automatically. To construct a valid trap, you must explicitly include these objects before adding your own data.

When using SNMPAgent or SNMPTCPAgent to send custom traps, you must manually include the required sysUpTime and snmpTrapOid objects before adding your own variable bindings.

C#

//Add the sysUpTime object.
snmpagent1.Objects.Add(new SNMPObject());
snmpagent1.Objects[0].ObjectType = SNMPObjectTypes.otTimeTicks;
snmpagent1.Objects[0].Oid = "1.3.6.1.2.1.1.3.0";
snmpagent1.Objects[0].Value = snmpagent1.SysUpTime.ToString();

//Add the snmpTrapOid object.
snmpagent1.Objects.Add(new SNMPObject());
snmpagent1.Objects[1].ObjectType = SNMPObjectTypes.otObjectId;
snmpagent1.Objects[1].Oid = "1.3.6.1.6.3.1.1.4.1.0";
snmpagent1.Objects[1].Value = "YourTrapOID";

//Now you can add your own objects.
snmpagent1.Objects.Add(new SNMPObject());
snmpagent1.Objects[2].ObjectType = SNMPObjectTypes.otOctetString;
snmpagent1.Objects[2].Oid = "YourObjectOID";
snmpagent1.Objects[2].Value = "test";

snmpagent1.SendTrap("RemoteHost", "Ignored");

Java

//Add the sysUpTime object.
snmpagent1.getObjects().add(new SNMPObject());
snmpagent1.getObjects().item(0).setOid("1.3.6.1.2.1.1.3.0");
snmpagent1.getObjects().item(0).setObjectType(SNMPObject.otTimeTicks);
snmpagent1.getObjects().item(0).setValue(String.valueOf(snmpagent1.getSysUpTime()));

//Add the snmpTrapOid object.
snmpagent1.getObjects().add(new SNMPObject());
snmpagent1.getObjects().item(1).setObjectType(SNMPObject.otObjectId);
snmpagent1.getObjects().item(1).setOid("1.3.6.1.6.3.1.1.4.1.0");
snmpagent1.getObjects().item(1).setValue("YourTrapOID");    

//Now you can add your own objects.
snmpagent1.getObjects().add(new SNMPObject());
snmpagent1.getObjects().item(2).setObjectType(SNMPObject.otOctetString);
snmpagent1.getObjects().item(2).setOid("YourObjectOID");
snmpagent1.getObjects().item(2).setValue("test");

snmpagent1.sendTrap("RemoteHost", "Ignored");

C++

//Add the sysUpTime object.
snmpagent1.SetObjType(0, OT_TIME_TICKS);
snmpagent1.SetObjId(0, "1.3.6.1.2.1.1.3.0");
char buffer[10];
itoa(agent.GetSysUpTime(), buffer, 10);
snmpagent1.SetObjValue(0, buffer, strlen(buffer));

//Add the snmpTrapOid object.
snmpagent1.SetObjType(1, OT_OBJECT_ID);
snmpagent1.SetObjId(1, "1.3.6.1.6.3.1.1.4.1.0");
snmpagent1.SetObjValue(1, "YourTrapOID", strlen("YourTrapOID"));

//Now you can add your own objects.
snmpagent1.SetObjType(2, OT_OCTET_STRING);
snmpagent1.SetObjId(2, "YourObjectOID");
snmpagent1.SetObjValue(2, "test", strlen("test"));

snmpagent1.SendTrap("RemoteHost", "Ignored");

The object collection supports explicit definition as shown above. When the snmpTrapOid object is specified manually, its value takes precedence, and the trapOID parameter passed to SendTrap is ignored.

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.