Template:FAQ:Coding 18

From Net-SNMP Wiki
Jump to: navigation, search

There are two aspects to having the agent generate a trap - knowing how to do this, and knowing when to do so.

Actually generating a trap is reasonably simple - just call one of the trap API routines (send_easy_trap() or send_v2trap()) with the relevant information (generic and specific trap values, or a varbind list respectively).

The 'mib2c.notify.conf' configuration file can be used to construct a suitable template routine for generating a trap, including building the variable list from the MIB trap definition. These variables can then be given suitable values, before invoking the send_v2trap() call to actually send the trap. See the 'snmp_trap_api(3)' man page for details.

Note that these APIs are only available within the agent (or subagents), and are not available to stand-alone applications. The code for 'snmptrap' shows an approach to use in such a case.

Determining when to generate the trap (either directly or via the mib2c-generated routine) is often harder. If the trap is generated in response to some action within the agent, (e.g. as the result of a SET), then this isn't too much of a problem.

But if the trap is intended to report on a change of status (e.g. a network interface going up or down, or a disk filling up), then actually detecting this is non-trivial. It's necessary to poll the value(s) on a regular basis, save the results and compare them with the new values the next time round.

The simplest way to handle this is via the DisMan Event MIB, which is designed for exactly this purpose. As long as you can specify MIB object(s) to monitor, and the values or thresholds that should trigger a notification, then this module can check these values regularly, and automatically send a suitable trap when appropriate. See the 'snmpd.conf(5)' man page (under ACTIVE MONITORING) for details.

Otherwise, you'd need to use the routines documented in 'snmp_alarm(3)' to regularly invoke a monitoring routine. This could check the necessary conditions (which need not be MIB objects), and call the 'snmp_xxx_trap()' routine (as generated by 'mib2c.notify.conf') when appropriate.