SNMP Code Example: Sending an SNMP trap with custom objects
When sending a trap with SNMPAgent/SNMPTCPAgent there may be some cases where you want to create your own variable binding objects. If you wish to do this, you are responsible for including the sysUpTime and snmpTrapOid objects before your objects (the component handles this automatically for built-in traps).
The code snippets below show how this is done using the SNMPAgent component, but the same concepts apply for the SNMPTCPAgent component. Note that the trapOID value passed to the SendTrap method is ignored in favor of the trap OID value you explicitly specify in the snmpTrapOid object.
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");
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.