FAQ:Coding 14

From Net-SNMP Wiki
Jump to: navigation, search

How can I get the agent to generate a trap (or inform)?

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.

   FAQ:Coding
   
  1. How do I write C code to integrate with the agent?
  2. How does the agent fetch the value of a MIB variable from the system?
  3. Mib2c complains about a missing "mib reference" - what does this mean?
  4. Mib2c complains about not having a "valid OID" - what does this mean?
  5. Why doesn't mib2c like the MIB file I'm giving it?
  6. Mib2c ignores my MIB and generates a pair of 'mib-2' code files. Why?
  7. What's the difference between the various mib2c configuration files?
  8. Which mib2c configuration file should I use?
  9. How can I have mib2c generate code for both scalars and tables?
  10. Are there any examples, or documentation for generating MIB modules?
  11. Where should I put the files produced by 'mib2c'?
  12. Why doesn't my new MIB module report anything?
  13. Why does the iterator call my get_{first,next} routines so often?
  14. How can I get the agent to generate a trap (or inform)?
  15. How can I get an AgentX sub-agent to generate a trap (or inform)?
  16. How can I get the agent to send an SNMPv1 (or SNMPv2c) trap?
  17. How can I get the agent to include varbinds with an SNMPv1 trap?
  18. How can I get the agent to send an SNMPv1 enterprise-specific trap?
  19. How can I get the agent to send an SNMPv3 trap (or inform)?
  20. Why does calling 'send_v2trap' generate an SNMPv1 trap (or vice versa)?
  21. How can I register a MIB module in a different (SNMPv3) context?