Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / net-snmp / agent / mibgroup / examples / notification.c
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.
11  *
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
15  *  roughly like:
16  *
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
19  *
20  */
21
22 /*
23  * start be including the appropriate header files 
24  */
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>
28
29 /*
30  * contains prototypes 
31  */
32 #include "notification.h"
33
34 /*
35  * our alarm prototype 
36  */
37
38
39 /*
40  * our initialization routine
41  * (to get called, the function name must match init_FILENAME() 
42  */
43 void
44 init_notification(void)
45 {
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 */
52         );
53 }
54
55 /** here we send a SNMP v2 trap (which can be sent through snmpv3 and
56  *  snmpv1 as well) and send it out. */
57 void
58 send_example_notification(unsigned int clientreg, void *clientarg)
59 {
60     /*
61      * define the OID for the notification we're going to send
62      * NET-SNMP-EXAMPLES-MIB::netSnmpExampleNotification 
63      */
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);
67
68     /*
69      * In the notification, we have to assign our notification OID to
70      * the snmpTrapOID.0 object. Here is it's definition. 
71      */
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);
74
75     /*
76      * here is where we store the variables to be sent in the trap 
77      */
78     netsnmp_variable_list *notification_vars = NULL;
79
80     DEBUGMSGTL(("example_notification", "defining the trap\n"));
81
82     /*
83      * add in the trap definition object 
84      */
85     snmp_varlist_add_variable(&notification_vars,
86                               /*
87                                * the snmpTrapOID.0 variable 
88                                */
89                               objid_snmptrap, objid_snmptrap_len,
90                               /*
91                                * value type is an OID 
92                                */
93                               ASN_OBJECT_ID,
94                               /*
95                                * value contents is our notification OID 
96                                */
97                               (u_char *) notification_oid,
98                               /*
99                                * size in bytes = oid length * sizeof(oid) 
100                                */
101                               notification_oid_len * sizeof(oid));
102
103     /*
104      * if we wanted to insert additional objects, we'd do it here 
105      */
106
107     /*
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. 
111      */
112     DEBUGMSGTL(("example_notification", "sending the trap\n"));
113     send_v2trap(notification_vars);
114
115     /*
116      * free the created notification variable list 
117      */
118     DEBUGMSGTL(("example_notification", "cleaning up\n"));
119     snmp_free_varbind(notification_vars);
120 }