Question about notificaction/trap

Hi,
I´m new about SNMP and i need help to understand trap and notification.
I make a agent using Snmp4JAgent 2.7.4 and work fine.
But i don´t see who use this notification in my agent, i need send a trap programmatically, but i don´t see who use the configuration a make in my agent to send this trap.

I work with SNMP++ rather than SNMP4J, but the class structure should be similar. I will explain how I do it in Agent++, and maybe that will help you in the SNMP4J domain.

========================================================

First, you must decide how the trap receiving machine will recognize your trap message. The trap receiver must know a few things about how the agent sends the trap. The receiving machine runs a trap daemon that listens for trap messages. It needs information to identify those messages. This information is encoded in the /etc/snmp/snmptrapd.conf file on the receiving machine.

authcommunity log,execute,net public
snmpTrapAddr 127.0.0.1:162

The first line tells the trap daemon to accept SNMPv1 and SNMPv2 trap messages that are sent with
the “public” community. The second line identifies where the daemon should listen for trap messages.

If you need to send SNMPv3 traps, the trap receiver must modify its configuration file to create a user (with the createUser) command and to authorize that user to receive the trap message with the authUser command.

createUser -e <agent’s SNMPv3 engine ID> test MD5 “testAuth” DES “testPriv”
authUser log,execute.net test authPriv

========================================================

The Agent++ examples all included initialization code that handled the initialization for SNMPv1 and SNMPv2c traps. If you need to send SNMPv3 traps, you need to create a user and assign authorization and privacy passwords for the user. Let’s skip that part unless you need it.

The default initialization added the “public” community to the required groups and gave it the necessary authorizations. Hopefully, your examples did the same. If you need further help with those steps, let me know.

Once the initialization is done, the agent must know where to send its trap message. The following example sends a trap notification to a trap daemon running on the local machine and listening on the default SNMP trap port of 162.

NotificationOriginator no(gpMib);
UdpAddress dest(“127.0.0.1/162”);
//
// Add table entries for traps.
//
no.add_v1_trap_destination(dest, “defaultV1Trap”, “v1trap”, “public”);

Assuming that your agent initialization is correct, the agent sends a trap notification by creating a class instance derived from the NotificationOriginator class and calling its notify() method. In my agent, I create separate classes for each trap that the agent sends.

class TrapMinVswrAlarm : public NotificationOriginator
{
public:
TrapMinVswrAlarm(Mib *pMib) : NotificationOriginator(pMib) {};
virtual ~TrapMinVswrAlarm() {};

virtual void generate(Vbx *vbs, unsigned int sz, const NS_SNMP OctetStr &context)
{
    notify(context, OID_TRAP_MIN_VSWR_ALARM, vbs, sz, 0);
};

};

Some traps require a list of variables in the message. You can generate these variables in the following way.

void TrapMgr::sendTrapMinVswrAlarm()
{
const unsigned int numVars = 4;
Vbx* vbs = new Vbx[numVars];
int n = 0;

//
//  Construct the variable bindings. 
//
vbs[n  ].set_oid(OID_NAME);
vbs[n++].set_value(mpModel->Name().c_str());
vbs[n  ].set_oid(OID_MIN_VSWR_LIMIT);
vbs[n++].set_value((int32_t)mpModel->MinVswrLimit());
vbs[n  ].set_oid(OID_MIN_VSWR_ALARM);
if (eActive == mpModel->MinVswrAlarm())
{
    vbs[n++].set_value(2);
}
else vbs[n++].set_value(1);	
vbs[n  ].set_oid(OID_VSWR);
vbs[n++].set_value((int32_t)mpModel->Vswr());
//
//  Create and send the SNMP trap. 
//
TrapMinVswrAlarm trapAlarm(gpMib);

trapAlarm.generate(vbs, numVars, "");
delete[] vbs;

}

If the trap message does not need any variables in its message, use:

const unsigned int numVars = 0;
Vbx* vbs = NULL;

That’s about it. Hopefully, the process in Agent4J is similar enough that you can figure it out.

Regards,
Chuck

Ty Chuck,
my problem was send traps using my agent config.
My current solution i have one poll and the end of the execution i need send trap.
In you example “TrapMinVswrAlarm(Mib *pMib) : NotificationOriginator(pMib) {};” i got the tip.

I made public this metod “notificationOriginator.notify” and now a can send traps using my agent config.
I don´t know if this is the best approach, but work for me.

Ty for the help.