Template:FAQ:Coding 18

From Net-SNMP Wiki
Revision as of 11:32, 23 October 2006 by Dts12 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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). See the 'snmp_trap_api(3)' man page for details.

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.

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.

With the v4 UCD agent, this would have to be done manually, using the routines documented in 'snmp_alarm(3)'. The v5 Net-SNMP agent has implemented the Distributed Management Event MIB, which provides this functionality in a flexible, standardised manner. See the 'snmpd.conf(5)' man page (under DISMAN-EVENT-MIB) for details (including the need for an 'agentSecName' setting).