notification.c

This example shows how to send a notification from inside the agent. In this case we do something really boring to decide whether to send a notification or not: we simply sleep for 30 seconds and send it, then we sleep for 30 more and send it again. We do this through the snmp_alarm mechanisms (which are safe to use within the agent. Don't use the system alarm() call, it won't work properly). Normally, you would probably want to do something to test whether or not to send an alarm, based on the type of mib module you were creating.

When this module is compiled into the agent (run configure with --with-mib-modules="examples/notification") then it should send out traps, which when received by the snmptrapd demon will look roughly like:

2002-05-08 08:57:05 localhost.localdomain [udp:127.0.0.1:32865]: sysUpTimeInstance = Timeticks: (3803) 0:00:38.03 snmpTrapOID.0 = OID: netSnmpExampleNotification

00001 
00022 /*
00023  * start be including the appropriate header files 
00024  */
00025 #include <net-snmp/net-snmp-config.h>
00026 #include <net-snmp/net-snmp-includes.h>
00027 #include <net-snmp/agent/net-snmp-agent-includes.h>
00028 
00029 /*
00030  * contains prototypes 
00031  */
00032 #include "notification.h"
00033 
00034 /*
00035  * our initialization routine
00036  * (to get called, the function name must match init_FILENAME() 
00037  */
00038 void
00039 init_notification(void)
00040 {
00041     DEBUGMSGTL(("example_notification",
00042                 "initializing (setting callback alarm)\n"));
00043     snmp_alarm_register(30,     /* seconds */
00044                         SA_REPEAT,      /* repeat (every 30 seconds). */
00045                         send_example_notification,      /* our callback */
00046                         NULL    /* no callback data needed */
00047         );
00048 }
00049 
00077 void
00078 send_example_notification(unsigned int clientreg, void *clientarg)
00079 {
00080     /*
00081      * define the OID for the notification we're going to send
00082      * NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification 
00083      */
00084     oid             notification_oid[] =
00085         { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 0, 1 };
00086     size_t          notification_oid_len = OID_LENGTH(notification_oid);
00087     static u_long count = 0;
00088 
00089     /*
00090      * In the notification, we have to assign our notification OID to
00091      * the snmpTrapOID.0 object. Here is it's definition. 
00092      */
00093     oid             objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
00094     size_t          objid_snmptrap_len = OID_LENGTH(objid_snmptrap);
00095 
00096     /*
00097      * define the OIDs for the varbinds we're going to include
00098      *  with the notification -
00099      * NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatRate  and
00100      * NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatName 
00101      */
00102     oid      hbeat_rate_oid[]   = { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 2, 1, 0 };
00103     size_t   hbeat_rate_oid_len = OID_LENGTH(hbeat_rate_oid);
00104     oid      hbeat_name_oid[]   = { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 2, 2, 0 };
00105     size_t   hbeat_name_oid_len = OID_LENGTH(hbeat_name_oid);
00106 
00107     /*
00108      * here is where we store the variables to be sent in the trap 
00109      */
00110     netsnmp_variable_list *notification_vars = NULL;
00111     const char *heartbeat_name = "A girl named Maria";
00112 #ifdef  RANDOM_HEARTBEAT
00113     int  heartbeat_rate = rand() % 60;
00114 #else
00115     int  heartbeat_rate = 30;
00116 #endif
00117 
00118     DEBUGMSGTL(("example_notification", "defining the trap\n"));
00119 
00120     /*
00121      * add in the trap definition object 
00122      */
00123     snmp_varlist_add_variable(&notification_vars,
00124                               /*
00125                                * the snmpTrapOID.0 variable 
00126                                */
00127                               objid_snmptrap, objid_snmptrap_len,
00128                               /*
00129                                * value type is an OID 
00130                                */
00131                               ASN_OBJECT_ID,
00132                               /*
00133                                * value contents is our notification OID 
00134                                */
00135                               (u_char *) notification_oid,
00136                               /*
00137                                * size in bytes = oid length * sizeof(oid) 
00138                                */
00139                               notification_oid_len * sizeof(oid));
00140 
00141     /*
00142      * add in the additional objects defined as part of the trap
00143      */
00144 
00145     snmp_varlist_add_variable(&notification_vars,
00146                                hbeat_rate_oid, hbeat_rate_oid_len,
00147                                ASN_INTEGER,
00148                               (u_char *)&heartbeat_rate,
00149                                   sizeof(heartbeat_rate));
00150 
00151     /*
00152      * if we want to insert additional objects, we do it here 
00153      */
00154     if (heartbeat_rate < 30 ) {
00155         snmp_varlist_add_variable(&notification_vars,
00156                                hbeat_name_oid, hbeat_name_oid_len,
00157                                ASN_OCTET_STR,
00158                                heartbeat_name, strlen(heartbeat_name));
00159     }
00160 
00161     /*
00162      * send the trap out.  This will send it to all registered
00163      * receivers (see the "SETTING UP TRAP AND/OR INFORM DESTINATIONS"
00164      * section of the snmpd.conf manual page. 
00165      */
00166     ++count;
00167     DEBUGMSGTL(("example_notification", "sending trap %ld\n",count));
00168     send_v2trap(notification_vars);
00169 
00170     /*
00171      * free the created notification variable list 
00172      */
00173     DEBUGMSGTL(("example_notification", "cleaning up\n"));
00174     snmp_free_varbind(notification_vars);
00175 }

net-snmpに対してSat Sep 5 13:14:19 2009に生成されました。  doxygen 1.4.7