1 /** @example notification.c
2 * This example shows how to send a notification from inside the
3 * agent. In this case we do something really boring to decide
4 * whether to send a notification or not: we simply sleep for 30
5 * seconds and send it, then we sleep for 30 more and send it again.
6 * We do this through the snmp_alarm mechanisms (which are safe to
7 * use within the agent. Don't use the system alarm() call, it won't
8 * work properly). Normally, you would probably want to do something
9 * to test whether or not to send an alarm, based on the type of mib
10 * module you were creating.
12 * When this module is compiled into the agent (run configure with
13 * --with-mib-modules="examples/notification") then it should send
14 * out traps, which when received by the snmptrapd demon will look
17 * 2002-05-08 08:57:05 localhost.localdomain [udp:127.0.0.1:32865]:
18 * sysUpTimeInstance = Timeticks: (3803) 0:00:38.03 snmpTrapOID.0 = OID: netSnmpExampleNotification
23 * start be including the appropriate header files
25 #include <net-snmp/net-snmp-config.h>
26 #include <net-snmp/net-snmp-includes.h>
27 #include <net-snmp/agent/net-snmp-agent-includes.h>
32 #include "notification.h"
40 * our initialization routine
41 * (to get called, the function name must match init_FILENAME()
44 init_notification(void)
46 DEBUGMSGTL(("example_notification",
47 "initializing (setting callback alarm)\n"));
48 snmp_alarm_register(30, /* seconds */
49 SA_REPEAT, /* repeat (every 30 seconds). */
50 send_example_notification, /* our callback */
51 NULL /* no callback data needed */
55 /** here we send a SNMP v2 trap (which can be sent through snmpv3 and
56 * snmpv1 as well) and send it out. */
58 send_example_notification(unsigned int clientreg, void *clientarg)
61 * define the OID for the notification we're going to send
62 * NET-SNMP-EXAMPLES-MIB::netSnmpExampleNotification
64 oid notification_oid[] =
65 { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 1 };
66 size_t notification_oid_len = OID_LENGTH(notification_oid);
69 * In the notification, we have to assign our notification OID to
70 * the snmpTrapOID.0 object. Here is it's definition.
72 oid objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
73 size_t objid_snmptrap_len = OID_LENGTH(objid_snmptrap);
76 * here is where we store the variables to be sent in the trap
78 netsnmp_variable_list *notification_vars = NULL;
80 DEBUGMSGTL(("example_notification", "defining the trap\n"));
83 * add in the trap definition object
85 snmp_varlist_add_variable(¬ification_vars,
87 * the snmpTrapOID.0 variable
89 objid_snmptrap, objid_snmptrap_len,
91 * value type is an OID
95 * value contents is our notification OID
97 (u_char *) notification_oid,
99 * size in bytes = oid length * sizeof(oid)
101 notification_oid_len * sizeof(oid));
104 * if we wanted to insert additional objects, we'd do it here
108 * send the trap out. This will send it to all registered
109 * receivers (see the "SETTING UP TRAP AND/OR INFORM DESTINATIONS"
110 * section of the snmpd.conf manual page.
112 DEBUGMSGTL(("example_notification", "sending the trap\n"));
113 send_v2trap(notification_vars);
116 * free the created notification variable list
118 DEBUGMSGTL(("example_notification", "cleaning up\n"));
119 snmp_free_varbind(notification_vars);